Firefox插件引用错误



我试图在加载的HTML上嵌入一个div,但它在控制台中给了我以下错误

Message: ReferenceError: contentScriptValue is not defined Stack: @javascript:if (document.body) {contentScript:contentScriptValue}:1:35

下面是我的代码

var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs").on("ready", runScript);
var pageMod = require("sdk/page-mod");
var contentScriptValue = 'document.body.innerHTML = ' + ' "<h1>Page matches ruleset</h1>";';
var button = buttons.ActionButton({
    id: "mozilla-link",
    label: "Visit Test Site",
    icon: {
        "16": "./icon-16.png",
        "32": "./icon-32.png",
        "64": "./icon-64.png"
    },
    onClick: handleClick,
});
pageMod.pageMod({
    include: "*.mozilla.org",
    contentScript: contentScriptValue
});
function handleClick(state) {
    tabs.open("http://my-website.com/");
}
function runScript(tab) {
    tab.attach({
        contentScript: "if (document.body) {contentScript:contentScriptValue}"
    });
}

contentScripts在不同的作用域中运行。所以你不能在你的插件中引用东西。

你必须这样做:

function runScript(tab) {
    tab.attach({
        contentScript: "var contentScriptValue = 'rawr'; if (document.body) {contentScript:contentScriptValue}"
    });
}

最新更新