bigbobr
bigbobr

Reputation: 355

Adding content script by simple-prefs

I've got a content script that should be added on page if I select this preference in addon settings. I can add bool setting, but I absolutely can't understand how to operate with it on main.js file. The setting is

  "preferences": [{
        "description": "",
        "name": "tagHide",
        "type": "bool",
        "value": false,
        "title": "Hide something"}]

And in the main file I added this

var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
var prefs = require("sdk/simple-prefs").prefs;
function onPrefChange(prefName) {
    if (prefs.tagHide) {
        console.log(prefs.tagHide);
        pageMod.PageMod({
            include: "*.corbina.net",
            contentScriptFile: data.url("cutter.js")
        });
    };
}

This code logs "true" of "false" in the console, but pagemod seems not working. Any errors I've got here?

Upvotes: 0

Views: 107

Answers (1)

therealjeffg
therealjeffg

Reputation: 5830

There are a few issues with this code as-is:

  • you don't bind onPrefChange anywhere in this code, eg require("sdk/simple-prefs").on("", onPrefChange);, see the docs
  • you don't apply the page-mod to existing tabs, see the docs here specifically the attachTo option.
  • even if your page-mod attached, there is no way I can see that you unload the content script's effects?

Upvotes: 2

Related Questions