Sell a Chrome extension with license keys

To sell a Manifest V3 Chrome extension, bundle monemee-client into the popup and the service worker and pass it a storage adapter around chrome.storage.local. The popup calls activate(key) once. The service worker calls check() on startup and on an alarm and saves the result for the rest of the extension.

Prerequisites

Steps

1. Install the package

npm install monemee-client

2. Declare permissions in manifest.json

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0.0",
  "background": { "service_worker": "background.js", "type": "module" },
  "action": { "default_popup": "popup.html" },
  "permissions": ["storage", "alarms"],
  "host_permissions": ["https://monemee.com/*"]
}

background.js and popup.js are your bundled outputs.

3. Create the license with a chrome.storage adapter

createLicense accepts storage: { get, set, remove }. Each method receives a string key; get returns the stored string or null, and all three may return promises. This module is shared by the service worker and the popup:

// src/license.js
import { createLicense } from 'monemee-client';

const prefix = 'monemee:';
const storage = {
  get: async (k) => (await chrome.storage.local.get(prefix + k))[prefix + k] ?? null,
  set: (k, v) => chrome.storage.local.set({ [prefix + k]: v }),
  remove: (k) => chrome.storage.local.remove(prefix + k)
};

export const license = createLicense({ productId: 123, storage, graceDays: 14 });

// Checks the license and saves a small status object for content scripts and pages
export async function refreshStatus() {
  const r = await license.check();
  const status = { valid: r.valid, plan: r.plan, reason: r.reason, offline: Boolean(r.offline) };
  await chrome.storage.local.set({ proStatus: status });
  return status;
}

4. Check in the service worker

// src/background.js
import { refreshStatus } from './license.js';

function schedule() {
  chrome.alarms.create('license-check', { periodInMinutes: 720 }); // every 12 hours
}

chrome.runtime.onInstalled.addListener(() => { schedule(); refreshStatus(); });
chrome.runtime.onStartup.addListener(() => { schedule(); refreshStatus(); });
chrome.alarms.onAlarm.addListener((alarm) => {
  if (alarm.name === 'license-check') refreshStatus();
});

check() does not throw on network errors. Offline it returns the last signed result with offline: true for up to graceDays.

5. Activate the key in the popup

<!-- popup.html -->
<form id="activate">
  <input id="key" placeholder="MM-XXXX-XXXX-XXXX-XXXX" autocomplete="off" required>
  <button>Activate</button>
</form>
<p id="message" role="alert"></p>
<button id="buy" type="button">Buy a license</button>
<script type="module" src="popup.js"></script>
// src/popup.js
import { license, refreshStatus } from './license.js';

const message = document.getElementById('message');

document.getElementById('activate').addEventListener('submit', async (e) => {
  e.preventDefault();
  try {
    const result = await license.activate(document.getElementById('key').value);
    if (!result.valid) { message.textContent = `Not activated: ${result.reason}`; return; }
    await refreshStatus();
    message.textContent = 'Pro is active.';
  } catch (err) {
    message.textContent = err.message; // wrong format or no connection
  }
});

document.getElementById('buy').addEventListener('click', () => {
  chrome.tabs.create({ url: 'https://monemee.com/p/123' });
});

6. Gate paid features

const { proStatus } = await chrome.storage.local.get('proStatus');
if (proStatus?.valid) enableProFeatures();

Content scripts can read chrome.storage.local too, so they do not need to import the SDK.

How it works

The popup and the service worker are separate contexts, but both read and write the same chrome.storage.local, so a key activated in the popup is visible to the worker. The SDK stores the key with the last signed response under monemee:state and a random installation ID under monemee:device. Each online check replaces the stored response; offline, the SDK accepts it only if the Ed25519 signature, key hash, product, device and issue time match. A refund makes the next online check return reason: 'revoked'.

Like any check that runs on the user’s machine, this one can be patched out by someone who edits the extension code. It stops casual sharing: each key works on a limited number of devices, which you set on the product.

FAQ

Why do I need a storage adapter in a Manifest V3 extension?

Service workers have no localStorage. Without a storage option monemee-client falls back to memory there, and the activation is lost whenever Chrome stops the worker. An adapter around chrome.storage.local keeps it, and the popup and the worker share the same data.

Which permissions does the extension need?

storage for chrome.storage, alarms if you re-check periodically, and host_permissions for https://monemee.com/* so the extension can call the license API.

Can I use monemee-client without a bundler?

Not directly. Extensions cannot resolve npm package names, so bundle the service worker and the popup script with the bundler you already use. The package has no dependencies, so the output stays small.

Next steps

Last updated: