我在我的Firefox SDK应用程序中使用了,我想转换成Webextension。
我在背景js文件的顶部声明:
var pageMod = require("sdk/page-mod");
我在中没有开发,是在manifest.json
中声明的
"content_scripts": [
{
"matches": ["*://*.mytestsite.com/*"],
"js": ["background.js"]
}
]
但是我在我的pageMod()
脚本中调用了background
,我有很多附件和其他内容。我不知道我怎么能改变这个。
,例如,我想要转换的东西:
pageMod.PageMod({
include: "*.mytestsite.com",
contentScriptFile: [self.data.url("jquery-2.0.3.min.js"), self.data.url("jquery-ui.min.js"), self.data.url("site_modal_min.js"), self.data.url("timerreview.js")],
onAttach: function onAttach(worker) {
如何使用我的PageMod()
调用与Webxtension
发布于 2017-12-07 03:32:23
请注意,在manifest.json中,不能在content_scripts标记中包含background.js。相反,您应该这样做(假设域的匹配选择器是正确的):
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://*.mytestsite.com/*"],
"js": ["timerreview.js", "jquery.js"]
}
]
如果您想要通信(变量等),请在后台脚本和内容脚本之间使用消息传递。这特别有用,因为大多数WebExtensions API只对后台脚本可用。
在内容脚本中添加:
// Listen for messages from the background script
browser.runtime.onMessage.addListener(onMessage);
function onMessage(message) {
switch(message.action)
case "testAction":
testAction(message.data);
break;
default:
break;
}
}
function sendMessage(action, data){
browser.runtime.sendMessage({"action": action, "data": data});
}
在后台脚本中,添加:
// listen for messages from the content or options script
browser.runtime.onMessage.addListener(function(message) {
switch (message.action) {
case "actionFromContent":
doSomething(); // message.data is not needed
break;
default:
break;
}
});
// See also https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/Tabs/sendMessage
function sendMessage(action, data){
function logTabs(tabs) {
for (tab of tabs) {
browser.tabs.sendMessage(tab.id, {"action": action, "data": data}).catch(function(){
onError("failed to execute " + action + "with data " + data);
});
}
}
browser.tabs.query({currentWindow: true, active: true}).then(logTabs, onError);
}
function onError(error){
console.error(error);
}
现在您可以使用:
sendMessage("actionFromContent") from within the content script to send a message to the background script
sendMessage("testAction", "someData") from within the background script to the active content script (the active tab)
https://stackoverflow.com/questions/47693569
复制相似问题