我试图在pageMod和内容脚本之间传递消息。
我将此代码与pageMod一起使用
var gmailPageMod = pageMod.PageMod({
include: "https://mail.google.com/*",
contentScriptWhen: "ready",
contentScriptFile: [self.data.url('js/script.js')],
contentStyleFile: [
self.data.url("css/angular.min.css"),
self.data.url("css/style.css")
],
onAttach: function(worker) {
try {
console.log("adding worker");
gmail_workers.push(worker);
} catch (error) {
console.log("error", error);
}
worker.port.on("getTalentGamil_recipient_change", function(email) {
console.log(email);
});
try {
console.log("updating SS");
console.log(simpleStorage);
worker.port.emit("updateSimpleStorage", simpleStorage);
} catch (error) {
console.log("error", error);
}
});
和script.js
self.port.on("updateSimpleStorage", function(simpleStorage) {
console.log('updateSimpleStorage from script.js', simpleStorage);
var e = new CustomEvent('updateSimpleStorage', {
'detail': simpleStorage
});
window.dispatchEvent(e);
});
然而,这给了我一个错误Message: [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: <unknown filename> :: <TOP_LEVEL> :: line 0" data: no]
我不明白为什么?因为我认为我正在完成最新工作here。我很感激有关此事的任何帮助
答案 0 :(得分:0)
所以你从main.js发出了这个:
worker.port.emit("updateSimpleStorage", simpleStorage);
你想知道为什么它没有在script.js
中调用回调对吗?
以下是我对可能出现的问题的看法:
contentScript
script.js?simpleStorage
?它是可以序列化的吗?如果没有加载script.js,我建议你试试这个:
在main.js
onAttach
中监听这样的加载事件:
var gmailPageMod = pageMod.PageMod({
include: "https://mail.google.com/*",
contentScriptWhen: "ready",
contentScriptFile: [self.data.url('js/script.js')],
contentStyleFile: [
self.data.url("css/angular.min.css"),
self.data.url("css/style.css")
],
onAttach: function(worker) {
try {
console.log("adding worker");
gmail_workers.push(worker);
} catch (error) {
console.log("error", error);
}
worker.port.on("getTalentGamil_recipient_change", function(email) {
console.log(email);
});
worker.port.on('iLoaded', function() {
try {
console.log("updating SS");
console.log(simpleStorage);
worker.port.emit("updateSimpleStorage", simpleStorage);
} catch (error) {
console.log("error", error);
}
});
});
请注意我是如何将其包裹在iLoaded
。
然后在script.js
中发出iLoaded
这样的消息:
self.port.on("updateSimpleStorage", function(simpleStorage) {
console.log('updateSimpleStorage from script.js', simpleStorage);
var e = new CustomEvent('updateSimpleStorage', {
'detail': simpleStorage
});
window.dispatchEvent(e);
});
self.port.emit('iLoaded');
答案 1 :(得分:0)
我遇到了this,根据它,CustomEvents
不再有效,所以我使用了一个带有FF Addon SDK原生消息传递路径的环形消息来获取我的信息。