当用户在Chromium中打开新选项卡时运行指定的函数

时间:2021-06-30 22:01:59

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:

我想从Chromium的“新标签页”中删除访问量最大的缩略图。在检查该页面的内容后,我确定以下JavaScript行可以解决问题:

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:

似乎Chromium明确禁止篡改“新标签页”。我通过进行以下更改来调试Haibara Ai的解决方案:

  1. In manifest.json:

    "matches": [
        "*://*/*"
      ],
    
  2. 在manifest.json中:“匹配”:[     “*:// * / *”   ]

  3. In content.js:

    var mv = document.getElementById("most-visited");
    if (mv) mv.remove(); else window.alert("test");
    
  4. 在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.

并重新加载扩展。当我打开一个新标签时,缩略图仍然出现。然而,当我刷新一个不同的页面时,会显示一个显示“test”的消息框。

1 个解决方案

#1


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.

    使用内容脚本。至于匹配newtab网址,请参阅google chrome新标签页的网址以及如何从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.

    使用程序化注射。您可以通过chrome.tabs.onCreated收听新标签打开事件,检查标签网址并确定是否调用chrome.tabs.executeScript。

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

    自定义新标签页。您还可以自定义自己的新标签页,而不会访问最多的部分。

#1


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.

    使用内容脚本。至于匹配newtab网址,请参阅google chrome新标签页的网址以及如何从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.

    使用程序化注射。您可以通过chrome.tabs.onCreated收听新标签打开事件,检查标签网址并确定是否调用chrome.tabs.executeScript。

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

    自定义新标签页。您还可以自定义自己的新标签页,而不会访问最多的部分。