2015-05-25 17 views
3

我寫了一個Chrome擴展程序overrides the New Tab page編程(或可選)覆蓋Chrome的新標籤頁

manifest.json的

"chrome_url_overrides": { 
    "newtab": "new-tab.html" 
    }, 

有沒有一種辦法,使這個覆蓋可選?也就是說,我想讓用戶取消選中選項頁面中的複選框並禁用新標籤覆蓋。這必須是可能的,因爲當我打開的第一次一個新的標籤,有一個擴展的改變新標籤設置和詢問是否保留變更或恢復設置彈出式通知:

enter image description here

我不能」找到任何用於控制覆蓋的API。 New Tab Redirect項目沒有選項來顯示原生新標籤。

+1

這很可能違背了谷歌針對擴展的單一目的政策。您要麼是新標籤的替代品,要麼不是。 – Xan

回答

5

而不是使用chrome_url_override你可以編寫偵聽當標籤使用chrome.tabs.onUpdated.addListener()更新監聽,然後檢查網址是鉻:// NEWTAB /如果是和該複選框被選中,然後使用chrome.tabs.update()將它們重新定位到另一個頁面。

+4

如果['tabs'權限被請求](https://developer.chrome.com/extensions/tabs)(它在安裝擴展時顯示爲警告)「和''chrome.tabs.update' ](https://developer.chrome.com/extensions/tabs#method-update)會將擴展名爲醜陋的'chrome-extension:// hibkhcnpkakjniplpfblaoikigg​​kopka/html/fauxbar.html' URL填充到Omnibox中。 Omnibox如何清除? –

3

Google發了一個Star Wars new tab replacement它允許您查看默認的新標籤頁。它使用的網址是chrome-search://local-ntp/local-ntp.html。例如:

options.html:

<input type="checkbox"> Use default new tab page 

options.js:

var checkbox = document.querySelector("input[type=checkbox]") 
checkbox.addEventListener("click", function() { 
chrome.storage.sync.set({ defaultnewtab: checkbox.checked }) 
}) 

newtab.js:

chrome.storage.sync.get("defaultnewtab", function(storage) { 
if(storage.defaultnewtab) { 
    chrome.tabs.update({ url: "chrome-search://local-ntp/local-ntp.html" }) 
} 
}) 
4

使用所描述的星球大戰方法@Daniel Herr,我做了這個,效果很好。雖然感覺有點黑客。

我有一個選項在popup.html中設置,無論分機是否「打開」。

首先,設置使用Chrome定義的方法的默認新標籤頁:

manifest.json的

"chrome_url_overrides": { 
     "newtab": "newtab.html" 
}, 

然後在你的分機的呼叫newtab.html一個新的JavaScript文件,newtab.js(或任何)。

我也使用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 
 
\t $('body').hide(); 
 

 
\t var background = chrome.extension.getBackgroundPage(); 
 
\t var _app = background._app; 
 

 
\t // App is OFF, show Default New Tab 
 
\t if(!_app._on){ 
 

 
\t \t // Get the current Tab 
 
\t \t chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { 
 

 
\t \t \t var active = tabs[0].id; 
 
      
 
      // Set the URL to the Local-NTP (New Tab Page) 
 
\t \t \t chrome.tabs.update(active, { url: "chrome-search://local-ntp/local-ntp.html" }, function() { }); 
 
\t \t }); 
 

 
\t // App is ON, show custom content 
 
\t } else { 
 
\t \t 
 
\t \t $('body').show(); 
 
\t } 
 

 
});

基本上,方法是使其被重定向到chrome-search://local-ntp/local-ntp.html這是很難URL爲默認瀏覽器NTP更新選項卡。

由於這是Chrome內部網址 - 網址字段仍顯示爲空白。

+0

缺少的信息是URL - 因爲重定向到'chrome:// newtab'沒有提供任何信息。非常有用的答案。 – Xan