feat: global on off toggle

This commit is contained in:
2025-06-23 11:20:51 +03:00
parent 2d0878f562
commit 428c97b3c5
7 changed files with 176 additions and 15 deletions

View File

@@ -1,4 +1,5 @@
let currentLists = [];
let isGlobalHighlightEnabled = true;
function escapeRegex(s) {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -19,9 +20,18 @@ function clearHighlights() {
function processNodes() {
observer.disconnect();
clearHighlights();
// If global highlighting is disabled, skip processing
if (!isGlobalHighlightEnabled) {
observer.observe(document.body, {
childList: true,
subtree: true,
characterData: true
});
return;
}
const textNodes = [];
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
acceptNode: node => {
@@ -35,7 +45,6 @@ function processNodes() {
while (walker.nextNode()) textNodes.push(walker.currentNode);
const activeWords = [];
for (const list of currentLists) {
if (!list.active) continue;
for (const word of list.words) {
@@ -92,8 +101,12 @@ function debounce(func, wait) {
}
// Initial highlight on load
chrome.storage.local.get("lists", ({ lists }) => {
chrome.storage.local.get(["lists", "globalHighlightEnabled"], ({ lists, globalHighlightEnabled }) => {
if (Array.isArray(lists)) setListsAndUpdate(lists);
if (globalHighlightEnabled !== undefined) {
isGlobalHighlightEnabled = globalHighlightEnabled;
}
processNodes(); // Initial processing
});
// Listen for updates from the popup and re-apply highlights
@@ -102,6 +115,9 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.storage.local.get("lists", ({ lists }) => {
if (Array.isArray(lists)) setListsAndUpdate(lists);
});
} else if (message.type === "GLOBAL_TOGGLE_UPDATED") {
isGlobalHighlightEnabled = message.enabled;
processNodes();
}
});
@@ -112,4 +128,5 @@ observer.observe(document.body, {
subtree: true,
characterData: true
});
window.addEventListener('scroll', debouncedProcessNodes);
window.addEventListener('scroll', debouncedProcessNodes);