在Firefox插件中,我需要跟踪与哪些标签消息相关联。内容脚本将数据发送到main.js
。稍后,当用户点击工具栏中的扩展程序按钮时,它将查找与活动标签相关联的数据。
在Chrome扩展程序中,收到邮件后,我可以询问邮件来自哪个标签,并按标签ID跟踪邮件。在Firefox中,tabs也有ID,但似乎并不是从内容脚本访问它们的简单方法。
答案 0 :(得分:2)
答案取决于您创建内容脚本的方式。以下是使用PageMod添加内容脚本的示例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
}
});
我还建议您阅读Content Scripts - Interacting with Page Scripts和Content Worker,以便更好地了解正在发生的事情以及如何使其适应您的情况。