inject JS code inside a frame that is inside an iframe

2,140 views
Skip to first unread message

lsri8088

unread,
Oct 29, 2015, 8:06:00 AM10/29/15
to Chromium-extensions
Hello,

I want to inject JavaScript code with a chrome extension, within a frame that is inside an iframe and that inside another iframe.

Page
  |-- iframe 1
         |-- iframe2
                |-- frame (no iframe), where I want to inject code

This is possible? how?

Thanks!

lsri8088

unread,
Oct 29, 2015, 9:50:07 AM10/29/15
to Chromium-extensions

In manifest.json file:
[...]
"content_scripts": [
 
{
 
"matches": ["https://myurl/*", "https://myurl/*/*"],
 
"js": ["content_script.js" ],
 
"all_frames": true
 
}
   
],
 
"web_accessible_resources": ["script.js"]
[...]


In scrtips.js file: the code you want to inject

In content_script.js file:

var s = document.createElement('script');
s
.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s
.onload = function() {
   
var s2 = document.createElement('script');
 s2
.src = chrome.extension.getURL('script.js');
 
(document.head||document.documentElement).appendChild(s2);
 s2
.onload = function() {
 
var s3 = document.createElement('script');
 s3
.src = chrome.extension.getURL('script.js');
 
(document.head||document.documentElement).appendChild(s3);
 
};
};


:)

jagdish achara

unread,
Oct 29, 2015, 12:10:55 PM10/29/15
to lsri8088, Chromium-extensions
Hi,

I'm using the same but I notice that js is not loaded in all iFrames...do you notice the same? I don't know if it's a bug or what...

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/e400e463-ef5f-442a-b268-0502cb692ab0%40chromium.org.

lsri8088

unread,
Oct 29, 2015, 12:14:37 PM10/29/15
to Chromium-extensions, lsri...@gmail.com
By testing, I have seen that it is not necessary to inject the code several times. Just do it once. But if you do so, it works, inserted in all frames:
Reply all
Reply to author
Forward
0 new messages