Skip to content

Commit

Permalink
Merge pull request #9 from FSou1/feat/options-rework
Browse files Browse the repository at this point in the history
feat(options): user-defined keywords and styles
  • Loading branch information
FSou1 authored May 10, 2023
2 parents 8f0a0f9 + 5bcf32c commit 716d742
Show file tree
Hide file tree
Showing 30 changed files with 698 additions and 396 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ To use Keyword Highlighter, simply click on the extension icon in the top right-

Keyword Highlighter uses storage to store user settings and preferences, making it easy to access them every time the extension is used. This feature provides a more personalized and seamless experience for the user.

![](/docs/chrome-web-store/Screenshot_2.png)

## Contributions

If you'd like to contribute to Keyword Highlighter, please feel free to submit a pull request. We welcome any feedback or suggestions to improve the functionality of the extension.
Expand Down
Binary file modified docs/chrome-web-store/Screenshot_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chrome-web-store/Screenshot_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const manifest: chrome.runtime.ManifestV3 = {
type: "module",
},
action: {
default_popup: "src/pages/popup/index.html",
default_icon: "icon-34.png",
},
icons: {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
},
"type": "module",
"dependencies": {
"@emotion/react": "^11.11.0",
"@emotion/styled": "^11.11.0",
"@fontsource/roboto": "^4.5.8",
"@mui/icons-material": "^5.11.16",
"@mui/material": "^5.12.3",
"lodash.debounce": "^4.0.8",
"lodash.throttle": "^4.1.1",
"react": "18.2.0",
Expand Down
23 changes: 6 additions & 17 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@
"type": "module"
},
"action": {
"default_popup": "src/pages/popup/index.html",
"default_icon": "icon-34.png"
},
"icons": {
"128": "icon-128.png"
},
"permissions": [
"storage"
],
"permissions": ["storage"],
"content_scripts": [
{
"matches": [
"https://www.linkedin.com/*"
],
"js": [
"src/pages/content/index.js"
],
"css": [
"assets/css/contentStyle16834373313.chunk.css"
]
"matches": ["https://www.linkedin.com/*"],
"js": ["src/pages/content/index.js"],
"css": ["assets/css/contentStyle16836018082.chunk.css"]
}
],
"web_accessible_resources": [
Expand All @@ -39,9 +30,7 @@
"icon-128.png",
"icon-34.png"
],
"matches": [
"*://*/*"
]
"matches": ["*://*/*"]
}
]
}
}
6 changes: 6 additions & 0 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import reloadOnUpdate from "virtual:reload-on-update-in-background-script";

chrome.action.setPopup({ popup: '' });

chrome.action.onClicked.addListener(() => {
chrome.tabs.create({ url: 'src/pages/options/index.html' });
});

reloadOnUpdate("pages/background");

/**
Expand Down
42 changes: 23 additions & 19 deletions src/pages/content/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
import { useEffect, useState } from "react";
import { get } from "@services/storage/storageService";
import {
SETTINGS_KEYWORDS,
OBSERVABLE_DEBOUNCE_TIME_MS,
} from "@services/constants";
import { OBSERVABLE_DEBOUNCE_TIME_MS } from "@services/constants";
import { getRules } from "@src/services/storage/optionsService";
import { IKeywordRule } from "@src/pages/options/components/KeywordRule";
import Highlighter from "./Highlighter";

export default function App() {
const [keywords, setKeywords] = useState("");
const [rules, setRules] = useState([]);

const map = (rule: IKeywordRule) => {
return {
className: rule.id,
keywords: rule.keywords,
styles: rule.cssStyles,
};
};

useEffect(() => {
get(SETTINGS_KEYWORDS).then((result) => {
const keywords = result?.[SETTINGS_KEYWORDS] || null;
if (keywords) {
setKeywords(keywords);
}
getRules().then((rules) => {
setRules(rules.map(map));
});
}, []);

if (!keywords) {
return;
}

return (
<Highlighter
keywords={keywords}
debounceTimeMs={OBSERVABLE_DEBOUNCE_TIME_MS}
/>
<>
{rules.map((rule) => (
<Highlighter
keywords={rule.keywords}
highlightedClassName={rule.className}
debounceTimeMs={OBSERVABLE_DEBOUNCE_TIME_MS}
/>
))}
</>
);
}
8 changes: 5 additions & 3 deletions src/pages/content/components/Highlighter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo } from "react";
import React, { useCallback, useMemo } from "react";
import { findAndWrap } from "@src/lib/findAndWrapHTMLNodes";
import useMutationObservable, {
DEFAULT_OPTIONS,
Expand All @@ -7,19 +7,21 @@ import useMutationObservable, {
interface HighlighterProps {
debounceTimeMs: number;
keywords: string;
highlightedClassName: string;
}

export default function Highlighter({
debounceTimeMs,
keywords,
highlightedClassName,
}: HighlighterProps) {
const onMutation = useCallback(() => {
findAndWrap(document.body, {
findTextRegExp: new RegExp(keywords.replaceAll(",", "|"), "gi"),
wrapWithTag: "span",
wrapWithClassName: "highlighted",
wrapWithClassName: highlightedClassName,
wrapIf: (node: Text) => {
return !node.parentElement.classList.contains("highlighted");
return !node.parentElement.classList.contains(highlightedClassName);
},
});
}, []);
Expand Down
21 changes: 7 additions & 14 deletions src/pages/content/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { createRoot } from "react-dom/client";
import DebugPopup from "@src/pages/content/components/DebugPopup";
import App from "@src/pages/content/components/App";
import { get } from "@services/storage/storageService";
import {
SETTINGS_DEBUG,
SETTINGS_HIGHLIGHTED_STYLES,
SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE,
} from "@services/constants";
import { SETTINGS_DEBUG, SETTINGS_KEYWORD_RULES } from "@services/constants";
import refreshOnUpdate from "virtual:reload-on-update-in-view";
import { getRules } from "@src/services/storage/optionsService";

refreshOnUpdate("pages/content");

Expand Down Expand Up @@ -36,20 +33,16 @@ const initApp = () => {
createRoot(root).render(<App />);
};

const addStyleBlock = (styles: string) => {
const addStyleBlock = (className: string, styles: string) => {
var sheet = document.createElement("style");
sheet.innerHTML = `.highlighted { ${styles} }`;
sheet.innerHTML = `.${className} { ${styles} }`;
document.body.appendChild(sheet);
};

const initStyles = () => {
const key = SETTINGS_HIGHLIGHTED_STYLES,
defaultValue = SETTINGS_HIGHLIGHTED_STYLES_DEFAULT_VALUE;

get(key).then((result) => {
const highlightedStyles = result?.[key] || defaultValue;
if (highlightedStyles) {
addStyleBlock(highlightedStyles);
getRules().then((styles) => {
for (const style of styles) {
addStyleBlock(style.id, style.cssStyles);
}
});
};
Expand Down
38 changes: 0 additions & 38 deletions src/pages/newtab/Newtab.css

This file was deleted.

10 changes: 0 additions & 10 deletions src/pages/newtab/Newtab.scss

This file was deleted.

28 changes: 0 additions & 28 deletions src/pages/newtab/Newtab.tsx

This file was deleted.

13 changes: 0 additions & 13 deletions src/pages/newtab/index.css

This file was deleted.

12 changes: 0 additions & 12 deletions src/pages/newtab/index.html

This file was deleted.

18 changes: 0 additions & 18 deletions src/pages/newtab/index.tsx

This file was deleted.

Loading

0 comments on commit 716d742

Please sign in to comment.