1

In my chrome extention, content script loads in wrong window, instead of loading in top level browser window, it loads in on of the iframes!

So, on https://www.google.com tab, I want it to load in main window, but instead it is loading in Google products dropdown - an iframe with url, https://notifications.google.com.

If I tried to access top level window using window.top or window.parent it is giving error as:

DOMException: Blocked a frame with origin "https://notifications.google.com" from accessing a cross-origin frame.

How can I make the content script to load in top level window?

My manifest.josn contanis:

"content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*", "<all_urls>"],
      "match_about_blank": true,
      "js": ["dist/contentScript.js"],
      "run_at": "document_idle",
      "all_frames": true
    }
  ],
demonofthemist
  • 4,081
  • 4
  • 26
  • 45
  • *Each* tab's "top level" with a matching URL gets its own instance of your content script and when `all_frames` is true *each* matching iframe gets its own instance of your content script too. – wOxxOm Sep 07 '18 at 09:54
  • @wOxxOm But its not, top level window not getting content script when extention added, but if I refersh page, everything works fine! – demonofthemist Sep 07 '18 at 09:55
  • Oh but yes it is because it's how the extensions API is implemented. The reason for "everything works fine" is probably that site is AJAX-driven and your content script doesn't account for such "soft" navigation, see [Is there a JavaScript/jQuery DOM change listener?](//stackoverflow.com/a/39508954) – wOxxOm Sep 07 '18 at 09:57
  • @wOxxOm I am little confused here, does that mean, when an extention is enjected, the windows which get content script do not execute it, if its contents are already loaded? Cause in my case, I am unable to see any logs i've added! – demonofthemist Sep 07 '18 at 10:05
  • I don't understand. I'll try to rephrase. AJAX-driven pages perform XHR/fetch to get the new data from their server, then simply modify the page DOM in-place without navigating the entire page. Since content scripts are automatically injected only on a "hard" navigation, your content script doesn't run on "soft" navigations (performed via HTML5 History API usually). That answer I've linked shows how to deal with this. – wOxxOm Sep 07 '18 at 10:11
  • @wOxxOm I got it now, so since the page has not navigated, the browser did not execute the content script for that particular page! – demonofthemist Sep 07 '18 at 10:18
  • @wOxxOm But the how can I use mutation observer, if content script is not getting executed in first place, how to attch listeners in that window's content script? – demonofthemist Sep 07 '18 at 10:35
  • I'm still in the dark and keep guessing what happens there ([MCVE](/help/mcve)?) but the only possible explanation why the content script isn't there initially is that the extension was just installed/updated/reloaded, in which case you need to enumerate all tabs manually and call executeScript ([example](https://stackoverflow.com/a/11598753)). – wOxxOm Sep 07 '18 at 10:58
  • @wOxxOm Yes, you are right, this problem was happening only when I install/reload. Your link helped & it is working now! Thanks a lot! – demonofthemist Sep 07 '18 at 11:08

0 Answers0