Self.port未定义

时间:2014-06-09 11:20:37

标签: firefox firefox-addon firefox-addon-sdk add-on browser-addons

我在popup.html中有一个iframe,有id = Receiver,它接收并发布消息。

以下代码来自popup.js:

self.port.on("show", function(title, url) {
    myApp.initialize(title, url);
});


var arr = new Array();
var myApp = {
    initialize: function (url,title){
        arr = [];
        arr.push(url);
        arr.push(title);

        var receiver = document.getElementById('Receiver').contentWindow;
        receiver.postMessage(arr, "*");
    },
    sendDetails : function(){
        alert("arr :"+arr);
    },
    closeIt : function(){
        self.port.emit("close-it");
    }
}  

window.addEventListener("message" , receiveMessageOnce, false);
function receiveMessageOnce(event){
    myApp.closeIt();
}

main.js的代码是:

main_panel.on("show", function() {
    main_panel.port.emit("show", UrlActiveTab, TitleActiveTab);
});

现在,我有两个问题:
1.)每当它收到消息时,都会触发myApp.CloseIt()。但是控制台说self.port是未定义的。我尝试过使用也会出错的addon.port 2.)如果调用myApp.sendDetails(),它会提醒' arr'虽然是全球阵列,但仍然是空白。为什么这样?

EDITED:面板构造函数代码:

var { ToggleButton } = require('sdk/ui/button/toggle');
const panel = require('sdk/panel');
var main_panel = panel.Panel({
    contentURL: data.url("popup.html"),
    contentScriptFile: [
        data.url('js/jquery-1.10.2.min.js'),
        data.url("js/popup.js")
    ],
    width: 350,
    height: 400
});

var button = ToggleButton({
    id: "myaddon_beta",
    label: "My Addon",
    icon: {
        "16": "./img/icon_main_16.png",
        "32": "./img/icon_main_32.png",
        "64": "./img/icon_main_64.png"
    },
    onChange : handleChange
});

function handleChange(state){
    currentUrl = tabs.activeTab.url;
    currentTitle = tabs.activeTab.title;
    if(state.checked){
        main_panel.show({
            position : button
        });

        button.state("window", {
            checked: false
        });
    }
}

1 个答案:

答案 0 :(得分:2)

小组被视为可信内容,因为您拥有它们。这样做的副作用是,消息传递API可通过addon全局而不是self获得,就像普通内容脚本一样。