Reputation: 21277
I have injected a content script file to all pages and inner iframes using "all_frames": true
but when I send a request from popup only top window is receiving it. Any idea how can send it to all inner frames or at least last focused window/document?
Popup.html (page action):
===========
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendRequest(tab.id, {...}, function(response) {...});
});
content_script.js
=================
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
sendResponse({...});
});
I have checked that content script is working in inner frames.
I'm trying to retrieve text selection which may be in inner frames.
(By the way I prefer not to open connection by every and all content script, that's a big overhead.)
Upvotes: 2
Views: 1681
Reputation: 111335
The problem is that one tab can have only one onRequest
listener. So if you want to send messages to only iframe script, and there is only one iframe on the page, then you should create a listener inside iframe only:
//listen to requests only inside iframe
if(window!=window.top) {
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
...
});
}
Now if you want to send requests to both parent page and iframe, or there are many iframes on the page that need to listen to requests - you need to communicate through ports.
Ports still have a similar limitation - only one port listener will be called, but you can open many ports. If there is a parent page with only one iframe - you just open two connections on two different ports. But if there are many iframes on the page, and each iframe has subframes - you would need to enumerate all iframes first to create unique ports for each of them.
PS. I am not sure what you meant by
I prefer not to open connection by every and all content script
but if you want to inject content script on demand, there is chrome.tabs.executeScript()
.
Upvotes: 3