I can't seem to access the window object in a content script. Is this normal?
For example, this does nothing:
window.onload = function() {
console.log("Hello from the onload");
};
Instead, I have to use the unsafeWindow
object.
unsafeWindow.onload = function() {
console.log("Hello from the onload");
};
I must be missing something simple right?
Don't use window.onload
, instead write:
window.addEventListener("load", function() {
console.log("Hello from the onload");
}, false);
window.onload
has the limitation that there can only be one event listener, setting a different listener replaces the existing one - that's already a reason why you should never use it. In case of the Add-on SDK things get more complicated because the content script has a different view of the DOM then the web page. So just use addEventListener
.
Oh, and please don't use unsafeWindow
- it is (as the name already says) inherently unsafe.