The index.js
script has no access to any document
or any window
object. index.js
is the main add-on or background script. Its execution environment and context are very specific to the addon SDK.
Therefor, your piece of code should take place in a content script (which “share” the DOM of the Web pages you want to deal with). The SDK provide the module page-mod
to load such scripts: https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/page-mod.
Also, see this documentation page to learn more about those two types of scripts: https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Two_Types_of_Scripts
N.B.: this separation does exist on Chromium add-ons as well. However, a Chromium background page is actually a virtual sort of Web page (with its own DOM). As a matter of fact, on such scripts, this
is actually a window
object; so you can pretty much access to every standard APIs you’ll find on any Web page. However, you’ll never access directly to the DOM of a Web page the user is browsing from a background script.