简体   繁体   中英

Load iframe into the page, using Chrome Extension content script

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:

在此输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM