18

Updated

Thanks @kofifus for the info, Chrome as of 61 explicitly forbids content scripts on its default new tab page

Previous

Say I have the following sample extension, it will output test in console.

manifest.json

{
  "name": "Test",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "content.js"
      ]
    }
  ]
}

content.js

console.log('test');

Will above extension work well in chrome://newtab page?

Some helpful info:

  1. I know by default chrome extension can't access to chrome:// pages, and we could change this behavior through chrome://flags/#extensions-on-chrome-urls

  2. chrome://newtab in fact is a url like https://www.google.co.jp/_/chrome/newtab?espv=2&ie=UTF-8, so it shouldn't be blocked by above restriction.

  3. There are many mouse gestures extension available, like crxMouse, they work well on chrome://newtab page

  4. There are also some voices saying it's not allowed to inject content scripts in chrome://newtab, for example, @Xan's comments below this answer enter image description here and this author's case enter image description here

So it looks weird as its different behavior across different devices( or settings?). Is there any official statements about whether content scripts can run in chrome://newtab pages? Or is there a setting we could change this behavior?

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • @wOxxOm, See #4, there are also some voices saying it's not allowed to inject content scripts into `chrome://newtab` – Haibara Ai Aug 15 '16 at 06:44
  • Note that in case of my comment #4, I really don't know. It was my educated guess. – Xan Aug 23 '16 at 13:53
  • @Xan, in fact I heard many voices about the inconsistent behavior for content scripts in new tab page. For my own experience, most time it works well in new tab page however I do ever meet the case that it doesn't work. – Haibara Ai Aug 24 '16 at 01:51
  • @wOxxOm, the question is, content scripts behavior in newtab is inconsistent. Some people find it not work while the others find it work well, they both simply declare matches as `` and without doing any other things. I want to know the reason causing that and if there is a setting we can change this behavior. – Haibara Ai Aug 24 '16 at 03:04
  • @wOxxOm, but there are also some people find it not work (see the examples in the post), even though they have declared `` – Haibara Ai Aug 24 '16 at 03:31
  • @wOxxOm: I am that third party. I have installed two extensions (AdBlock and pdf.js), but neither should do anything to the “New Tab” page. I installed Chromium from an Arch Linux package. Maybe I should try installing it directly from http://chromium.org/ – isekaijin Aug 25 '16 at 14:37

2 Answers2

9

Chrome as of 61 explicitly forbids content scripts on its default new tab page

kofifus
  • 17,260
  • 17
  • 99
  • 173
2

Q: Is there any official statements about whether content scripts can run in chrome://newtab pages?

A: Not really, but Google's documentation states that:

Host permissions and content script matching are based on a set of URLs defined by match patterns. A match pattern is essentially a URL that begins with a permitted scheme (http, https, file, or ftp, and that can contain '*' characters. The special pattern matches any URL that starts with a permitted scheme.

chrome://newtab only redirects the user to whatever url is set as the new tab page. Usually it will redirect the user to https://<your local google domain>/_/chrome/newtab(a permitted match pattern). But if another extension sets the new tab page to customNewTab.html, then chrome://newtab will redirect the user to chrome-extension://chrome-id/customNewTab.html, and any other extension's content scripts will not be able to run on the new tab.

This SA question also confirms this. If you try adding "exclude_matches": ["*://*/_/chrome/newtab*"], to your manifest, the content script will stop working on the new tab page.

Q: Is there a setting that could change this behavior?

None beside an override page set by another extension. (Note that themes only change the appearance of the new tab, and won't override the new tab url)

Q: Will above extension work well in chrome://newtab page?

A: yes, unless that tab was overriden by another extension.

I have a 30,000 user extension called Screen Shader on the extension store with a huge variety of different chrome versions and a few different browsers, and so far no one has complained about it not working on the new tab page, when they complain a lot about other things.

I'm not too sure why other people think it shouldn't work on the new tab page, but hopefully this answered all the questions you were having.

On a sidenote: Keypress problems with the new tab page

On his blogpost at http://www.toptip.ca/2010/01/google-chrome-content-script.html the developer of "Boss Key and Button" complains about issues with running a content script in the new tab page. His extension minimizes all chrome windows and opens the one you are currently on in a new tab whenever you press F9. He thought the content script didn't work on the new tab page because whenever he used his shortcut in the new tab page, nothing happened. He failed to realize that the reason it wasn't working on the new tab page was because chrome redirects any keypresses to the browser bar so his content script could not capture the keypress.

Try using his extension and pressing F9 on a random page. All chrome windows will be minimized. go back to that random page, and click in the address bar and try pressing F9. This time nothing will happen.

Community
  • 1
  • 1
Marc Guiselin
  • 3,442
  • 2
  • 25
  • 37
  • Does a theme count as an extension? – isekaijin Aug 29 '16 at 02:51
  • @pyon A theme only changes the background image and some of the styling in the page, so it doesn't override the default new tab url. – Marc Guiselin Aug 29 '16 at 03:25
  • My new tab page seems to redirect me to `chrome-search://local-ntp/local-ntp.html` and not a google domain and I am unable to inject a content script here. Is there any way to inject a content script here? I already specified in my manifest's permissions – Tom Apr 13 '20 at 09:41