7

I am able to add HTML/CSS dynamically into the page using a content script. But I tried adding an iframe tag, and ran into a little bit of trouble.

Here is my code:

  const myIFrame = `
    <iframe src="${modalIFrameURL}"></iframe>
  `;
  let div = document.createElement('div');
  div.style.zIndex = 9999999;
  div.innerHTML = myIFrame;
  document.body.insertBefore(div, document.body.firstChild);

note the modalIFrameURL value is:

chrome-extension://omelijcoklpokoeobkpepoipjpbakoeo/modal-iframe.html

This is what I get at the top left of the page:

enter image description here

If I hover over the gray fail box, it says:

Requests to the server have been blocked by an extension.

Does anyone know how to load an iframe from a content-script? What I am looking to do is load an HTML file from my Chrome Extension codebase into that iframe.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • If you make a new profile and only install the extension you're testing, does the issue still occur? – Wesley Smith Feb 04 '18 at 05:21
  • Right so you think another extension is blocking the iframe? If that's the case, then my iframe solution is not gonna work, simply because other users will likely face the same problem I am facing. Other users will have extensions that block iframes or whatever...right? – Alexander Mills Feb 04 '18 at 05:53
  • yeah even with a new profile and just my extension loaded, I get the same error: `Requests to the server have been blocked by an extension.` – Alexander Mills Feb 04 '18 at 05:58
  • 1
    Possibly given the content of the message. I would start a new profile and add one extension at a time till it happens again to narrow down the extension causing the issue. Then, see why that extension is causing the issue. – Wesley Smith Feb 04 '18 at 05:58
  • I just commented - yeah if this error will always be caused by using iframes, I guess it won't be a good solution - I can't trouble my users to disabled extensions to use mine, etc. And like I said, even if my extension is the only one installed, the error still seems to happen. – Alexander Mills Feb 04 '18 at 05:59
  • Gotcha, then that means its likely a configuration issue of some sort. If done iframes in extensions before but its been a while, trying to dig up that code now – Wesley Smith Feb 04 '18 at 06:03
  • Actually, have a look at https://stackoverflow.com/a/24649134 – Wesley Smith Feb 04 '18 at 06:11
  • nice I actually think that works, just tried it. if you want to add answer, that will get at least an upvote. – Alexander Mills Feb 04 '18 at 06:26
  • I don't know that much about iframes but one thing is that I can't seem to click on surround text near the iframe, maybe it's somehow blocking the rest of the page, can't tell – Alexander Mills Feb 04 '18 at 06:27
  • 1
    Thats possible depending on the styling of the iframe. Right click on the edge of the frame and click "inspect". That'll open the dom inspector in the dev tools console. Hover over the iframe node in the console and you should see clearly all the space the frame takes up. Then you can play with the css for the frame in the right had CSS panel till you get it to where it doesnt interfere with the page. – Wesley Smith Feb 04 '18 at 19:46

1 Answers1

15

I ran into this a few weeks ago. I found that adding a "web_accessible_resources" section to my manifest.json file resolved it.

From what you are calling out, it should look like:

"web_accessible_resources": [
    "modal-iframe.html"
],

https://developer.chrome.com/extensions/manifest/web_accessible_resources

Just make sure that you are including "modal-iframe.html" in your extension build directory.

Hope that helps.

Sarah Dasko
  • 181
  • 4