I'm creating an add-on on Firefox Dev Edition (v100.0b7) and the background script script.js doesn't invoke window.onload. It only works when the developer console is open in Inspect Element and I see the message it prints to console. The onload function is meant to start a listener as soon as the page loads, so the add-on popup (when clicking on icon in top-right) can send a request for DOM content. (afaik, popup has its own DOM so can't access page's DOM directly). I can tell it's not working when dev console is closed because the popup doesn't show any data fetched from page DOM
Here's the script files. The rest are HTML/CSS which work as expected:
manifest.json
{
"manifest_version": 2,
"name": "Extension",
"version": "1.0",
"content_scripts": [
{
"matches": [
"https://example.com"
],
"js": [
"script.js"
]
}
],
"permissions": [
"storage",
"activeTab"
],
"browser_action": {
"default_title": "Extension",
"default_icon": "icon.png",
"default_popup": "index.html"
},
"browser_specific_settings": {
"gecko": {
"id": "example@example.com"
}
}
}
script.js
// This script should run immediately when window finishes loading
window.onload = function () {
console.log("Loaded successfully");
browser.runtime.onMessage.addListener(messageHandler);
};
addon-popup-window/script.js
// This script only runs when the popup is opened (user clicks top-right icon)
window.onload = function () {
browser.tabs.query({ currentWindow: true, active: true })
.then((tabs) => {
for (const tab of tabs) {
browser.tabs.sendMessage(tab.id, "message request")
.then(responseHandler);
}
});
};