Im trying to pass messages between a pageMod and a content script.
I use this code with the 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);
}
});
and in 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);
});
However this gives me an error Message: [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: <unknown filename> :: <TOP_LEVEL> :: line 0" data: no]
I dont understand why though? Because I think Im doing exactly whats being done here . I'd appreciate any help on the matter
I'm reading this here - https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts/using_port#Accessing_port_in_the_Add-on_Script
So you are emitting from main.js this:
worker.port.emit("updateSimpleStorage", simpleStorage);
And you are wondering why its not calling the callback in script.js
right?
Here are my thoughts on possible issues:
contentScript
of script.js has not loaded yet? simpleStorage
? Is it serializ-able? If script.js has not loaded, I would recommend trying this:
In main.js
in onAttach
listen for a load event like this:
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);
}
});
});
Notice how I wrapped it in a iLoaded
.
Then in script.js
emit the iLoaded
message like this:
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');
我碰到了这一点 ,根据它, CustomEvents
不再起作用,因此我使用了带有FF Addon SDK原生消息传递功能的回旋消息传递路径来传递我的信息。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.