[英]firefox jpm - emit to tab from outside the tab-call
In this documentation there is an example of how to emit data/events to a tab. 在本文档中,有一个如何向选项卡发送数据/事件的示例。 Here is my edited version:
这是我的编辑版本:
success-window.js 成功-window.js
// "self" is a global object in content scripts
// Listen for a "drawBorder"
self.port.on("drawBorder", function(color) {
document.body.style.border = "5px solid " + color;
});
In index.js 在index.js中
var self = require("sdk/self");
var tabs = require("sdk/tabs");
var success_tab;
var myTab = tabs.open({
url: data.url("success-window.html"),
onReady: function onReady(tab) {
success_tab = tab.attach({
contentScriptFile: [data.url('angular/angular.min.js'),
data.url('angular-bootstrap/ui-bootstrap-tpls.min.js'),
data.url('success-window.js')]
});
}
});
How can i use success_tab.port.emit
outside the button-definition? 如何在按钮定义之外使用
success_tab.port.emit
? Like in another function within index.js eg console.log
就像在index.js中的另一个函数中一样,例如
console.log
console.log('mytab:', myTab); // mytab: undefined
console.log('success_tab:', success_tab); // success_tab: undefined
success_tab.port.emit("drawBorder", "red"); // JPM [error] Message: TypeError: success_tab is undefined
Try this. 尝试这个。 Don't forget to adjust
include
to match whatever your success html page is named like: 不要忘记调整
include
来匹配您成功的html页面,例如:
var self = require("sdk/self");
var tabs = require("sdk/tabs");
var mods = require("sdk/page-mod");
var successWorker;
mods.PageMod({
include: "*", //adjust this to match ONLY your success page
contentScriptWhen:'start',
contentScriptFile: [data.url('angular/angular.min.js'),
data.url('angular-bootstrap/ui-bootstrap-tpls.min.js'),
data.url('success-window.js')],
onAttach: function(worker){
successWorker= worker;
worker.on("message", function(aData){
//messages from success-window.js
});
worker.on("detach", function(){
//prevent worker from being used after it's been disposed
successWorker= undefined;
});
}
});
function postToSuccessWindow(message){
if(successWorker){
successWorker.postMessage(message);
}
}
Also, a call to tabs.open
doesn't return a tab. 另外,对
tabs.open
的调用不会返回标签。 To reference that tab you have to do it like this: 要引用该选项卡,您必须这样做:
var successTab;
tabs.open({
url: "success-window.html"
onOpen: function(tab){
successTab = tab;
}
});
Did you try setting it to a global like this: 您是否尝试过将其设置为像这样的全局变量:
var self = require("sdk/self");
var tabs = require("sdk/tabs");
var worker;
var button = require("sdk/ui/button/action").ActionButton({
id: "style-tab",
label: "Style Tab",
icon: "./icon-16.png",
onClick: function() {
worker = tabs.activeTab.attach({
contentScriptFile: self.data.url("my-script.js")
});
worker.port.emit("drawBorder", "red");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.