I tried to use something like document.forms
in Firefox-addon script but it doesn't work.
So, I need to manipulate DOM objects in Firefox-addon script such as forms, inputs... etc. How can I do that without using SDK?
I tried to use something like document.forms
in Firefox-addon script but it doesn't work.
So, I need to manipulate DOM objects in Firefox-addon script such as forms, inputs... etc. How can I do that without using SDK?
Share Improve this question edited Jul 8, 2014 at 12:24 nmaier 33.2k5 gold badges65 silver badges79 bronze badges asked Jul 8, 2014 at 7:34 user3377708user3377708 1616 silver badges18 bronze badges 4- Where is your script running? Or how are you invoking it? – Wladimir Palant Commented Jul 8, 2014 at 8:06
- It is a javascript file inside my Firefox plugin ".xpi" file. – user3377708 Commented Jul 8, 2014 at 10:47
- A JavaScript file inside an xpi file won't execute just like that. Once again, how are you running it? Browser overlay? XPCOM ponent? SDK module? – Wladimir Palant Commented Jul 8, 2014 at 11:14
- Yes Browser overlay ".xul" file. – user3377708 Commented Jul 8, 2014 at 11:18
2 Answers
Reset to default 4document.forms
will not work, because document
is not what you think it is: It is the top level browser (Firefox) window, and not the content in a tab.
A Firefox browser window can have multiple tabs, one of which is the active tab. The active tab <browser>
element (which is the XUL element containing the actual content document) also has a shortcut named content
, e.g. content.document.forms
will be a collection of forms in the active tab.
So you'll have to adjust your mental model here from
window
anddocument
refer to a website
to
window
anddocument
refer to the top-level browser window that may contain a lot of different websites.
The top-level window is more like a document containing multiple frames (the actual websites), really, but with a different APIs to access them.
So, e.g. when executing some action after the user pressed some add-on toolbar button, it might be enough to just use content.document.forms
to get the forms of the currently active tab.
But using content.
is often not enough: Add-ons would listen for page loads in tabs as the user navigates by adding appropriate event listeners to the <tabbrowser>
element (gBrowser
), which is the element containing all tabs. MDN has some code snippets for that and lots of other stuff.
Other add-ons add item(s) to the content context menu (contentAreaContextMenu
) and use the popupshowing
event to know what DOM node (and by this what .ownerDocument
and content window == .ownerDocument.defaultView
) is currently focused.
An important thing to always keep in mind: Your add-on code runs with full privileges, while websites of course do not. So be careful not to write insecure code. E.g. all forms of unbound eval
are evil.
Judging by your ments, your code is running in the context of the browser window. This means that document
refers to the document of the browser window, not the document that is loaded into it. The easiest way to get to the latter is using the window.content
property:
var contentDoc = content.document;
alert(contentDoc.forms.length);
This will give you only the current tab however. For the other tabs you can use the APIs provided by the <tabbrowser>
element (accessible via the global gBrowser
variable), e.g. to access the first tab:
var contentDoc = gBrowser.browsers[0].contentDocument;
alert(contentDoc.forms.length);