[英]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的解决方案:
In manifest.json: 在manifest.json中:
"matches": [ "*://*/*" ],
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.
但是,当我刷新另一个页面时,显示了一个消息框,显示“测试”。
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();
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
。
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.