简体   繁体   English

用户在Chromium中打开新选项卡时运行指定的功能

[英]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. 我想从Chromium的“新标签”页面中删除访问量最大的缩略图。 After inspecting the contents of that page, I determined that the following line of JavaScript does the trick: 在检查了该页面的内容之后,我确定下面的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. Chromium似乎明确阻止篡改“新标签页”页面。 I debugged Haibara Ai's solution by making the following changes: 我通过进行以下更改调试了Haibara Ai的解决方案:

  1. In manifest.json: 在manifest.json中:

     "matches": [ "*://*/*" ], 
  2. In content.js: 在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 . 至于匹配的newtab URL,请参阅google chrome新标签页的URL是什么以及如何将它从manifest.json中排除

    manifest.json 的manifest.json

     { "name": "Redesign", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "*://*/_/chrome/newtab*" ], "js": [ "content.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. 您也可以自定义自己的新标签页,而无需访问最多的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户打开新标签页时暂停应用程序(AngularJS) - Pause an application when the user opens a new tab (AngularJS) JWT:当用户打开新标签时如何处理GET请求? - JWT: how to handle GET requests when user opens a new tab? 用户打开新标签时如何在click和href事件上定义click事件 - How to define click event on click and href event when user opens a new tab 如何在用户打开新选项卡或在网站中处于非活动状态时重新启动css动画 - How to restart css animation when user opens a new tab or is inactive in the website 单击时如何向按钮添加超链接,打开一个新窗口(不是选项卡),显示 function 的结果? - How to add a hyperlink to a button when clicked, opens a new window(not tab) which displays the result of a function? 当链接在新选项卡中打开时如何保留在当前窗口选项卡上 - How to stay on current window tab when the link opens in new tab iframe在新标签页中打开 - iframe opens in new tab fancybox在新标签页中打开 - fancybox opens in new tab 最大化时,Window.open在safari中打开新标签页 - Window.open opens new tab in safari when maximized 是否可以创建一个按钮,在新选项卡中打开指定网站并关闭当前网站? - Is it possible to create a button that opens a specified website in a new tab and closes the current one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM