2

I want to remove the most visited thumbnails from Chromium's “New Tab” page. After inspecting the contents of that page, I determined that the following line of JavaScript does the trick:

document.getElementById("most-visited").remove();

But I still have one remaining problem: How do make it so that this line runs automatically when I open a new tab? Presumably I have to wrap it in a function and register an event handler, but I've been unable to find more precise information.


EDIT:

It seems that Chromium explicitly prevents tampering with the “New Tab” page. I debugged Haibara Ai's solution by making the following changes:

  1. In manifest.json:

    "matches": [
        "*://*/*"
      ],
    
  2. In content.js:

    var mv = document.getElementById("most-visited");
    if (mv) mv.remove(); else window.alert("test");
    

And reloaded the extension. When I opened a New Tab, the thumbnails still appeared. Yet, when I refreshed a different page, a message box saying “test” was displayed.

isekaijin
  • 19,076
  • 18
  • 85
  • 153
  • 1
    use a content script and have it run on every page, no need for listeners – Daniel Lizik Aug 15 '16 at 01:07
  • @Daniel_L: Thanks for the pointer, will look up more on it. – isekaijin Aug 15 '16 at 01:08
  • I thought chrome extensions and user scripts couldn't access things like new page... (It does actually have a URL though, you can find it by running window.location in console) – Quill Aug 15 '16 at 01:08
  • 1
    @Quill, http://stackoverflow.com/questions/37287333/what-is-the-url-of-the-google-chrome-new-tab-page-and-how-to-exclude-it-from-man/37288295#37288295 – Haibara Ai Aug 15 '16 at 01:23

1 Answers1

2
  1. Use Content scripts. As for matching newtab url, see What is the URL of the google chrome new tab page and how to exclude it from manifest.json.

    manifest.json

    {
      "name": "Redesign",
      "version": "1.0",
      "manifest_version": 2,
      "content_scripts": [
        {
          "matches": [
            "*://*/_/chrome/newtab*"
          ],
          "js": [
            "content.js"
          ]
        }
      ]
    }
    

    content.js

    document.getElementById("most-visited").remove();
    
  2. Use Programmatic injection. You could listen to new tab opened event via chrome.tabs.onCreated, check the tab url and determine whether to call chrome.tabs.executeScript.

  3. Customize new tab page. You could also customize your own new tab page without the most visited part.

Community
  • 1
  • 1
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • Thanks for the answer, your first alternative seems to get to the heart of the technical issue. Unfortunately, it still doesn't solve my original need, because Chromium itself apparently prevents tampering with the “New Tab” page. – isekaijin Aug 15 '16 at 02:00
  • @pyon, it works well at my end. Let me see what may cause the different behavior. – Haibara Ai Aug 15 '16 at 02:04
  • @pyon, if you go to devtools Sources->Content scripts panel, are you able to see your content scripts? – Haibara Ai Aug 15 '16 at 02:19
  • When I'm browsing any normal page (say, Stack Overflow), yes. When I'm in the “New Tab” page, nope. – isekaijin Aug 15 '16 at 02:21
  • @pyon, weird, I'm able to see it in both on newtab page and normal pages – Haibara Ai Aug 15 '16 at 02:26
  • Anyway, the smart approach (1) didn't work, but the dumb one (3) did, so I'm finally accepting this answer. Thanks for your patience! – isekaijin Aug 15 '16 at 03:12
  • @pyon, np, and I have posted a question targeted for this weird behavior: http://stackoverflow.com/questions/38948958/does-content-script-have-access-to-newtab-page – Haibara Ai Aug 15 '16 at 03:34