如何让插件的内容脚本访问当前选项卡的JavaScript变量? [英] How to allow addon's content script access Javascript variables from the current tab?
问题描述
optimizelyCode
。我的插件将只能工作,如果这个变量是可访问的。 我怎么让我的pageMod做到这一点?
为了进行实验,下面是一些教程脚本:
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)'
})
}
});
和
var pageMod = require(sdk / page-mod);
pageMod.PageMod({
include:* .mozilla.org,
contentScriptWhen:'end',
contentScript:'console.log(optimizelyCode) ;'
});
我得到Reference / Undefined错误,因为 optimizelyCode
插件无法使用。在这个例子中,我甚至没有使用内容脚本文件。
据我所知,我只能访问基本的DOM东西,如 getElementById code>那种方法。如何访问这些网页本地JS变量?
参考: https://developer.mozilla.org/zh-CN/Add-ons/SDK/Guides/Content_Scripts/Loading_Content_Scripts
$ b $由于这是附加SDK,所以它使用与Firefox其余部分不同的语法,并且您需要使用解决方案 /developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/Accessing_the_DOM#unsafeWindowrel =nofollow> unsafeWindow.optimizelyCode
。
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
Since this is add-on SDK, it uses a different syntax from the rest of Firefox, and you need to use unsafeWindow.optimizelyCode
.
这篇关于如何让插件的内容脚本访问当前选项卡的JavaScript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!