本文介绍了Firefox Addon - 从ActionButton访问预加载的内容脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果用户点击ActionButton(浏览器右上角的图标),我正在尝试访问内容脚本处理函数,但它不起作用。



我使用,但仍然无法正常工作。



我做错了什么?



请看下面的代码TODO标记的区域不工作;

  // main.js 
tabs.on('ready',function(tab){
var worker = tab.attach({
include:*,
// contentScript:'window.alert(Page match ruleset);',
contentStyleFile:[data.url(scripts / myTestContent1.js)]
});
worker.port.on('listener1',function( params){
//我会听一些来自myTestContent1.js
});
console.log(main.js:tab is ready!);
});



//浏览器图标通常在右上角
var button = buttons.ActionButton({
id:myActionButton,
标签:我漂亮的插件,
图标:{
16:./icon-16.png,
32:./icon-32。 png,
64:./icon-64.png
},
onClick:handleClick
});

//点击右上角的浏览器图标按钮
function handleClick(state){
var myWorker = tabs.activeTab.attach({
// contentScriptFile: //我不想附加任何东西,只需要激活tab!
});
// TODO这个代码不是由活动标签的内容脚本处理
//应该由myTestContent1.js处理,但不起作用
myWorker.port.emit(initialize,Message从附加);

myWorker.port.emit 不是调用处理函数函数在我的内容脚本。

  // myTestContent1.js 
// TODO这个代码不会调用:(
self.port.on(initialize,function(){
alert('self.port.on(initialize)');

});


解决方案



  var TabWorkerPair = function(tabid,worker){
this.tabid = tabid;
this。 (var i = 0; i< TabWorkerPairList.length; i ++);
$;
$ b $;

tabs.on('close',function(tab){
){
var pair = TabWorkerPairList [i];
if(pair.tabid == tab.id){
//从列表中删除对象
TabWorkerPairList.splice(i, 1);
break;
}
}
console.log(main.js> tabs.on('close )> TabWorkerPairList计数:+ TabWorkerPairList.length);
});

$ b tabs.on('ready',function(tab){
var worker = tab.attach({
include:*,
contentScriptFile:[data.url(scripts / myTestContent1.js)]
});
//队列工作人员tab
var pair = new TabWorkerPair(tab.id,worker) ;
TabWorkerPairList.push(pair);
console.log(main.js:tab is ready!);
});

最后现在我可以发出辅助函数了;

  //点击右上角的浏览器图标按钮
function handleClick(state){
for(var i = 0; i< TabWorkerPairList.length; i ++){
var pair = TabWorkerPairList [i];
if(pair.tabid == tabs.activeTab.id){
pair.worker.port.emit(initialize,pair.tabid);
break;





$ b $主要原因:标签可以有多个工作人员。因此,您应该手动访问您感兴趣的工作人员。


I am attaching content script to every tab on my firefox add-on.

And if user clicks on ActionButton (top right icon on browser) I am trying to access content script handler function, but it does not work.

I am using walkthroughs from Content Scripts but still could not make it work.

Am I doing something wrong?

Please see code below the TODO marked areas are not working;

// main.js    
tabs.on('ready', function (tab) {
    var worker = tab.attach({
        include: "*",
        //contentScript: 'window.alert("Page matches ruleset");',
        contentStyleFile: [data.url("scripts/myTestContent1.js")]
    });
    worker.port.on('listener1', function (params) {
        // I will listen some messages coming from myTestContent1.js
    });   
    console.log("main.js: tab is ready!");
});



// browser icon typically at top right
var button = buttons.ActionButton({
    id: "myActionButton",
    label: "My pretty add-on",
    icon: {
        "16": "./icon-16.png",
        "32": "./icon-32.png",
        "64": "./icon-64.png"
    },
    onClick: handleClick
});

// when top right browser icon button is clicked
function handleClick(state) {
    var myWorker = tabs.activeTab.attach({
        //contentScriptFile: // I do not want to attach anything, just get Active tab!
    });
// TODO this code is not handled by active tab's content script
// should be handled by myTestContent1.js but does not work
    myWorker.port.emit("initialize", "Message from the add-on");
}

myWorker.port.emit is not calling handler function on my content script.

// myTestContent1.js    
// TODO this code is not calling :(
self.port.on("initialize", function () {
    alert('self.port.on("initialize")');

});
解决方案

I fixed the problem by keep tracking tab-worker pairs, so

var TabWorkerPair = function (tabid, worker) {
    this.tabid = tabid;
    this.worker = worker;
};

tabs.on('close', function (tab) {
    for (var i = 0; i < TabWorkerPairList.length; i++) {
        var pair = TabWorkerPairList[i];
        if (pair.tabid == tab.id) {
            // remove object from list
            TabWorkerPairList.splice(i, 1);
            break;
        }
    }
    console.log("main.js > tabs.on('close') > TabWorkerPairList count: " + TabWorkerPairList.length);
});


tabs.on('ready', function (tab) {
    var worker = tab.attach({
        include: "*",     
        contentScriptFile: [data.url("scripts/myTestContent1.js")]
    });
    // queue workers for tab
    var pair = new TabWorkerPair(tab.id, worker);
    TabWorkerPairList.push(pair);    
    console.log("main.js: tab is ready!");
});

and finally I can now emit worker function;

// when top right browser icon button is clicked
function handleClick(state) {    
    for (var i = 0; i < TabWorkerPairList.length; i++) {
        var pair = TabWorkerPairList[i];
        if (pair.tabid == tabs.activeTab.id) {
            pair.worker.port.emit("initialize", pair.tabid);
            break;
        }
    }

}

main reason: a tab can have multiple workers. So you should manually access the worker you are interested in.

这篇关于Firefox Addon - 从ActionButton访问预加载的内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 01:33