Javascript Firefox插件SDK-pageMod更新contentScriptOptions?

Javascript Firefox插件SDK-pageMod更新contentScriptOptions?,javascript,firefox,firefox-addon-sdk,Javascript,Firefox,Firefox Addon Sdk,我正在使用我的main.js中的以下代码向example.com上的页面添加内容脚本: var self = require("sdk/self"); var pageMod = require("sdk/page-mod"); pageMod.PageMod({ include: "https://example.com/*", contentScriptWhen: "ready", contentScriptFile: [

我正在使用我的
main.js
中的以下代码向
example.com
上的页面添加内容脚本:

var self = require("sdk/self");
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
        include: "https://example.com/*",
        contentScriptWhen: "ready",
        contentScriptFile: [
            self.data.url("example.js"),
        ],
        contentScriptOptions: {
            myString: 'helloWorld'
        }
    });
如果我设置了一个工作进程或事件侦听器,告诉
main.js
更新
myString
(可以使用
self.options.myString
从内容脚本访问),我如何在
example.js
内容脚本中反映下一次
example.com
页面加载的更改

我尝试使用新的
myString
值再次调用
pageMod.pageMod
函数,但这会导致
example.js
在页面上运行两次

编辑:

我已经实现了,但是现在我被困在如何更新
contentScriptOptions
传递给内容脚本的对象上

/* main.js */
var self = require("sdk/self");
var pageMod = require("sdk/page-mod");

pageMod.PageMod({
    include: "https://example.com/*",
    contentScriptWhen: "ready",
    contentScriptFile: [
        self.data.url("example.js"),
    ],
    contentScriptOptions: {
        myString: 'foo'
    },
    onAttach: function(worker) {
        worker.port.on("updateOptions", function(data) {

            // how to replace myString with value from data.myString?

        });
    }
});


对选项的更新不会传播到已加载的辅助对象。您必须使用通过的消息来与内容脚本通信。

您需要端口API

/* main.js */
var self = require("sdk/self");
var pageMod = require("sdk/page-mod");

var externalStringVar =  "helloWorld";

var pageMod = pageMod.PageMod({
    include: "https://example.com/*",
    contentScriptWhen: "ready",
    contentScriptFile: [self.data.url("example.js")],
    contentScriptOptions: {
        myString: externalStrVar; // Pass it to the content script
    }
    onAttach: function(worker) {
        worker.port.on("nameForTheMessage", function(updatedString) {
            // update string value when a message is received like so:
            externalStringVar = updatedString;
        });
    }
});
在内容脚本中,
emit
侦听页面模块将接收的消息

/* example.js */  
self.port.emit("nameForTheMessage", "bar"); // self is a global variable  
在PageMod中,调用内容脚本的函数
onAttach

/* main.js */
var self = require("sdk/self");
var pageMod = require("sdk/page-mod");

var externalStringVar =  "helloWorld";

var pageMod = pageMod.PageMod({
    include: "https://example.com/*",
    contentScriptWhen: "ready",
    contentScriptFile: [self.data.url("example.js")],
    contentScriptOptions: {
        myString: externalStrVar; // Pass it to the content script
    }
    onAttach: function(worker) {
        worker.port.on("nameForTheMessage", function(updatedString) {
            // update string value when a message is received like so:
            externalStringVar = updatedString;
        });
    }
});

我尝试了这一点,但是
worker.port.on
函数中的
myString
contentScriptOptions
中传递给内容脚本的
myString
不同。它没有任何效果。我已经编辑了我的问题,以显示到目前为止我得到了什么。@iglvzx我也更新了我的答案(请注意,这两个文件都有更改)。现在myString数据没有传递到内容脚本。@iglvzx我对误解表示歉意。包含
contentScriptOptions
的新编辑功能应该可以帮助您实现最简单意义上的目标。或者您的意思是更新
contentScriptOptions
myString
的值吗?请注意,作为
contentScriptOptions
传递的对象不能包含任何函数。