title: Content script sees wrong Document element
body: <p>I am currently working on a Chrome extension that tries to get the highlighted text from the page displayed in the current tab.</p>
  
  <p>It works for some simple pages, like Wikipedia for instance, but does not work for some complex pages, especially main media pages like <a href="http://www.nytimes.com/" rel="nofollow">http://www.nytimes.com/</a>.</p>
  
  <p>When I debug my content script below, it seems that the document or window element is not corresponding to the page displayed in my Chrome tab.</p>
  
  <p>Here is my manifest.json :</p>
  
  <pre><code>{
    "name": "Capitalize!t2",
    "version": "3.1.2",
    "manifest_version": 2,
    "minimum_chrome_version": "29",
    "browser_action": {
      "default_icon": "icon-small.png",
      "default_popup": "popup.html"
    },
    "content_scripts": [ {
      "js": [ "selectionjfm.js","jquery-1.11.1.min.js","jquery-ui.min.js" ],
      "css":["jquery-ui.css"],
      "matches": [ "http://*/*", "https://*/*"],
      "all_frames":true
      }],
     "icons"  : { "16": "icon-small.png",
             "48": "icon.png",
            "128": "icon-small.png" },
    "key": "MXXXXXX",
    "permissions": [
      "https://secure.flickr.com/","identity", "https://accounts.google.com/*", "https://www.googleapis.com/*","https://spreadsheets.google.com/*","tabs","storage","&lt;all_urls&gt;"
    ],
    "oauth2": {
      "client_id": "XXXXXX",
      "scopes": ["https://www.googleapis.com/auth/plus.login","https://spreadsheets.google.com/feeds"]
    }
  }
  </code></pre>
  
  <p>Here is the intresting part of my popup.js</p>
  
  <pre><code>function extractselection(id)
  {
  chrome.tabs.query({active:true, windowId: chrome.windows.WINDOW_ID_CURRENT}, 
    function(tab) {
      chrome.tabs.sendMessage(tab[0].id, {method: "getSelection"}, 
      function(response){
        var text = document.getElementById(id); 
        if(response!=null){
        text.innerHTML = response.data;
        }
      });
    });
    }
  </code></pre>
  
  <p>Here is then my content script (selectionjfm.cs), a bit messy I am sorry.</p>
  
  <pre><code>chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    //console.log("grrroooooooove"+request.method);
    var toto="";
    if (request.method == "getSelection"){
      var frame= document.getElementsByTagName('iframe');
      if(frame!=null &amp;&amp; frame.length&gt;0){
          for(var i=0;i&lt;frame.length;i++){
              win= frame[i].contentWindow;
              idoc=win.document;
              if(idoc.getSelection){
                  toto+=idoc.getSelection().toString();
              }
          }
          //else console.log("no selection");
      }
  
      //console.log('totototo:'+window.getSelection().toString());
  
      toto+=window.getSelection().toString();
      toto+=document.getSelection().toString();
      var html = document.getElementsByTagName('html')[0].innerHTML;
      sendResponse({data: toto });
      }
    else{
     //console.log('nullll');
      sendResponse({}); // snub them.
  
      }
  });
  </code></pre>
  
  <p>If I go to <a href="http://www.nytimes.com/2015/01/27/nyregion/new-york-blizzard.html?hp&amp;action=click&amp;pgtype=Homepage&amp;module=a-lede-package-region&amp;region=top-news&amp;WT.nav=top-news" rel="nofollow">a NY Times page</a>, run my application and look at the debug console, I can see my document element URI is in fact:</p>
  
  <pre><code>`googleads.g.doubleclick.net/pagead/ads?...`
  </code></pre>
  
  <p>I can see the good URL in <code>document.referer</code>.</p>
  
  <p>I was wondering if any one could explain that redirection and if there was a way to avoid it...</p>