DEV Community

Kushagra Gour
Kushagra Gour

Posted on • Originally published at kushagragour.in

5 1

Overriding new tab page in Chrome extension, conditionally!

If you use Chrome extensions like Momentum, Panda etc you know that Chrome extensions have the ability to override your new tab pages i.e. the page you see when you open a new tab in the browser. They do this through the Override Pages API, by doing so in the manifest file:

{
  "name": "My extension",
  ...

  "chrome_url_overrides" : {
    "newtab": "theNewPage.html"
  },
  ...
}
Enter fullscreen mode Exit fullscreen mode

Issue with such extensions is that you can use only one such extension, because if you have multiple extensions with each one trying to override the new tab page, only one of them can finally override. Also, these extensions don't provide any configurable setting to make the overriding of new tab optional. But, there is a very simple trick to make new tab overriding conditional which I use in Web Maker.

First, you don't do anything in the extension's manifest as mentioned above. Then you can have a background page that listens for a new tab creation event. Whenever a new tab is created and the new tab's URL is chrome://newtab/, we can do our condition checking and replace the URL accordingly. Heres how you do that:

chrome.tabs.onCreated.addListener(function(tab) {
    if (tab.url === 'chrome://newtab/') {
        if (shouldReplaceNewTabSetting === true) {
            chrome.tabs.update(
            tab.id,
            {
                url: chrome.extension.getURL('theNewPage.html')
            }
        );
        }

    }
});
Enter fullscreen mode Exit fullscreen mode

There you go - conditional new tab page replacement! You can also see the actual code I use in Web Maker here.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay