开发者

javascript google chrome extension iframe cannot examine contentWindow

I am writing a Google Chrome extension. Now I need to examine the contents of an iframe but the content script seems unable to access this content even though the debugger can. The iframe contents are a list of messages I have previously sent to that site. If I put the following statement in the content script, it always returns null:

document.getElementById("td_messages_show").getElementsByTagName("iframe")[0].contentWindow.document;

But if I open the debugger and execute the same command from the command line, it returns "Document" with the appropriate contents. At first I thought it was because the frame wasn't finished loading so I found a snippet like this and tried to use it.

function wait4Iframe2Load() {
   // Get a handle to the iframe element
   //console.log('Checking for null myFrame');
    var myFrame = document.getElementById("td_messages_show").getElementsByTagName("iframe")[0].contentWindow;
    if (myFrame!=null) 
    {
        console.log(myFrame);
       // Check if loading is complete
       if ( myFrame.document.readyState == 'complete' ) {
          // The loading is complete, call the function we want executed once the iframe is lo开发者_运维问答aded
          console.log('Loading Complete');
          //frameContent=document.getElementById("td_messages_show").getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName('tbody')[0];
          return;
       }
       else alert(" Frame is Not Loaded");
    }
    else myFrame = document.getElementById("td_messages_show").getElementsByTagName("iframe")[0].contentWindow;
   // If we are here, it is not loaded. Set things up so we check the status again in 100 milliseconds
   console.log('Waiting for frame to load...');
   window.setTimeout('wait4Iframe2Load()', 100);      
}

This simply returns null forever. But while this script is piling up console messages, I can open the debugger and execute the very same command line and it returns a document. Faced with this problem and researching internet answers, it seems it may be some deliberate kind of security issue. Whether it is or isn't, I need to examine the iframe contents and determine what I have written there previously so I can decide what to write there next.

Does anybody have an idea how to solve this problem?


The idea is to inject content script into this iframe and use it to get required information.

As I understand this frame has a specific url known upfront, so you can just inject the script through the manifest (use the all_frames option).

If for some reason you need to dynamically inject it, then there is:

chrome.tabs.executeScript(tabId, {allFrames: true});

It will be injected into both parent and iframe pages. Then inside injected content script you can check whether or not it is running inside the right iframe. For example your dynamically injected content script might look like this (if you inject it though the manifest url checking won't be needed):

if(window.location.href == "http://iframe.example.com/" && window != window.top) {
    //we are in the right page that is embedded as iframe, do stuff
} else {
    //do nothing
}


Excuse me, have you got solution, Jerome? I am having same problem as yours. But I can not make comment on your post. So please do not mind because this is a question...

Edited:

Finally I got it working. I don't understand while flag all_frames: true in manifest.json is not affected. I need to code as @serg's:

chrome.tabs.executeScript(tabId, {allFrames:true, file:"content_script.js"})

Thank you all, and could you please accept @serg's answer as the right one, Jerome? :-)


I created this library:

https://github.com/attachmentsme/Queuebert

to simplify the communication between browser tabs, the extension's background process, and iframes.

The use-case that I was running into was:

  • an action is taken in one iframe.
  • the results should be displayed in an alternate iframe.

Doing this can be a pain, hence the abstraction I built.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜