当用户在Chromium中打开一个新选项卡时运行指定的功能 [英] Run a specified function when the user opens a new tab in Chromium

查看:235
本文介绍了当用户在Chromium中打开一个新选项卡时运行指定的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Chromium的新标签页中删除访问最多的缩略图。在检查完该页面的内容后,我确定下面的JavaScript代码可以实现:

  document.getElementById(most -visited)除去(); 

但是我仍然有一个问题:如何使它在我打开时自动运行一个新标签?据推测,我必须将它包装在一个函数中并注册一个事件处理程序,但我一直无法找到更准确的信息。






编辑:



似乎Chromium明确地防止篡改新标签页面。我调整了Haibara Ai的解决方案,做了以下更改:
$ b


  1. 在manifest.json中:

     matches:[
    *:// * / *
    ],


  2. 在content.js中:

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


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

解决方案

    >

    使用内容脚本。至于匹配newtab网址,请参阅什么是google chrome新标签页的URL以及如何将其从manifest.json中排除



    manifest.json

      {
    name:Redesign,
    version: 1.0,
    manifest_version:2,
    content_scripts:[
    {
    matches:[
    *:// * / _ / chrome / newtab *

    js:[
    content.js

    }
    ]
    }

    content.js

     的document.getElementById( 最参观)删除()。 


  1. 使用程序化注入。您可以通过 chrome.tabs.onCreated来收听新标签打开的活动 ,检查标签url并确定是否调用 chrome.tabs.executeScript


  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.

解决方案

  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.

这篇关于当用户在Chromium中打开一个新选项卡时运行指定的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆