在 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/