Firefox插件参考错误

时间:2015-02-12 23:54:39

标签: javascript firefox firefox-addon firefox-addon-sdk

我正在尝试在加载的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}"
    });
}

1 个答案:

答案 0 :(得分:1)

contentScripts在不同的范围内运行。因此,您无法在插件中引用内容。

你必须这样做:

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