-
Notifications
You must be signed in to change notification settings - Fork 4
/
content.js
28 lines (25 loc) · 974 Bytes
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
chrome.storage.sync.get("keywords", ({ keywords }) => {
if (keywords && keywords.length > 0) {
const regexPattern = new RegExp(`\\b(${keywords.join("|")})\\b`, "gi");
function highlightKeywords(node) {
if (node.nodeType === Node.TEXT_NODE) {
const matches = node.nodeValue.match(regexPattern);
if (matches) {
const replacedText = node.nodeValue.replace(regexPattern, match => {
return `<span class="keyword-highlight">${match}</span>`;
});
const span = document.createElement("span");
span.innerHTML = replacedText;
node.parentNode.replaceChild(span, node);
}
} else if (node.nodeType === Node.ELEMENT_NODE) {
for (const childNode of node.childNodes) {
highlightKeywords(childNode);
}
}
}
setTimeout(() => {
highlightKeywords(document.body);
}, 1000);
}
});