javascript - addon.port.on 未收到来自插件的消息

标签 javascript firefox-addon firefox-addon-sdk

main.js 中,我创建了一个 Panel
ui.html 包含一个 js 源文件,我在其中监听来自 Panel 的消息。
我从未见过恢复转储控制台,为什么这个函数从未被调用?

panel = PanelAPI.Panel({
        width: 300,
        height: 400,
        contentURL: Data.get("html/ui.html")
    });

    panel.port.emit('previousHistory', SimpleStorage.getHistory(), SimpleStorage.getCurrentHistoricalEntry());

    panel.port.on("historyUpdate", function (history, currentHistoricalEntry) {
        SimpleStorage.setHistory(history);
        SimpleStorage.setCurrentHistoricalEntry(currentHistoricalEntry);
    });

contentURL: Data.get("html/ui.html")js 文件包含此内容以监听消息..

addon.port.on("previousHistory", function(history, currentHistoricalEntry) {
    console.log("restoring");
    Namespace.restoreHistory(history, currentHistoricalEntry);
});

SimpleStorage.js,是我处理对 simple-storage api 访问的地方..

var ss = require("sdk/simple-storage");

exports.getHistory = function(){
    console.log("retrieving ss: " + ss.storage.history)
    return ss.storage.history;
}

exports.setHistory = function(history){
    ss.storage.history = history;
    console.log("history in ss is now: " + ss.storage.history)
}

exports.setCurrentHistoricalEntry = function(currentHistoricalEntry){
    ss.storage.currentHistoricalEntry = currentHistoricalEntry;
    console.log("currhisent is now" + ss.storage.currentHistoricalEntry);
}

exports.getCurrentHistoricalEntry = function(){
    console.log("retrieving ss.currentr: " + ss.storage.currentHistoricalEntry);
    return ss.storage.currentHistoricalEntry;
}

最佳答案

this.port.emit('previousHistory', prefs); 移动到 Panel.onShow() 有效..

panel = PanelAPI.Panel({
        width: 300,
        height: 400,
        contentURL: Data.get("html/ui.html"),
        onShow: function() { 

            var prefs = JSON.stringify({
                history: SimpleStorage.getHistory(),
                currentHistoricalEntry: SimpleStorage.getCurrentHistoricalEntry()
            });

            this.port.emit('previousHistory', prefs);
        }
    });

关于javascript - addon.port.on 未收到来自插件的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923392/

相关文章:

google-chrome - Chrome中的REST控制台扩展

javascript - 如何在 firefox 插件 sdk 1.10 main.js 中使用 nsIParserUtils?

javascript - 从javascript为不同的域设置cookie

javascript - 如何让 'get' 请求在 NodeJS 中按计划运行?

javascript - 对象变量初始化

android - 如何使用 page-mod 和 addon-sdk-1.17 为 Firefox Mobile 编写一个简单的附加组件?

java - 如何从 Java 应用程序连接到 Cisco Show and Share API

javascript - 无法从循环外访问数组 firefox addon sdk

javascript - 如何从 Firefox 附加 SDK 扩展启动正常下载

javascript - 将字幕加载到 Netflix 播放器