Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension Inject into iFrame with JavaScript

I'm working on a Chrome Extension that allows a user to open a site into an iframe in a new tab. My goal is to allow them to modify the CSS by injecting the code via JavaScript from the parent window, yet I'm getting blocked by origin-only issues.

Is there a way around this? I know I've seen other extensions do similar things.

like image 829
Fluidbyte Avatar asked Aug 19 '12 16:08

Fluidbyte


People also ask

Can you use JavaScript in an iframe?

It is called an inline frame because to the user it is all one web page. The child iframe is a complete browsing environment within the parent frame. It can load its own JavaScript and CSS separate from the parent. They can also be refreshed and loaded asynchronously from the parent site.

Can we use iframe in Chrome extension?

If you have a web application already, and would like to build a chrome extension for it - using iframes is an amazing tool. You can just inject an iframe of that web app in any page instead of re-implementing the whole UI from ground up.

Does Google Chrome support iframes?

Google Chrome now supports lazy loading iframes for speeding up page loads.

How do I get iframe for Chrome?

How to identify the iframe: Right click on the element, If you find the option like 'This Frame' then it is an iframe. Right click on the page and click 'View Page Source' and Search with the 'iframe', if you can find any tag name with the 'iframe' then it is meaning to say the page consisting an iframe.


1 Answers

I believe what you are looking for is the

chrome.tabs.executeScript

with allFrames set to true

Documentation can be found here https://developer.chrome.com/extensions/tabs#method-executeScript

This will allow you to inject a content script into a page and all iFrames in that page. There is also a similar function for injecting CSS : )

like image 160
Mike Grace Avatar answered Sep 24 '22 14:09

Mike Grace