BrunoLM
BrunoLM

Reputation: 100361

How to edit a property on `window` from Firefox addon?

I want to make an addon that modifies one value on the window.

Let's take for example a simple window:

<html>
    <head>
        <title></title>
        <script>
            window.hello = 1;
        </script>
    </head>

    <body>

    </body>
</html>

Using

gBrowser.addEventListener("DOMContentLoaded",
    function (e)
    {
        e.originalTarget.defaultView.hello = 2;
    }, false);

does not modify the value of window.hello. Meaning e.originalTarget.defaultView != window.

How can I access the pure window?

Upvotes: 2

Views: 456

Answers (1)

skabbes
skabbes

Reputation: 900

e.originalTarget refers to the document element of that page. To access the window element of the page, you use e.target.defaultView. However, in order to stay withing the bounds of Mozilla's security protocols, you must access the window object through its wrappedJSObject property. Overall, you'd change the variable like:

e.target.defaultView.wrappedJSObject.hello = 2

Upvotes: 3

Related Questions