Reputation: 17040
Supposed my add-on has to access a Javascript variable from a webpage. Let's take https://mozilla.org. There is a global variable called optimizelyCode
. My addon will only work if this variable is accessible.
How do I let my pageMod do that?
To experiment, here are some tutorial scripts:
var widgets = require("sdk/widget");
var tabs = require("sdk/tabs");
var widget = widgets.Widget({
id: "mozilla-link",
label: "Mozilla website",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: function() {
tabs.activeTab.attach({
contentScriptWhen: 'end',
contentScript:
'console.log(optimizelyCode)'
})
}
});
and
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.mozilla.org",
contentScriptWhen: 'end',
contentScript: 'console.log(optimizelyCode);'
});
I get Reference/Undefined error because optimizelyCode
is not avaiaible to addon. In this example I am not even using content script file.
As far as I know, I can only access basic DOM stuff like getElementById
that kind of methods. How can I access these webpage local JS variables?
Reference: https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts
Upvotes: 1
Views: 569
Reputation: 32063
Since this is add-on SDK, it uses a different syntax from the rest of Firefox, and you need to use unsafeWindow.optimizelyCode
.
Upvotes: 1
Reputation: 37228
Just FYI, from non-content-script you can do gBrowser.contentWindow.wrappedJSObject.VAR_HERE
Upvotes: 2