content script load into dynamically added IFrame

1,241 views
Skip to first unread message

Nitin Sharma

unread,
Feb 12, 2019, 3:56:17 AM2/12/19
to Chromium Extensions
I m trying to inject my custom script into first place in the head section using a content script.
It is able to inject my custom script into the main window document, and all other frames which have been loaded into DOM initially.

But whenever any new IFrame has been added into DOM, how should I add the custom script into the newly added Iframe.
For now what I m planning is to put the mutant observer into a content script which checks if any new IFrame has been added. if yes inject the custom script into Head of Iframe document.

Is there any better approach where the content script do this task by changing some configuration? 

my current configuration for a content script is

"content_scripts": [
{
"js": [ "main.js" ],
"matches": [ "<all_urls>" ],
"all_frames": true,
"run_at": "document_start"
}
],

Message has been deleted

Nitin Sharma

unread,
Feb 12, 2019, 5:14:27 AM2/12/19
to Chromium Extensions
Just identified, if the Iframe src is different then actual src then it's not able to load content scripts into it,
for example: if my main window is 127.0.0.1:8080 and iframe src is some local file like index.html, in this case, it is working fine,

if we provide some other URL like http://www.example.com then it's failing.
All though strange is if we are adding Iframe src as www.example.com then it's working.

Also not able to inject script from parent window to iframe by keeping mutant observer as mention in post 1 because of CROS issue
Please help how to solve it without changing anything on the website.

wOxxOm

unread,
Feb 12, 2019, 9:28:31 AM2/12/19
to Chromium Extensions
If you add "match_about_blank": true in the declaration of the content script, Chrome will inject the content scripts you've specified automatically into the dynamic iframes, no need to do it manually. The injected content script will have full access to its iframe's DOM directly via document object so you'll be able to insert a script into document.documentElement or document.head (provided the head was already added into DOM).
Reply all
Reply to author
Forward
0 new messages