I am developing a chrome extension, which injects two separate content scripts…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I am developing a chrome extension, which injects two separate content scripts (topFrame.js and aboutBlank.js) into the same targeted page. The two content scripts work together to hide/show certain UI elements on the page, depending on certain conditions.
My extension is mostly working the way I want it to.
However, there is one thing that I am trying to address: In the event that a user of the extension decides to disable or uninstall the extension, with the targeted page still open, I want aboutBlanks.js to send a message to topFrame.js (or vice versa).
In aboutBlank.js, I am detecting a likely uninstall event by explicitly catching and handling an error message that includes the string "Extension context invalidated". In this catch block, I tear down the still-active functionality in aboutBlank.js (namely, a keydown event listener). However, in this same catch block, I would also like to send a message to my topFrame.js script, indicating the "Extension context invalidated" event and thus signaling to the topFrame.js script to tear down its own functionality (namely, a click event listener).
I am considering a few different approaches: Custom DOM Events, BroadcastChannel API, window.postMessage. For my scenario, which of these approaches would you recommend? Or would you recommend a different, unmnentioned approach?
In the latest version of my extension, I am actually already dispatching (and listening for) a custom event, in order to make my content scripts indempotent.
If you recommend that I should pursue the Custom DOM Events approach, here are some additional questions...
For dispatching a custom event related to an uninstall event, should I set up discrete listeners for this custom event? Or, should I use the same event listener for both scenarios and then just handle each accordingly in the event listener's callback?
Here is the relevant code related to ensuring my aboutBlank.js content script is indempotent (and I have similar code within topFrame.js)...
(function () {
console.log(aboutBlank.js. (ts: ${ts()}));
if (window[chrome.runtime.id + "initializedAboutBlank"]) {
console.log(Content script already initialized, so we will halt any further execution of this particular script. (ts: ${ts()}));
return;
}
window[chrome.runtime.id + "initializedAboutBlank"] = true;
// Check for orphaned aboutBlank.js content script. let orphanMessageIdAbout = chrome.runtime.id + "orphanCheckAbout"; window.dispatchEvent(new Event(orphanMessageIdAbout)); window.addEventListener(orphanMessageIdAbout, unregisterOrphanAbout);
// If nec, deactivate orphaned script.
function unregisterOrphanAbout() {
try {
console.log(unregisterOrphanAbout(). (ts: ${ts()}));
if (chrome.runtime.id) {
console.log(`chrome.runtime.id is true, so exiting unregisterOrphanAbout() function. (ts: ${ts()})`);
return;
}
// Tear down script
// Code not included
} catch (error) {
console.log("unregisterOrphan()", error);
}
}
// More Code... // Code not included.
})();
Based on your scenario, I would recommend using Custom DOM Events for a few reasons:
Here's how I would structure the solution:
// In aboutBlank.js
const EXTENSION_DISABLED_EVENT = chrome.runtime.id + "extensionDisabled";
try {
// Your existing code
} catch (error) {
if (error.message.includes("Extension context invalidated")) {
// Tear down aboutBlank.js functionality
// Notify topFrame.js
window.dispatchEvent(new CustomEvent(EXTENSION_DISABLED_EVENT, {
detail: {
source: 'aboutBlank',
timestamp: Date.now()
}
}));
}
}
// In topFrame.js
const EXTENSION_DISABLED_EVENT = chrome.runtime.id + "extensionDisabled";
window.addEventListener(EXTENSION_DISABLED_EVENT, (event) => {
// Tear down topFrame.js functionality
});
Regarding your question about discrete vs. combined listeners: Since the extension disable/uninstall scenario is distinct from your indempotence check, I recommend using separate event listeners. This maintains better separation of concerns and makes the code more maintainable. Each listener can focus on its specific responsibility:
The BroadcastChannel API would be overkill for your use case since you're just communicating between scripts on the same page. window.postMessage would work but requires more setup and careful origin checking that isn't necessary for your scenario.
You might also want to consider adding a cleanup function that both scripts can call:
function cleanupExtensionEffects() {
// Remove event listeners
// Reset UI elements
// Clear any stored state
// etc.
}
// Use in both scripts when needed
window.addEventListener(EXTENSION_DISABLED_EVENT, (event) => {
cleanupExtensionEffects();
});
This ensures consistent cleanup behavior regardless of which script detects the extension being disabled.