How to send message from content script to panel?
I got some problems with the messaging among the main.js, page-mod's content script and panel's content script in my firefox addon.I transfer the chrome extension code to firefox addon.
What is the order of these js files loaded? I try to figure out with console,but the log is refreshed.
I have transfer the chrome specific API to firefox addon API,but I can't sure the other native message js code should change.Now,I will show you the example. in main.js
:
var pmworker = []
var pagemod = PageMod({
include: ['*'],
contentScriptWhen: 'start',
contentScriptFile: [data.url('pm-content.js')],
onAttach: function(worker) {
worker.port.on('message',
function(data) {
switch (data.type) {
case 'some type':
do something;
break;
}
});
worker.on('detach',function() {
detachWorker(this, pmworker);
});
pmworker.push(worker);
}
});
var panel = require("sdk/panel").Panel({
width: 322,
height: 427,
contentURL: data.url("panel.html"),
include:["http://*/*","https://*/*"]
});
panel.on('message', function(messagedata) {
switch(messagedata.type) {
case 'some type':
panel.port.emit("something");
break;
}
});
in pm-content.js
:
var sendInfoToFrame = function(){
var frameWindow = document.getElementById(iframeId).contentWindow;
var infoMsg = {type:"some type",content:something};
frameWindow.postMessage(infoMsg, "*");
}
in the panel.html
's head
include a panel.js
with script
tag:
function addParentListener(){
window.addEventListener("message",function (e) {
var message=e.data;
if(message.type="some type"){
//do something
}
},false);
}
so the message can be sent from pm-content.js
to panel.js
with contentWindow.postMessage
? And the panel.js
use window.addListener
to receive the msg(these example code from panel.js
and pm-content.js
is copied from chrome extension code,and I don't change it)?
Conversely,How is the msg sent from panel.js
to content script ? I also copy the same code from chrome and I don't whether it's right. The example code is: in the panel.js:
function sendSomeMessage(value){
var message = {type : "sometp",value: value};
window.parent.postMessage(message, "*");
}
and the receive code in the pm-content.js
:
function addMessageListener() {
window.addEventListener("message",function(e) {
var message = e.data;
if (message.type == null) {
return;
}
var msgType = message.type;
switch (msgType) {
case "sometp":
//do something
}
}, false);
}
I run the code and I get lots of errors.So should I change these messaging code and How ? Thank you.
What is the order of these js files loaded? I try to figure out with console,but the log is refreshed.
Use the Network Panel instead of the console
I have transfer the chrome specific API to firefox addon API,but I can't sure the other native message js code should change.Now,I will show you the example. in main.js:
Use or analyze an existing tool, as in a similar question:
Cross-Browser Extensions API?
链接地址: http://www.djcxy.com/p/45750.html下一篇: 如何从内容脚本发送消息到面板?