Easton L.
Easton L.

Reputation: 3044

Firefox add-on: inject content script to iframe with src as "about:blank"

We are developing an add-in, and would like to have the content script being injected into the top page and all frames. However we found that the content scripts cannot be injected into iframe with src as "about:blank" or "javascript:...".

Any comment/suggestion?

sample page-mod:

require("sdk/page-mod").PageMod({
            include:["*", "file://*"],
            contentScriptFile: [
                "./content/content.js",
            ],
            attachTo: ["existing","top", "frame"],...

Upvotes: 6

Views: 1405

Answers (3)

Walty Yeung
Walty Yeung

Reputation: 3562

There is an option of match_about_blank in the content script of firefox now:

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/content_scripts#match_about_blank

Upvotes: 1

Easton L.
Easton L.

Reputation: 3044

I want to update an interesting fact that detected by my teammate Wayland:

The funny thing is that it will work if we build the iframe as following:

It's like if we flush by iframe content by calling 'document.write'.

function buildme() {

  var iframe = ...;
  iframe.contentDocument.open();
  iframe.contentDocument.write("<html><body></body></html>");
  iframe.contentDocument.close()

  var child = iframe.contentDocument.craeteElement("..");
  ...
  iframe.contentDocument.body.appendChild(child);
}

< iframe id = "myframe"
onload = "buildme();" / >

Upvotes: 1

erikvold
erikvold

Reputation: 16538

Try this:

require("sdk/page-mod").PageMod({
        include:["*", "file://*", "about:blank", "javascript:*"],
        contentScriptFile: [
            "./content/content.js",
        ],
        attachTo: [[b]"existing","top", "frame"[/b]],...

Upvotes: 0

Related Questions