In my Chrome extension, I embed the contents of the script in all the IFRAMEs inside the page. Here is part of the manifest.json file:
"content_scripts": [ { "run_at": "document_end", "all_frames" : true, "match_about_blank": true, "matches": ["http://*/*", "https://*/*"], "js": ["content.js"] } ],
Thus, a single webpage containing multiple IFRAMEs will end up launching many copies of my embedded content.js .
The logic inside content.js collects data from each IFRAME that it enters, or from the main / top page, and sends it back to the background script (using chrome.runtime.sendMessage .) The background script, in turn, needs to store data in a global variable , which will later be used in the extension itself.
The problem I am facing is that the application must distinguish between โdataโ received from several IFRAMEs , since my data collection method can be called repeatedly when the user interacts with the page, and therefore I canโt just โdumpโ the data. received background script into an array. Instead, I need to use the dictionary data store.
I can tell if the data comes from IFRAME or from the top page by running the following:
//From the `content.js` var isIframe = window != window.top;
and I thought that if I collect the URLs of the pages of each IFRAME , then I will have to use it as a unique key to store data in my global dictionary-type variable:
//Again from content.js var strUniqueIFrameURL = document.URL;
Well, this will not work, because two or more IFRAMEs can have the same URLs.
So, thus, my original question is how to tell IFRAMEs on a page separately? Is there some kind of unique identifier or something like what Chrome assigns them?