Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 以编程方式(或可选)覆盖Chrome';新标签页_Javascript_Google Chrome_Google Chrome Extension_Tabs - Fatal编程技术网

Javascript 以编程方式(或可选)覆盖Chrome';新标签页

Javascript 以编程方式(或可选)覆盖Chrome';新标签页,javascript,google-chrome,google-chrome-extension,tabs,Javascript,Google Chrome,Google Chrome Extension,Tabs,我已经编写了一个Chrome扩展: manifest.json: "chrome_url_overrides": { "newtab": "new-tab.html" }, 有没有办法使此覆盖成为可选的?也就是说,我想让用户取消选中选项页面中的复选框,并禁用新的选项卡覆盖。这必须是可能的,因为当我第一次打开新选项卡时,会出现一个弹出窗口,通知扩展正在更改新选项卡设置,并询问是保留更改还是恢复设置: 我找不到任何用于控制覆盖的API。项目没有显示本机新选项卡的选项。您可以编写一

我已经编写了一个Chrome扩展:

manifest.json

  "chrome_url_overrides": {
    "newtab": "new-tab.html"
  },
有没有办法使此覆盖成为可选的?也就是说,我想让用户取消选中选项页面中的复选框,并禁用新的选项卡覆盖。这必须是可能的,因为当我第一次打开新选项卡时,会出现一个弹出窗口,通知扩展正在更改新选项卡设置,并询问是保留更改还是恢复设置:


我找不到任何用于控制覆盖的API。项目没有显示本机新选项卡的选项。

您可以编写一个侦听器,在选项卡更新时使用
chrome.tabs.onUpdated.addListener()
,然后检查url是否正确chrome://newtab/ 如果是,并且勾选了复选框,然后使用
chrome.tabs.update()
将它们重新定位到另一个页面。

谷歌制作了一个新标签页,允许您查看默认的新标签页。它使用的url是
chrome-search://local-ntp/local-ntp.html

例如:

options.html:

<input type="checkbox"> Use default new tab page
var checkbox = document.querySelector("input[type=checkbox]")
checkbox.addEventListener("click", function() {
 chrome.storage.sync.set({ defaultnewtab: checkbox.checked })
})
chrome.storage.sync.get("defaultnewtab", function(storage) {
 if(storage.defaultnewtab) {
  chrome.tabs.update({ url: "chrome-search://local-ntp/local-ntp.html" })
 }
})
newtab.js:

<input type="checkbox"> Use default new tab page
var checkbox = document.querySelector("input[type=checkbox]")
checkbox.addEventListener("click", function() {
 chrome.storage.sync.set({ defaultnewtab: checkbox.checked })
})
chrome.storage.sync.get("defaultnewtab", function(storage) {
 if(storage.defaultnewtab) {
  chrome.tabs.update({ url: "chrome-search://local-ntp/local-ntp.html" })
 }
})

使用《星球大战》中描述的@Daniel Herr方法,我做到了这一点,效果很好。虽然感觉有点不舒服

我在
popup.html
中设置了一个选项,无论扩展名是否为“开”

首先,使用Chrome定义的方法设置默认的新选项卡页面:

manifest.json

“chrome\u url\u覆盖”:{
“newtab”:“newtab.html”
},
然后在扩展名的
newtab.html
中调用一个新的JavaScript文件,
newtab.js
(或其他任何文件)

我也在使用jQuery,所以我的代码使用它,但您可以使用DOMContentLoaded以本机方式执行此操作

newtab.js

$(文档).ready(函数(){
//Chrome查询/更新需要一段时间,因此有时会出现内容闪现
//隐藏主体使其看起来空白/白色,直到重定向或显示
$('body').hide();
var background=chrome.extension.getBackgroundPage();
var _app=背景。_app;
//应用程序已关闭,显示默认的新选项卡
如果(!\u应用程序。\u打开){
//获取当前选项卡
chrome.tabs.query({active:true,currentWindow:true},函数(tabs){
var active=tabs[0].id;
//将URL设置为本地NTP(新建选项卡页)
chrome.tabs.update(活动,{url:“chrome-search://local-ntp/local-ntp.html“},函数(){});
});
//应用程序已打开,显示自定义内容
}否则{
$('body').show();
}

});
这很可能与谷歌的单一用途扩展政策背道而驰。您可能是一个新的选项卡替换者,也可能不是。如果(在安装扩展时显示为警告)和将填充丑陋的
chrome,则此操作有效-extension://hibkhcnpkakjniplpfblaoikiggkopka/html/fauxbar.html
Omnibox中扩展的URL。如何清除Omnibox?缺少的信息是URL-因为重定向到
chrome://newtab
什么也不给。非常有用的答案。这里有一个适用于Firefox Web扩展的URL吗?