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

07-26 07:00