简体   繁体   English

Firefox附加SDK:在侧边栏和主脚本之间通信对象

[英]Firefox Add-on SDK: communicating objects between a sidebar and the main script

I am hacking away at my first addon, so the problem I am running into probably has either an obvious solution or my general approach is flawed. 我是第一个插件,所以遇到的问题可能有一个明显的解决方案,或者我的一般方法有缺陷。

I am using the Add-on SDK . 我正在使用附加SDK My lib/main.js contains an object foo . 我的lib/main.js包含一个对象foo The data directory has the files sidebar.html and sidebar.js . data目录包含文件sidebar.htmlsidebar.js I want to pass foo to the sidebar-script and then change the sidebar depending on the state of foo . 我想将foo传递到侧边栏脚本,然后根据foo的状态更改侧边栏。 In the minmal example I will just try to display the object. 在最小的示例中,我将尝试显示对象。

Minimal example: 最小示例:

main.js : main.js

var foo = {"a": "b", "c": "d"};
var bar = require("sdk/ui/sidebar").Sidebar({
    id: "sb",
    title: "foobar",
    url: "./sidebar.html"
});
bar.show();

sidebar.html : sidebar.html

<!DOCTYPE html>
<html>
    <body>        
        <div id="foo-here">
            <!-- to be modified by sidebar.js -->
        </div>
    </body>
    <script type="text/javascript" src="sidebar.js"></script>
</html>

sidebar.js : sidebar.js

var foodiv = document.getElementById("foo-here");
var str = "", o;
// magically get foo from main.js here
for (o in foo) {
    if (foo.hasOwnProperty(o)) {
        str += "[" + o + " is " + foo[o] + "]";
    }
}
foodiv.innerHTML = str;

The sidebar docs explain how to listen for signals from the main script and call a function upon receiving a signal, but I can't figure out how to pass the object itself. 边栏文档解释了如何侦听来自主脚本的信号并在收到信号后调用函数,但是我不知道如何传递对象本身。

您链接的侧边栏文档中提到的port.emit() API也需要第二个参数来传递对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM