Blog

Hiding Tab Switches from the Page Visibility API

A half-faded eye in a browser window with a puzzle piece in the corner

The moment you switch to another tab, the page knows: document.hidden turns true and a visibilitychange event fires. Video players pause on that signal, course and exam pages mark you as away, and timers stop. I wrote an open source Chrome extension called Visibility Manipulator to keep those signals from reaching the page. The part that runs inside pages is a single 26-line file, inject.js.

Below is why that file has to run in the page's own JavaScript world, why it swallows events only at the page level, and which problems I fixed between 0.1.0 and 0.4.2. One limit up front: the extension changes what the page sees. It cannot change how Chrome slows down background tabs.

A page learns it is hidden in four ways

MDN's Page Visibility API page defines two core signals: document.visibilityState (visible or hidden) and document.hidden. When the value changes, visibilitychange fires. Real pages look at more than that, so the extension covers a wider list:

  • Visibility properties: hidden, visibilityState and their old prefixed twins webkitHidden and webkitVisibilityState.
  • Focus: document.hasFocus() and the blur event on the window. Both change when you switch to another app, even if the tab stays visible.
  • Pointer: mouseleave on the document. Some pages show a warning when the pointer leaves the window.
  • Lifecycle: the freeze event Chrome sends when it freezes a tab.

Pinning the values in the page's own world

In Chrome, a content script runs in an "isolated world" by default. The Chrome for Developers documentation describes it as a private execution environment that the page and other extensions cannot reach. That protects the extension, and it is useless here: change document.hidden inside the isolated world and the page's own code still reads the real value.

So the script is registered with world: 'MAIN'. The scripting API reference describes MAIN as the environment shared with the host page's JavaScript. The second requirement is timing. With runAt: 'document_start', the script runs before any of the page's scripts, so even the page's first read gets the fake value.

The line that pins the values is one helper:

  • const pin = (name, value) =>
  • Object.defineProperty(Document.prototype, name, { get: () => value, configurable: true });

Then come pin('hidden', false) and pin('visibilityState', 'visible'), and hasFocus is replaced with a function that always returns true. I put the getter on Document.prototype rather than on the document itself, because that is where the browser's real getter lives. allFrames: true injects the same script into every iframe, so an embedded player also sees visible in its own document.

One caveat: according to Chrome's documentation, a script injected into the MAIN world is subject to the page's CSP. inject.js uses no eval and makes no network requests, so it also works on pages with a strict CSP.

Swallowing events without breaking forms

Pinning the values is not enough. If the page has a visibilitychange listener, the event still arrives. The extension adds capture-phase listeners on window and document for five events and stops them there: visibilitychange, webkitvisibilitychange, blur, mouseleave and freeze.

Stopping every blur would be the shortest path. But blur is also how a form field reports that you left it, and validation messages and autosave depend on it. So only events whose target is the page itself are swallowed:

  • const isPageTarget = (event) =>
  • event.target === window || event.target === document || event.target === document.documentElement;

If the target is an input or a button, the event continues as usual. In the README's comparison list, this is the line that reads "forms keep working".

Dynamic registration for per-site switches

In 0.1.0 the script was a static content script in manifest.json and ran on every site. To allow switching it off per site, 0.2.0 moved to dynamic registration through chrome.scripting.registerContentScripts. Switched-off sites live in a disabledHosts list in chrome.storage.local, and the script is registered again on every change. The registration's excludeMatches field gets a *://<host>/* pattern for each listed host, so those sites never receive inject.js.

Two decisions sit in that registration. The first is order: the code registers the script first and saves the list second. If a host that Chrome rejects as a match pattern were saved, every later registration would fail too; registering first keeps such a host out of the list. The second is updates: the background service worker rebuilds the registration on chrome.runtime.onInstalled. That event fires on install and on every update, which is when dynamically registered scripts are dropped.

The pattern only looks at the host name. Switch off localhost and both localhost:3000 and localhost:5173 are off.

How the popup knows a page is patched

The popup asks two questions about every open tab. First, is the page patched? It runs a small function in the MAIN world: if the source text of the hidden getter does not contain [native code], the getter belongs to the extension. Second, can the tab reach the extension's scripts? Tabs opened before installation do not answer the vm-ping message, and for those the popup shows a Reload Pages button.

0.4.2: a frozen tab kept the popup waiting for minutes

Up to 0.4.1, the popup waited for every tab to answer through Promise.all before drawing the list. As the commit message says, a frozen background tab can take minutes to answer, and the popup stayed blank the whole time.

0.4.2 changed the order. The list is drawn first, the queries go out afterwards, and each answer updates the button on its own. Discarded, frozen and still-loading tabs are not queried at all; they pick up the settings when they load. The lesson: an extension's UI should never wait on tabs it does not control.

0.4.0: fitting fullscreen into the tab, with two worlds

The tab-sized fullscreen feature I added later exposed a gap in the MAIN world: chrome.storage is not available there. Two scripts work together to know which site has the feature on. fullscreen-config.js reads the setting in the isolated world and sends the page a vm-fullscreen-on or vm-fullscreen-off event. fullscreen.js waits for that event in the MAIN world and changes nothing on the page until it is switched on. The feature is off for every site by default.

What the extension cannot do

  • Timer throttling: According to MDN, timers such as setTimeout are throttled in background tabs, and most browsers stop sending requestAnimationFrame callbacks to hidden tabs. That is the browser's decision, and no page script can lift it. If a page still slows down, move the tab into its own window and leave that window visible. Per MDN, tabs playing audio and tabs using WebSocket or WebRTC connections are exempt from this throttling.
  • Tabs discarded to save memory: Chrome can drop a tab from memory. Add the site to Always keep these sites active under chrome://settings/performance.
  • Server-side measurement: If a site measures activity on its server, through mouse movement or keystrokes for example, the extension does not touch it.
  • Out-of-scope pages: Extensions do not run on chrome:// pages or the Chrome Web Store.
  • Manual verification: I ran my automated tests in Playwright's headless Chromium, and headless does not really hide the tab. To see the "hidden" moment in real Chrome, paste the line from the README into the console and switch tabs: setInterval(() => console.log(document.visibilityState, document.hasFocus()), 2000); With the extension on, every line reads visible true.

The extension is not on the Chrome Web Store. Download the latest release ZIP, turn on developer mode at chrome://extensions and pick the folder with Load unpacked; it runs on Chrome 111 or later and other Chromium browsers. My other open source work is on the projects page, and more technical notes are on the blog.

Chrome ExtensionPerformance