Dan*_*scu 4 javascript tabs google-chrome google-chrome-extension
我编写了一个Chrome扩展程序,它覆盖了新标签页:
manifest.json:
"chrome_url_overrides": {
"newtab": "new-tab.html"
},
Run Code Online (Sandbox Code Playgroud)
有没有办法使这个覆盖可选?也就是说,我想让用户取消选中选项页面中的复选框并禁用新标签覆盖.这一定是可能的,因为当我第一次打开新标签时,会弹出一个通知,指示扩展程序更改"新建标签"设置并询问是保留更改还是恢复设置:

我找不到任何用于控制覆盖的API." 新建选项卡重定向"项目没有显示本机"新建选项卡"的选项.
Google制作了星球大战新标签替换版,可让您查看默认的新标签页.它使用的网址是chrome-search://local-ntp/local-ntp.html.例:
options.html:
<input type="checkbox"> Use default new tab page
Run Code Online (Sandbox Code Playgroud)
options.js:
var checkbox = document.querySelector("input[type=checkbox]")
checkbox.addEventListener("click", function() {
chrome.storage.sync.set({ defaultnewtab: checkbox.checked })
})
Run Code Online (Sandbox Code Playgroud)
newtab.js:
chrome.storage.sync.get("defaultnewtab", function(storage) {
if(storage.defaultnewtab) {
chrome.tabs.update({ url: "chrome-search://local-ntp/local-ntp.html" })
}
})
Run Code Online (Sandbox Code Playgroud)
小智 5
而不是使用的chrome_url_override ,你可以编写侦听标签时使用更新的监听器chrome.tabs.onUpdated.addListener(),然后检查网址是铬:// NEWTAB /如果是和该复选框被选中,然后使用chrome.tabs.update()他们移居到其他页面.
使用@Daniel Herr所述的星球大战方法,我做到了,效果很好。虽然感觉有点。
我popup.html是否在扩展名“开”中设置了一个选项。
首先,使用Chrome定义的方法设置默认的新标签页:
manifest.json
"chrome_url_overrides": {
"newtab": "newtab.html"
},
Run Code Online (Sandbox Code Playgroud)
然后在扩展程序中newtab.html调用一个新的JavaScript文件newtab.js(或其他文件)。
我也使用jQuery,所以我的代码使用了jQuery,但是您可以使用DOMContentLoaded本地执行此操作。
newtab.js
$(document).ready(function(){
// It takes a moment for the Chrome query/update so sometimes there is a flash of content
// Hiding the Body makes it look blank/white until either redirected or shown
$('body').hide();
var background = chrome.extension.getBackgroundPage();
var _app = background._app;
// App is OFF, show Default New Tab
if(!_app._on){
// Get the current Tab
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
var active = tabs[0].id;
// Set the URL to the Local-NTP (New Tab Page)
chrome.tabs.update(active, { url: "chrome-search://local-ntp/local-ntp.html" }, function() { });
});
// App is ON, show custom content
} else {
$('body').show();
}
});Run Code Online (Sandbox Code Playgroud)
基本上,方法是更新标签页,以便将其重定向到chrome-search://local-ntp/local-ntp.html默认Chrome NTP的硬URL。
由于这是Chrome内部网址,因此“网址”字段仍显示为空白。
| 归档时间: |
|
| 查看次数: |
3132 次 |
| 最近记录: |