Programmatically (or optionally) override the new Chrome page page

I wrote a Chrome extension that overrides the new tab page :

manifest.json

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

      

Is there a way to make this override optional? That is, I would like to enable the user to uncheck the checkbox on the options page and disable the new tab override. This should be possible because when I open a new tab for the first time, a popup appears informing the extension changing the settings of the new tab and asking whether to save changes or restore the settings:

enter image description here

I couldn't find an API to control overrides. The New Tab Redirect project does not have the ability to display its own new tab.

+3


source to share


3 answers


Instead of using, chrome_url_override

you can write a listener that listens for when tab updates are used with chrome.tabs.onUpdated.addListener()

and then check if the url is chrome: // newtab / and if so and the checkbox is checked, then with chrome.tabs.update()

move them to another page ...



+5


source


Google has introduced a new Star Wars New Features tab that allows you to view the new tab by default. The url used is chrome-search: //local-ntp/local-ntp.html. Example:

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


source


Using the Star Wars method as described by @Daniel Herr I did it and it works well. Feels a little hacked though.

I have an option set to popup.html

whether the extension is "on" or not.

First of all, set your default new tab using the method defined by Chrome:

manifest.json

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

      

Then, in the extension, newtab.html

call the new JavaScript file, newtab.js

(or whatever).

I'm using jQuery as well, so my code uses that, but you can do this natively with 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 codeHide result


Basically, the methodology is to update the tab to redirect to chrome-search://local-ntp/local-ntp.html

, which is the hard URL for Chrome NTP by default.

Since this is Chrome's internal URL, the URL field is still blank.

+4


source







All Articles