Reputation: 3488
I am trying to embed a div on top of the loaded HTML
but it gives me the following error in console
Message: ReferenceError: contentScriptValue is not defined
Stack:
@javascript:if (document.body) {contentScript:contentScriptValue}:1:35
Below is my code
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}"
});
}
Upvotes: 1
Views: 48
Reputation: 37228
contentScripts run in a different scope. So you can't reference stuff in your addon.
You would have to do it like this:
function runScript(tab) {
tab.attach({
contentScript: "var contentScriptValue = 'rawr'; if (document.body) {contentScript:contentScriptValue}"
});
}
Upvotes: 1