Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject javascript in an iframe using chrome extension

There are lots of questions/answers on injecting javascript into the document page. What I really want to do is inject javascript in an iframe within the document.

To be clear, that iframe doesn't belong to the same domain as the document. I've already tried it through console (not through extension) but was unable to do so.

I'm very curious if this is something that can be done using a chrome-extension ?

like image 261
coding_idiot Avatar asked Oct 10 '13 06:10

coding_idiot


People also ask

Can you use JavaScript in an iframe?

As long as the protocol, domain and port of the parent page and iframe match, everything will work fine. I added an example on how to use the window.

Can we use iframe in Chrome extension?

No, the content scripts will NOT execute in the iframes loaded dynamically via JavaScript in the page.

What is Chrome extension inject JS?

JS Inject. Create, save, and inject javascript code into web pages. This extension allows you to write, save, and execute javascript into web pages. You can save scripts to be run globally (on any page), on a specific domain, on a specific page, or via a "URL contains" string.

How do I find iframes in Google Chrome?

Right-clicking on the frame should show you the option "View frame source". By clicking on it, it will open the source code in a new tab.


1 Answers

Yes, you can use content_scripts property in manifest.json like this:

"content_scripts": [{     "matches": ["http://*/*", "https://*/*"],     "js": ["content_frame.js"],     "all_frames": true }], 

By setting all_frames to true you will inject/include content_frame.js javascript file into top document and all iframes. In case you just need javascript injected in iframe but not in top document you can check inside your content_frame.js file like this:

if (parent === top) {     // here you can put your code that will run only inside iframe } 
like image 128
Igor Jerosimić Avatar answered Sep 21 '22 09:09

Igor Jerosimić