I'm working on a browser extension, and I need specific code to run when the new window is loaded. The below code works perfectly in Chrome/Edge:
window.addEventListener('load', (event) => {
handle_data();
});
This event is never triggered in the Firefox version. Alternatively, I tried:
window.onload = handle_data;
But this is called before the DOM of the page is loaded. Any idea why this is an issue in Firefox?
EDIT: The script is executed using the following:
function open_tab() {
chrome.tabs.create({
url: "https://www.test.ca/apd/myad",
active: false
}, function (tab) {
chrome.tabs.executeScript(tab.id, { file: "manage_content_script.js" });
});
}
And is called on the click of the extension, using the following code in background.js:
if (msg.content == "clicked") {
chrome.tabs.executeScript(null, { file: "dom_script.js" });
open_tab();
}
Have you tried DOMContentLoaded?
window.addEventListener('DOMContentLoaded', (event) => {
console.log('DOM fully loaded and parsed');
});
See: https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event