Firefox addon 如何跟踪消息来自哪个选项卡或内容脚本?
在Firefox插件中,我需要跟踪与哪些选项卡消息关联。内容脚本将数据发送到Firefox addon 如何跟踪消息来自哪个选项卡或内容脚本?,firefox-addon,mozilla,content-script,Firefox Addon,Mozilla,Content Script,在Firefox插件中,我需要跟踪与哪些选项卡消息关联。内容脚本将数据发送到main.js。稍后,当用户单击工具栏中的扩展按钮时,它将查找与活动选项卡关联的数据 在Chrome extensions中,当收到消息时,我可以询问消息来自哪个选项卡,并通过选项卡id跟踪消息。在Firefox中,也有id,但似乎没有一种简单的方法可以从内容脚本访问它们。答案取决于您创建内容脚本的方式。下面是一个示例main.js文件,用于使用添加内容脚本 我还建议你阅读和阅读,以便更好地了解正在发生的事情以及如何使之
main.js
。稍后,当用户单击工具栏中的扩展按钮时,它将查找与活动选项卡关联的数据
在Chrome extensions中,当收到消息时,我可以询问消息来自哪个选项卡,并通过选项卡id跟踪消息。在Firefox中,也有id,但似乎没有一种简单的方法可以从内容脚本访问它们。答案取决于您创建内容脚本的方式。下面是一个示例
main.js
文件,用于使用添加内容脚本
我还建议你阅读和阅读,以便更好地了解正在发生的事情以及如何使之适应你的情况
var buttons = require('sdk/ui/button/action'),
pageMod = require('sdk/page-mod'),
data = require('sdk/self').data;
// Map of messages keyed by tab id
var messages = {};
pageMod.PageMod({
include: 'http://www.example.com',
contentScriptFile: [
data.url('my-script.js')
],
onAttach: function(worker){
// Get the tab id from the worker
var tabId = worker.tab.id;
// Save the message
worker.port.on('message', function(message){
messages[tabId] = message;
});
// Delete the messages when the tab is closed
// to prevent a memory leak
worker.on('detach', function(){
delete messages[tabId];
});
}
});
var button = buttons.ActionButton({
id: 'my-extension',
label: 'Example',
icon: {
'16': './icon-16.png',
'32': './icon-32.png',
'64': './icon-64.png'
},
onClick: function(state){
// Retrieve the message associated with the
// currently active tab, if there is one
var message = messages[tabs.activeTab.id];
// Do something with the message
}
});