How to make Chrome Extension run for each new Iframe added later in dom?
2,003 views
Skip to first unread message
Abhijit Dhobale
unread,
Nov 10, 2016, 1:02:08 AM11/10/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Chromium-Extensions-Announce
Hello All,
I am injecting third party libraries and content script from background js. I used chrome.tabs.executeScript with property allFrames: true. So that my code is injected in each iframe which is present in DOM. But I am facing problem with dynamically added iframe i.e. in some sites there is image tag and clicking on it iframe loaded (appended) in DOM in which my plugin code doesn't exists. So is there any way to inject script in dynamically appended iframe. Thanks in advance for your help.
Thanks, Abhijit.
wOxxOm
unread,
Nov 10, 2016, 9:17:29 AM11/10/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Chromium-Extensions-Announce
Inject a script with MutationObserver that looks for iframes in the main frame.
Abhijit Dhobale
unread,
Nov 10, 2016, 11:31:32 PM11/10/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Chromium-Extensions-Announce
On Thursday, November 10, 2016 at 7:47:29 PM UTC+5:30, wOxxOm wrote:
Inject a script with MutationObserver that looks for iframes in the main frame.
Hello wOxxOm,
Thanks for your suggestion. I have one more question regarding with your solution. If I use MutationObserver I get event that node i.e. iframe appended in dom. But how can I inject my code in that iframe only cause code injection is based on tab id. If I tried to inject code on the basis of tab id it gives me multiple time injection error because in that page code injection is already done and I want to inject my code in iframe only.
Thanks, Abhijit
wOxxOm
unread,
Nov 10, 2016, 11:54:28 PM11/10/16
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Chromium-Extensions-Announce
executeScript has frameId parameter, so when your MutationObserver function discovers new iframe you can send a message to your background page that will inject the script.
To list all frames' frameId you can use chrome.webNavigation.getAllFrames. You'll need to maintain a list of frameId that were already processed per each opened tab by making your injected scripts report back to your background page immediately after injection so that you can use sender.tab and sender.frameId in chrome.runtime.onMessage listener to store these frameIds per tabId e.g. processed[sender.tab.id][sender.frameId] = true. Of course you'll have to clear this variable on navigation using chrome.tabs.onUpdated or chrome.webNavigation.onCommitted.
On Friday, November 11, 2016 at 7:31:32 AM UTC+3, Abhijit Dhobale wrote:
On Thursday, November 10, 2016 at 7:47:29 PM UTC+5:30, wOxxOm wrote:
Inject a script with MutationObserver that looks for iframes in the main frame.