目录
Is it okay to insert an <iframe> into the page's content?
iframegoogle-chrome-extensioncontent-script
浏览量:59
编辑于:2023-04-12 22:10:00

Using a content script, I plan to insert a UI into the page in an , but I have a few concerns.<iframe>

  • Do people commonly use settings/extensions to block s on the page?iframe
  • If my extension's is blocked, is there a way for me to detect this?iframe
  • Any other reasons to avoid the use of s in this scenario?iframe

There are similar questions on the site, but they don't specifically ask what I want to know.

[The reason I want to use an is this: My extension has to run independently of the website loaded (i.e. on any webpage). Its content script needs to show a UI for settings/help etc. Currently it does so by inserting a div into the page's DOM. However CSS from the webpage gets applied to the extension's UI, something which is harder to fix than one would think. Using an seems to a way to avoid this issue.]iframe``<iframe>

解决方案:

As you've discovered, trying to sandbox your CSS from interference, when injecting into the DOM is very difficult without declaratively setting every known style attribute. It's a well documented problem and some solutions can be found in the following posts:

There's nothing fundamentally wrong with dropping your UI it into an . I'm not aware of any settings or extension that would block this behaviour.<iframe>

If your framed UI needs to interact with the DOM on the parent page or a content script you can do so with the use of Window.Parent. Also you may need to consider Same-Origin Policy if your pulling in the UI from another domain.