Upload files to "content_scripts"
This commit is contained in:
96
content_scripts/replacer.js
Normal file
96
content_scripts/replacer.js
Normal file
@ -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();
|
||||
}
|
Reference in New Issue
Block a user