npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

keep-tabs-open

v1.0.2

Published

npm + AMD module that prevents Firefox from closing WebExtension tabs during development

Downloads

6

Readme

Keep WebExtension tabs open

This drop-in module does one simple thing: it allows you to keep the extension tabs of your WebExtension open when you reload it during development.

Notes / Limitations

It is not possible to catch the 'unload' event of the background page (in Firefox 65), so this module (only) hooks into the browser.runtime.reload() method, which means that it will only handle reloads initiated by calls to that method, and not reloads caused by clicking Reload on about:debugging#addons or updating/re-installing the extension. I'd suggest you put a button that invokes that function on your extension pages (which you want to have open anyway) or add a context menu option (see below).

I was reminded of this problem and decided to use it as a quick exercise. Is it Worth the Time is already giving me a clear no, so here is the solution I came up with in about two hours:

  • Firefox keeps unloaded/discarded extension tabs open, so unload all tabs prior to reloading the extension, then reload them to load them again.
  • But that only works if the tab is not active in its window, so if that is the case, open a new tab with a reload notice.
  • But now we have those additional tabs, so close them after reloading the extension.
  • But then you have those closed tabs in the session history, so remove them from there as well.
  • And I hope that's it.

This probably requires the 'tabs' permission, and to remove the temporary tabs from the history, it (optionally) requires the 'sessions' permission (which you might want to only add to development builds).

Usage

This module is available via NPM, so do npm install keep-tabs-open. Now include the file node_modules/keep-tabs-open/index.js in your extension (web-ext might mess with this) and load it as a background script (however you do that with your other scripts).

If you use an AMD loader (like require.js) the script will define an anonymous module, otherwise it exposes itself as a keepExtTabsOpen global function. Either way, you need to call that function with extension specific options (all optional) to activate it:

  • iconUrl: favicon of the temporary reload tab.
  • title: HTML title of the temporary reload tab.
  • message: HTML message to display on the temporary reload tab.
  • browser: Promise capable browser API to use and patch. Defaults to the browser global, so in Firefox you usually don't need to supply this.

E.g.:

background/index.js:

const manifest = browser.runtime.getManifest();
require([ 'node_modules/keep-tabs-open/index', ], keepExtTabsOpen => {
	keepExtTabsOpen({ browser: global.browser, iconUrl: '/icon.png',
		title: 'Reloading: '+ manifest.name, message: `
			<style> :root { background: #424F5A; filter: invert(1) hue-rotate(180deg); font-family: Segoe UI, Tahoma, sans-serif; } </style>
			<h1>Reloading ${manifest.name}</h1><p>This tab should close in a few seconds ...</p>
		`,
	}).catch(console.error);
});

If you have the 'menus' or 'contextMenus' permission, you can also add this to make reloading the extension more convenient.

const Menus = browser.menus || browser.contextMenus; if (Menus) {
	Menus.create({ contexts: [ 'browser_action', ], id: 'extension:restart', title: 'Restart Extension', });
	Menus.onClicked.addListener(({ menuItemId, }) => { menuItemId === 'extension:restart' && browser.runtime.reload(); });
}