Frameworks Vanilla JS

Vanilla JS

Gate features in a framework-free extension popup with createUserStore or bill.onChange from @extensionbill/sdk and plain DOM rendering.

createUserStore(bill) from the main @extensionbill/sdk entry holds the current user and tells you when it changes, so you can render with plain DOM code. Shared setup: Pick your stack.

Quick start

  1. Create the project.

    npm create vite@latest my-extension -- --template vanilla-ts
    cd my-extension
    npm install @extensionbill/sdk
    npm install -D @crxjs/vite-plugin
    
  2. Add the manifest. Create vite.config.ts; CRXJS builds every file the manifest references:

    // vite.config.ts
    import { defineConfig } from 'vite'
    import { crx } from '@crxjs/vite-plugin'
    
    export default defineConfig({
      plugins: [
        crx({
          manifest: {
            manifest_version: 3,
            name: 'My Extension',
            version: '1.0.0',
            permissions: ['storage', 'alarms'],
            background: { service_worker: 'src/background.ts', type: 'module' },
            action: { default_popup: 'index.html' },
          },
        }),
      ],
    })
    
  3. Create the client and the background.

    # .env.development
    VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_test_…
    
    // src/bill.ts
    import { ExtensionBill } from '@extensionbill/sdk'
    
    export const bill = ExtensionBill({
      publishableKey: import.meta.env.VITE_EXTENSIONBILL_PUBLISHABLE_KEY,
    })
    
    // src/background.ts
    import { bill } from './bill'
    
    bill.startBackground()
    

    Keep startBackground() at the top level (why).

  4. Gate your popup. Replace index.html and src/main.ts:

    <!-- index.html -->
    <!doctype html>
    <html lang="en">
      <body>
        <p id="status">Loading…</p>
        <button id="upgrade" type="button" hidden>Upgrade to Pro</button>
        <button id="restore" type="button" hidden>Already paid? Restore</button>
        <button id="manage" type="button" hidden>Manage subscription</button>
        <script type="module" src="/src/main.ts"></script>
      </body>
    </html>
    
    // src/main.ts
    import { createUserStore } from '@extensionbill/sdk'
    import { bill } from './bill'
    
    const account = createUserStore(bill)
    const status = document.querySelector<HTMLParagraphElement>('#status')!
    const upgrade = document.querySelector<HTMLButtonElement>('#upgrade')!
    const restore = document.querySelector<HTMLButtonElement>('#restore')!
    const manage = document.querySelector<HTMLButtonElement>('#manage')!
    
    function render() {
      const { loading } = account.getSnapshot()
      const pro = account.hasAccess('pro')
    
      status.textContent = loading ? 'Loading…' : pro ? 'Pro feature unlocked.' : 'Free plan'
      upgrade.hidden = loading || pro
      restore.hidden = loading || pro
      manage.hidden = loading || !pro
    }
    
    upgrade.addEventListener('click', () => bill.openCheckout())
    restore.addEventListener('click', () => bill.openRestore())
    manage.addEventListener('click', () => bill.openPortal())
    
    account.subscribe(render) // starts loading; not called with the current state
    render()
    
  5. Test a purchase. Run npm run dev, load the dist folder unpacked and buy Pro with a sandbox buyer (steps).

Advanced

Drop-in paywall

Swap your upgrade buttons for the ready-made Paywall UI. Add <div id="paywall"></div> to index.html, then mount it while the user has no access and call the returned cleanup function once they do:

// src/main.ts (additions)
import { mountPaywall } from '@extensionbill/sdk/paywall'

const paywallHost = document.querySelector<HTMLDivElement>('#paywall')!
let unmountPaywall: (() => void) | null = null

account.subscribe(() => {
  const showPaywall = !account.getSnapshot().loading && !account.hasAccess('pro')
  if (showPaywall && !unmountPaywall) {
    unmountPaywall = mountPaywall(paywallHost, bill, { title: 'Upgrade to Pro', theme: 'auto' })
  }
  if (!showPaywall && unmountPaywall) {
    unmountPaywall()
    unmountPaywall = null
  }
})

Every option: Paywall UI.

What createUserStore returns

MemberWhat it does
getSnapshot()Returns { user, loading, error }, the same object until the state changes
subscribe(listener)Calls listener on every change (not immediately) and returns an unsubscribe function. The first subscriber starts the load.
hasAccess(feature?)Checks the current snapshot
refresh()Reloads from the server and updates the snapshot

Field meanings: What every binding returns. Every call with the same bill returns the same store, so one load serves the whole page.

Without the store

The store is optional. The core API does the same job in two calls, and it is what content scripts and the no-bundler build use:

import type { UserState } from '@extensionbill/sdk'
import { bill } from './bill'

function render(user: UserState) {
  document.body.classList.toggle('pro', bill.hasAccess(user, 'pro'))
}

bill.onChange((user) => render(user)) // fires in every extension context
bill.getUser().then(render)

Keys per build

npm run build reads .env.production: put VITE_EXTENSIONBILL_PUBLISHABLE_KEY=pk_live_… there. Live keys work after going live.

Other build tools

  • WXT: npx wxt@latest init my-extension with the Vanilla template, then follow the WXT setup without a modules entry. The popup files from step 4 go in entrypoints/popup/, with the script src changed to ./main.ts.
  • No bundler: copy the global build into your extension: Without a bundler. It has no createUserStore, so use bill.onChange() as shown above.

Troubleshooting

  • Nothing renders after loading: call account.subscribe(...) before reading state; the first subscriber is what starts the load.

Everything else: shared troubleshooting.

Next steps