Files
Firefox-Word-Replacer-Addon/background.js
2025-07-26 14:32:08 +02:00

49 lines
1.6 KiB
JavaScript

// Extension initialization
chrome.runtime.onInstalled.addListener(() => {
console.log("Word Replacer Addon installed.");
// Set default replacements if they don't already exist
chrome.storage.local.get('wordReplacements', (data) => {
if (!data.wordReplacements) {
chrome.storage.local.set({
wordReplacements: {
"oldWord1": "newWord1",
"oldWord2": "newWord2",
"oldWord3": "newWord3"
}
});
}
});
});
// Data cleanup when possible
function cleanupStorageData() {
chrome.storage.local.clear(() => {
console.log("Extension storage cleared.");
});
}
// For Firefox we can use onSuspend which is called when the extension process
// is terminated (can happen on deactivation or uninstallation)
chrome.runtime.onSuspend.addListener(() => {
console.log("Word Replacer Addon terminated.");
// Uncomment the following line if you want to remove all data on suspension
// cleanupStorageData();
});
// Add a command to manually clean the data
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'getReplacements') {
chrome.storage.local.get('wordReplacements', (data) => {
sendResponse(data.wordReplacements || {});
});
return true; // Required for asynchronous sendResponse
}
// New command to clean the data
if (message.action === 'clearStorageData') {
cleanupStorageData();
sendResponse({success: true});
return true;
}
});