From f8a99381cc86427e09ddd52f23f717321e8de2f9 Mon Sep 17 00:00:00 2001 From: krypt77 Date: Sat, 26 Jul 2025 14:37:40 +0200 Subject: [PATCH] Upload files to "content_scripts" --- content_scripts/replacer.js | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 content_scripts/replacer.js diff --git a/content_scripts/replacer.js b/content_scripts/replacer.js new file mode 100644 index 0000000..e5f55cf --- /dev/null +++ b/content_scripts/replacer.js @@ -0,0 +1,96 @@ +// Default dictionary as fallback +const defaultWordsToReplace = { + "oldWord1": "newWord1", + "oldWord2": "newWord2", + "oldWord3": "newWord3" +}; + +// Variable to store words to replace +let wordsToReplace = {}; + +// Function to traverse text nodes and replace words +function processNode(node) { + // Check if it's a text node + if (node.nodeType === Node.TEXT_NODE) { + let content = node.textContent; + let modified = false; + + // Perform replacements + for (const [oldWord, newWord] of Object.entries(wordsToReplace)) { + const regex = new RegExp(`\\b${oldWord}\\b`, 'gi'); // 'gi' for case insensitive + if (regex.test(content)) { + content = content.replace(regex, newWord); + modified = true; + } + } + + // Update text node only if necessary + if (modified) { + node.textContent = content; + } + } + // Recursively process child nodes (if they are not script or style tags) + else if (node.nodeType === Node.ELEMENT_NODE && + node.nodeName !== 'SCRIPT' && + node.nodeName !== 'STYLE' && + node.nodeName !== 'TEXTAREA') { + Array.from(node.childNodes).forEach(child => processNode(child)); + } +} + +// Main function to replace words +function replaceWords() { + processNode(document.body); + + // Observe DOM changes to handle dynamically loaded content + const observer = new MutationObserver(mutations => { + mutations.forEach(mutation => { + mutation.addedNodes.forEach(node => { + processNode(node); + }); + }); + }); + + observer.observe(document.body, { + childList: true, + subtree: true + }); +} + +// Function to load words from storage and start the process +function loadReplacementsAndStart() { + // Request replacements from background script + chrome.runtime.sendMessage({action: 'getReplacements'}, function(response) { + // If we have a response, use it, otherwise use the default dictionary + wordsToReplace = response || defaultWordsToReplace; + console.log("Word replacements loaded:", wordsToReplace); + + // Start the replacement process + replaceWords(); + }); +} + +// Listen for replacement updates while the page is open +chrome.storage.onChanged.addListener(function(changes, namespace) { + if (namespace === 'local' && changes.wordReplacements) { + wordsToReplace = changes.wordReplacements.newValue; + console.log("Word replacements updated:", wordsToReplace); + // Restart the replacement process with new words + replaceWords(); + } +}); + +// Listen for messages from popup/options.js +chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) { + if (message.action === 'replacementsUpdated') { + // Reload replacements and reapply + loadReplacementsAndStart(); + } +}); + +// Start loading and replacement when DOM is ready +if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', loadReplacementsAndStart); +} else { + loadReplacementsAndStart(); +} \ No newline at end of file