pyon pyon - 3 months ago 14
Javascript Question

Run a specified function when the user opens a new tab in Chromium

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.

Answer
  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.