简体   繁体   中英

Firefox Add-on SDK Attach Content Script Once

var self = require("sdk/self");
var tabs = require("sdk/tabs");

var button = require("sdk/ui/button/action").ActionButton({
  icon: "./icon16.png", 
  label: "Test Button",
  onClick: function() {   
    tabs.activeTab.attach({
        contentScriptFile: ['./content.js']
    }); 
  }
});

I am attaching a content script file to the active tab by a toolbar button click.

How can I know if the content script is already attached to the activeTab or not? I do not want to add multiple content scripts to the tab. If there is any attached content script, I need to remove it.

Toggle button does not help me. Button does work tab specific way.

.attach returns a worker. Put it in a Map with the tab object as key. If there already is an entry present for that tab, don't attach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM