6

I am currently building a simple google chrome extension to prank my brother. The Extension simply deletes the old page and replaces it with HTML and CSS copied off of the "You have no internet" page, In essence making the user think they don't have internet when they do. The following is the content script injected into all new pages:

var hed = document.head;
var bod = document.body;
document.head.parentElement.removeChild(document.head);
document.body.parentElement.removeChild(document.body);
document.write(/*Copy of "no internet" page source here*/);

This script works perfectly. The only page i don't want the change to occur on is the google chrome New Tab page. The way to go about normally excluding pages is to specify it in the Extensions manifest.json file under the content scripts. Here was my original manifest.json setup:

{
    "manifest_version": 2,

    "name": "No Internet Extention",
    "description": "Make people with this extention think they have no internet",
    "version": "1.0",

    "content_scripts": 
    [
        {
          "matches": ["*://*/*"],
          "js": ["bup.js"]
        }
    ],

    "permissions": 
    [
        "activeTab",
        "https://ajax.googleapis.com/"
    ]
}

I have currently tried the following set ups in my manifest.json file content_scripts section in order to exclude the Chrome New Tab page to no avail:

"matches": ["http://*/*", "https://*/*"]

I assumed this would exclude it because This page on the google product forms Told me that the URL of the New Tab page is "chrome://newtab", which is not a HTTP: or HTTPS: Protocol.

I also Tried:

"matches": ["*//*/*"]
"exclude_matches": ["chrome://newtab"]

"matches": ["*//*/*"]
"exclude_matches": ["chrome://newtab/*"]

"matches": ["*//*/*"]
"exclude_matches": ["*://newtab/*"]

"matches": ["*//*/*"]
"exclude_globs": ["chrome://newtab"]

None of these have worked and the content script still executes on the new tab page. I believe the root of the problem here is that my New Tab URL is incorrect. My research for excluding pages was done here: Google Content Scripts Page. The real question is: Does anyone know the proper URL for the google new tab page or how to go about excluding it in a manifest file?

James Oswald
  • 510
  • 3
  • 12
  • 25
  • 1
    Chrome as of 61 explicitly forbids content scripts on its default new tab page - see https://codereview.chromium.org/2978953002/ – kofifus Aug 13 '17 at 10:59
  • Possible duplicate of [Does content script have access to newtab page?](https://stackoverflow.com/questions/38948958/does-content-script-have-access-to-newtab-page) – kofifus Aug 14 '17 at 10:47

4 Answers4

11

Please note that most of the content in my original answer from 2016 no longer holds true.

I decided to delete the original content of this answer to avoid possibly misleading somebody. The original answer can be viewed in edit history.

Petr Srníček
  • 2,296
  • 10
  • 22
  • 1
    Thanks so much to both you and @Haibara Ai . I'd up-vote both your answers if i had more than 1 reputation. I also wanted to point out that when i tried "run_at": "document_start", The extension didn't run on any webpages at all, but when i left it out it worked perfectly. – James Oswald May 18 '16 at 14:13
3

According to Match Patterns, chrome is not a valid scheme.

scheme := '*' | 'http' | 'https' | 'file' | 'ftp'

  1. You could use the following exclude_matches

    "exclude_matches": ["*://*/_/chrome/newtab*"]
    
  2. You could also check if window.location.href contains chrome/newtab in you content scripts.

Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
0

Opened a new tab, opened the Dev Tools - Network tab. Looks like it requests https://www.google.com/_/chrome/newtab. Try with this one, maybe.

yuriy636
  • 11,171
  • 5
  • 37
  • 42
  • Tried it and it doesn't work, I believe That page only points to the new tab page, the new tab page itself is saved locally somewhere as it loads even without internet connection, meaning it would use a chrome:// "protocol". – James Oswald May 17 '16 at 23:17
0

It's probably safer to use "exclude_globs" instead of "exclude_matches". With "exclude_matches": ["*://*/_/chrome/newtab*"] you could unintentionally skip something like http://example.com/_/chrome/newtab:

"content_scripts": [{
    "matches": ["<all_urls>"],
    "exclude_globs": ["https://www.google.*/_/chrome/newtab*"],
    "run_at": "document_end",
    "js": ["script.js"]
}],
traxium
  • 2,701
  • 2
  • 16
  • 17