How to set javascript variable in Firefox Add On created using Add On SDK?

I am trying to set a javascript variable to determine if the Firefox extension is installed. The idea is to read this variable on the page.

Failed to implement this using complex approaches when using XUL add-ons ( Install an object in a page window object from the Firefox extension? ) And find out about contentScripts in the Firefox Add On SDK that looks appropriate for the task, I still have problems accessing variables javascript installed in ContentScript on the page.

I have the following in my main.js (using the sample provided here https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/page-mod.html ):

var pageMod = require("sdk/page-mod");

pageMod.PageMod({
  include: "*",
  contentScript: 'window.isInstalled=true;window.alert("Page matches ruleset");',
  contentScriptWhen: "ready"
});

However, when I try to get the variable window.isInstalledin javascript on the page, it is undefined, despite the warning being shown.

How to make sure that the value written by the script content will be available on the page?

+4
source share
1 answer

window XRayWrapper, , , -. , , , - , window.

unsafeWindow.isInstalled = ... ( .isInstalled , , , -).

docs XRayWrapper unsafeWindow.

, @canuckistani:

unsafeWindow , ( script). Spidermonkey . Spidermonkey Gecko. ( , Spidermonkey. IIRC , . ).

, , -. unsafeWindow pwnd. , , .

. , , :

for (var el of window.document.querySelectorAll("*[onclick]")) {
  el.addEventListener("click", el.getAttribute("onclick"));
  el.removeAttribute("onclick");
}

( ) - (.getAttribute) script, -.

, unsafeWindow :

for (var el of unsafeWindow.document.querySelectorAll("p")) {
  el.addEventListener("click", 'alert("I am ' + el.clientHeight + 'px tall");');
  el.removeAttribute("onclick");
}

XRayWrapper wrapped window , document , document.querySelectorAll , , el.clientHeight .

unsafeWindow, . , - - :

document.querySelectorAll = function() {
  return [{
    clientHeight: 'a pwnd content script"); doSomethingEvil(); alert("Now I own you! And I am certainly not 0'
  }];
};

, - unsafeWindow.document.querySelectorAll, ( ) -, .

script , , . . .

Object.defineProperty(document, "title", {
  get: function() { while(true); }
});
// or
Object.defineProperty(document, "title", {
  get: function() { throw new Error("get off my lawn!"); }
});
+6

All Articles