diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a261f29
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+dist/*
diff --git a/README.md b/README.md
index a692766..a06a0d3 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,92 @@
-# tab-park
+# Tab Park
-A light weight tab unloader
\ No newline at end of file
+Tab Park frees up memory by putting tabs you are not using to sleep. A
+parked tab stays in the tab strip with its title and icon, but Firefox lets
+go of the page itself. Click the tab and it comes back, like a refresh.
+
+That is the whole idea. It is not a tab manager, a session saver, or a
+workspace. It parks tabs.
+
+## How to use
+
+Install it from addons.mozilla.org once it is listed. The toolbar button
+opens a small panel with everything on it:
+
+- **Unload this tab** moves you to the next tab that is still awake, then
+ parks the one you were on. If every other tab is already parked, it opens
+ a new tab to move you to instead.
+- **Unload other tabs in this window** parks every other tab in the window,
+ leaving the one you are on. Pinned tabs are left alone.
+- **Never park this tab** takes the current tab out of the idle sweep and
+ out of "Unload other tabs". Handy for a chat or a dashboard you keep open
+ for hours.
+- **Auto-unload** parks a tab after it has been idle for a while. A tab is
+ idle when it has not been the tab you were looking at for the chosen
+ time; looking at a tab resets its clock. It is off by default, and the
+ default idle time is 5 minutes.
+
+Keyboard shortcuts: `Ctrl+Alt+P` parks the current tab, `Ctrl+Alt+A` parks
+every other tab in the window, leaving the one you are on. Open the
+extension's Preferences and click Manage Extension Shortcuts, or use
+`about:addons` under the gear icon.
+
+## What it never does
+
+- It never closes a tab. Nothing is lost; everything stays where it was.
+- It never parks pinned tabs, the tab you are on, tabs that are still
+ loading, or tabs that would show a "leave this page?" warning.
+- It never parks a tab that is playing sound, so your music keeps going.
+ You can turn that safeguard off in the toolbar panel.
+- It never reads your browsing. The extension has no permission to see
+ your tabs or your websites, so it cannot look at addresses, titles, or
+ page content. It can only see the state a tab is in, awake or parked,
+ pinned, playing sound, and how long ago you last looked at it. That is
+ exactly what it needs to decide what to park.
+- It never talks to the network, shows notifications, or collects anything.
+ Settings stay on your machine. The only permissions are `storage` and
+ `alarms`.
+
+One consequence of those small permissions: "Never park this tab" is tied to
+the tab itself and lasts only until that tab is closed or Firefox restarts.
+
+## How it is different
+
+Most tab unloaders are tab managers with an unloading feature bolted on.
+They ask for access to your tabs or to every site you visit, and then offer
+rules, schedules, whitelists, statistics, and a settings page full of
+switches.
+
+Tab Park does one thing: it parks tabs, on demand or once they have been
+idle long enough. The permission list is the short version of that
+difference.
+
+## Settings
+
+The toolbar panel has the auto-unload switch, the idle time, the skip-sound
+toggle, and the "Never park this tab" button. The extension's Preferences page
+only shows the current keyboard shortcuts and a button that opens Firefox's
+Manage Extension Shortcuts page.
+
+## A small quirk
+
+On Firefox 153, a parked tab does not get the faded look that Firefox's own
+Unload Tab menu item gives, unless you set `browser.tabs.fadeOutUnloadedTabs`
+to true in `about:config`. Firefox 160 fades them properly (bug 2000401).
+
+## For developers
+
+`tools/build.sh` writes `dist/tabpark/` (unpacked) and `dist/tabpark.xpi`
+(zipped). The xpi is what you upload to AMO. `npx web-ext lint --source-dir
+dist/tabpark` is clean.
+
+The test harness drives the real extension in Firefox through Selenium; see
+`test/harness/README.md`.
+
+To try it without installing, load it temporarily from
+`about:debugging#/runtime/this-firefox` (Load Temporary Add-on, pick
+`manifest.json`).
+
+## The name
+
+A park is where something waits without going away. That is what happens to
+the tab: it stays in the strip, out of memory, until you come back.
diff --git a/background.js b/background.js
new file mode 100644
index 0000000..9d4ad68
--- /dev/null
+++ b/background.js
@@ -0,0 +1,90 @@
+// Tab Park - keyboard commands, popup actions and the idle sweep. Loaded
+// after common.js, so the tu* helpers are in scope.
+
+const TU_ALARM = 'tu-idle-sweep';
+const TU_SWEEP_MINUTES = 0.25; // 15 s
+
+// ---------- idle sweep
+
+async function tuSyncAlarm() {
+ const { autoUnload } = await tuSettings();
+ if (autoUnload) {
+ browser.alarms.create(TU_ALARM, { periodInMinutes: TU_SWEEP_MINUTES });
+ } else {
+ await browser.alarms.clear(TU_ALARM);
+ }
+}
+
+async function tuSweep() {
+ const { autoUnload, idleSeconds, skipAudible } = await tuSettings();
+ if (!autoUnload) return 0;
+ const cutoff = Date.now() - idleSeconds * 1000;
+ const protectedIds = await tuProtectedIds();
+ const tabs = await browser.tabs.query({});
+ const idle = tuCandidates(tabs, { skipAudible }).filter(tab =>
+ !protectedIds.has(tab.id) &&
+ typeof tab.lastAccessed === 'number' && tab.lastAccessed <= cutoff
+ );
+ return tuDiscard(idle);
+}
+
+// ---------- wiring
+
+browser.runtime.onInstalled.addListener(() => {
+ tuSyncAlarm();
+});
+
+browser.runtime.onStartup.addListener(() => {
+ tuSyncAlarm();
+});
+
+browser.storage.onChanged.addListener((changes, area) => {
+ if (area === 'local' && changes.autoUnload) tuSyncAlarm();
+});
+
+// Forget the parking tab once the user navigates it or closes it. Without the
+// "tabs" permission there is no URL to compare, but the tab has finished its
+// initial load before the id is recorded, so any later load is a navigation.
+browser.tabs.onUpdated.addListener(async (tabId, changeInfo) => {
+ if (changeInfo.status !== 'loading') return;
+ if (tabId === await tuParkingTabId()) {
+ await browser.storage.session.remove(TU_PARKING_KEY);
+ }
+});
+
+browser.tabs.onRemoved.addListener(async tabId => {
+ if (tabId === await tuParkingTabId()) {
+ await browser.storage.session.remove(TU_PARKING_KEY);
+ }
+ if (await tuIsProtected(tabId)) {
+ await tuSetProtected(tabId, false);
+ }
+});
+
+browser.alarms.onAlarm.addListener(alarm => {
+ if (alarm.name === TU_ALARM) tuSweep();
+});
+
+browser.commands.onCommand.addListener(async command => {
+ const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
+ if (!tab) return;
+ if (command === 'unload-current') {
+ await tuUnloadActiveTab(tab);
+ } else if (command === 'unload-others') {
+ await tuUnloadOtherTabs(tab.windowId, tab.id);
+ }
+});
+
+// The popup hands the work over instead of doing it itself: switching tabs
+// closes the popup, which would kill the rest of its async handler.
+browser.runtime.onMessage.addListener(message => {
+ if (!message || typeof message !== 'object') return;
+ if (message.type === 'tu:unload-current') {
+ if (typeof message.windowId !== 'number') return false;
+ return browser.tabs.query({ active: true, windowId: message.windowId })
+ .then(([tab]) => tab ? tuUnloadActiveTab(tab) : false);
+ }
+ if (message.type === 'tu:unload-others') {
+ return tuUnloadOtherTabs(message.windowId, message.tabId);
+ }
+});
diff --git a/common.js b/common.js
new file mode 100644
index 0000000..5bf2fc5
--- /dev/null
+++ b/common.js
@@ -0,0 +1,190 @@
+// Tab Park - settings and discard helpers shared by the background page,
+// the popup and the options page. Loaded as a classic script, so declarations
+// here are visible to whatever script follows.
+
+const TU_DEFAULTS = {
+ autoUnload: false,
+ idleSeconds: 300,
+ skipAudible: true
+};
+
+// Below this the sweep would fight the user's own tab switching.
+const TU_MIN_IDLE_SECONDS = 10;
+
+async function tuSettings() {
+ const stored = await browser.storage.local.get(TU_DEFAULTS);
+ const idle = Math.round(Number(stored.idleSeconds));
+ return {
+ autoUnload: Boolean(stored.autoUnload),
+ idleSeconds: Number.isFinite(idle) && idle >= TU_MIN_IDLE_SECONDS ? idle : TU_DEFAULTS.idleSeconds,
+ skipAudible: Boolean(stored.skipAudible)
+ };
+}
+
+async function tuSaveSettings(patch) {
+ await browser.storage.local.set(patch);
+}
+
+// A tab is a discard candidate when it is loaded, not pinned, not the active
+// tab of its window, and not mid-navigation. Firefox refuses to discard the
+// active tab, so filtering it here keeps the counts honest.
+function tuCandidates(tabs, { skipAudible = false } = {}) {
+ return tabs.filter(tab =>
+ !tab.discarded &&
+ !tab.pinned &&
+ !tab.active &&
+ tab.status !== 'loading' &&
+ (!skipAudible || !tab.audible)
+ );
+}
+
+const TU_PARKING_KEY = 'parkingTabId';
+
+// The blank tab we opened to park the window on. Without the "tabs"
+// permission Firefox does not reveal a tab's URL, so the id is the only way
+// to tell this tab apart from a page the user opened.
+async function tuParkingTabId() {
+ const stored = await browser.storage.session.get(TU_PARKING_KEY);
+ const id = stored[TU_PARKING_KEY];
+ return typeof id === 'number' ? id : null;
+}
+
+async function tuSetParkingTabId(tabId) {
+ await browser.storage.session.set({ [TU_PARKING_KEY]: tabId });
+}
+
+// A blank tab is the one place unloading makes no sense: there is nothing to
+// free, and unloading it would only put another blank tab in its place.
+// Without the "tabs" permission Firefox does not reveal tab URLs, so the id
+// of the tab we opened is the only signal available.
+async function tuIsParkingTab(tab) {
+ if (!tab) return false;
+ return tab.id === await tuParkingTabId();
+}
+
+const TU_PROTECTED_KEY = 'protectedTabIds';
+
+// Tabs the user marked "never park". Stored by id, so the mark lives as long
+// as the tab does. Without the "tabs" permission there is no stable
+// identifier to carry it across a restart.
+async function tuProtectedIds() {
+ const stored = await browser.storage.session.get(TU_PROTECTED_KEY);
+ const ids = stored[TU_PROTECTED_KEY];
+ return new Set(Array.isArray(ids) ? ids.filter(id => typeof id === 'number') : []);
+}
+
+async function tuIsProtected(tabId) {
+ return (await tuProtectedIds()).has(tabId);
+}
+
+async function tuSetProtected(tabId, protect) {
+ const ids = await tuProtectedIds();
+ if (protect) ids.add(tabId);
+ else ids.delete(tabId);
+ await browser.storage.session.set({ [TU_PROTECTED_KEY]: [...ids] });
+}
+
+async function tuWaitActive(tabId, timeoutMs = 1000) {
+ const deadline = Date.now() + timeoutMs;
+ while (Date.now() < deadline) {
+ const tab = await browser.tabs.get(tabId).catch(() => null);
+ if (!tab) return false;
+ if (tab.active) return true;
+ await new Promise(resolve => setTimeout(resolve, 25));
+ }
+ return false;
+}
+
+async function tuWaitComplete(tabId, timeoutMs = 2000) {
+ const deadline = Date.now() + timeoutMs;
+ while (Date.now() < deadline) {
+ const tab = await browser.tabs.get(tabId).catch(() => null);
+ if (!tab) return false;
+ if (tab.status === 'complete') return true;
+ await new Promise(resolve => setTimeout(resolve, 25));
+ }
+ return false;
+}
+
+// Opens the blank tab the window parks on. The id is recorded only after the
+// initial load finishes, so a later "loading" event means the user navigated
+// it and the id can be dropped.
+async function tuCreateParkingTab(windowId) {
+ const created = await browser.tabs.create({ windowId, active: true });
+ await tuWaitActive(created.id);
+ await tuWaitComplete(created.id);
+ await tuSetParkingTabId(created.id);
+ return created;
+}
+
+// Discards in one batch, then reads the tabs back so the caller gets the
+// number that actually unloaded. A tab with a beforeunload prompt cannot be
+// discarded and rejects the batch, so fall back to one at a time.
+async function tuDiscard(tabs) {
+ const ids = tabs.map(tab => tab.id).filter(id => typeof id === 'number');
+ if (ids.length === 0) return 0;
+ try {
+ await browser.tabs.discard(ids);
+ } catch (e) {
+ for (const id of ids) {
+ await browser.tabs.discard(id).catch(() => {});
+ }
+ }
+ const after = await browser.tabs.query({});
+ const byId = new Map(after.map(tab => [tab.id, tab]));
+ return ids.filter(id => {
+ const tab = byId.get(id);
+ return !tab || tab.discarded;
+ }).length;
+}
+
+// Unloads every candidate in the window except the tab the user asked to
+// keep. tuCandidates already excludes the active tab; keepTabId covers the
+// context menu case, where the tab to keep is not the active one.
+async function tuUnloadOtherTabs(windowId, keepTabId) {
+ if (typeof windowId !== 'number') return 0;
+ const { skipAudible } = await tuSettings();
+ const protectedIds = await tuProtectedIds();
+ const tabs = await browser.tabs.query({ windowId });
+ return tuDiscard(
+ tuCandidates(tabs, { skipAudible })
+ .filter(tab => tab.id !== keepTabId && !protectedIds.has(tab.id))
+ );
+}
+
+// Unloads the active tab without leaving a blank window: switch to the next
+// loaded tab first (wrapping around), or open a fresh tab when every other
+// tab is already unloaded. Firefox will not discard the active tab, so the
+// order matters. The discard is verified because tabs.discard() resolves
+// without doing anything if the tab is still selected.
+async function tuUnloadActiveTab(tab) {
+ if (await tuIsParkingTab(tab)) return false;
+
+ const tabs = await browser.tabs.query({ windowId: tab.windowId });
+ const current = tabs.find(other => other.id === tab.id) || tab;
+ const loaded = tabs
+ .filter(other => other.id !== current.id && !other.discarded && other.status !== 'loading')
+ .sort((a, b) => a.index - b.index);
+ const next = loaded.find(other => other.index > current.index) || loaded[0];
+
+ if (next) {
+ await browser.tabs.update(next.id, { active: true });
+ } else if (tabs.length > 1) {
+ await tuCreateParkingTab(current.windowId);
+ } else {
+ // The only tab in the window: there is nowhere to switch to.
+ return false;
+ }
+
+ for (let attempt = 0; attempt < 3; attempt++) {
+ try {
+ await browser.tabs.discard(current.id);
+ } catch (e) {
+ return false;
+ }
+ const after = await browser.tabs.get(current.id).catch(() => null);
+ if (!after || after.discarded) return true;
+ await new Promise(resolve => setTimeout(resolve, 120));
+ }
+ return false;
+}
diff --git a/icons/icon-128.png b/icons/icon-128.png
new file mode 100644
index 0000000..9c8b7ef
Binary files /dev/null and b/icons/icon-128.png differ
diff --git a/icons/icon-16.png b/icons/icon-16.png
new file mode 100644
index 0000000..fca55e1
Binary files /dev/null and b/icons/icon-16.png differ
diff --git a/icons/icon-32.png b/icons/icon-32.png
new file mode 100644
index 0000000..f280ed8
Binary files /dev/null and b/icons/icon-32.png differ
diff --git a/icons/icon-48.png b/icons/icon-48.png
new file mode 100644
index 0000000..6b114b2
Binary files /dev/null and b/icons/icon-48.png differ
diff --git a/icons/icon-96.png b/icons/icon-96.png
new file mode 100644
index 0000000..ae40530
Binary files /dev/null and b/icons/icon-96.png differ
diff --git a/manifest.json b/manifest.json
new file mode 100644
index 0000000..d5a76b9
--- /dev/null
+++ b/manifest.json
@@ -0,0 +1,55 @@
+{
+ "manifest_version": 3,
+ "name": "Tab Park",
+ "version": "0.1.0",
+ "description": "Unload tabs on demand, or automatically after they have been idle for a while.",
+ "browser_specific_settings": {
+ "gecko": {
+ "id": "tabpark@neutrino.local",
+ "strict_min_version": "140.0",
+ "data_collection_permissions": {
+ "required": ["none"]
+ }
+ }
+ },
+ "icons": {
+ "16": "icons/icon-16.png",
+ "32": "icons/icon-32.png",
+ "48": "icons/icon-48.png",
+ "96": "icons/icon-96.png",
+ "128": "icons/icon-128.png"
+ },
+ "permissions": ["storage", "alarms"],
+ "background": {
+ "scripts": ["common.js", "background.js"]
+ },
+ "action": {
+ "default_title": "Tab Park",
+ "default_popup": "popup.html",
+ "default_icon": {
+ "16": "icons/icon-16.png",
+ "32": "icons/icon-32.png",
+ "48": "icons/icon-48.png",
+ "96": "icons/icon-96.png",
+ "128": "icons/icon-128.png"
+ }
+ },
+ "options_ui": {
+ "page": "options.html",
+ "open_in_tab": false
+ },
+ "commands": {
+ "unload-current": {
+ "suggested_key": {
+ "default": "Ctrl+Alt+P"
+ },
+ "description": "Unload this tab and switch to the next loaded tab"
+ },
+ "unload-others": {
+ "suggested_key": {
+ "default": "Ctrl+Alt+A"
+ },
+ "description": "Unload other tabs in this window"
+ }
+ }
+}
diff --git a/options.css b/options.css
new file mode 100644
index 0000000..6fb2aa7
--- /dev/null
+++ b/options.css
@@ -0,0 +1,73 @@
+:root {
+ color-scheme: light dark;
+ --fg: #1c1e24;
+ --muted: #5d6472;
+ --line: #d9dce3;
+}
+
+@media (prefers-color-scheme: dark) {
+ :root {
+ --fg: #e8eaf0;
+ --muted: #9aa3b5;
+ --line: #3a4152;
+ }
+}
+
+body {
+ max-width: 560px;
+ margin: 0 auto;
+ padding: 24px 20px 40px;
+ font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
+ color: var(--fg);
+}
+
+h1 {
+ margin: 0 0 20px;
+ font-size: 20px;
+ font-weight: 600;
+}
+
+h2 {
+ margin: 0 0 10px;
+ font-size: 14px;
+ font-weight: 600;
+}
+
+section {
+ margin: 0 0 24px;
+ padding: 16px;
+ border: 1px solid var(--line);
+ border-radius: 8px;
+}
+
+p {
+ margin: 8px 0 0;
+ color: var(--muted);
+}
+
+.hint {
+ font-size: 13px;
+}
+
+kbd {
+ padding: 1px 5px;
+ font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
+ color: var(--fg);
+ border: 1px solid var(--line);
+ border-bottom-width: 2px;
+ border-radius: 4px;
+}
+
+button {
+ padding: 7px 12px;
+ font: inherit;
+ color: var(--fg);
+ background: transparent;
+ border: 1px solid var(--line);
+ border-radius: 6px;
+ cursor: pointer;
+}
+
+button:hover {
+ border-color: var(--muted);
+}
diff --git a/options.html b/options.html
new file mode 100644
index 0000000..048e2e1
--- /dev/null
+++ b/options.html
@@ -0,0 +1,24 @@
+
+
+
+
+
+ Tab Park shortcuts
+
+
+
Tab Park
+
+
+
Keyboard shortcuts
+
unloads the current tab and switches
+ to the next loaded tab.
+
unloads every other tab in this window,
+ leaving the one you are on.
+
+
These are the shortcuts in effect now. Change them in the
+ Firefox shortcuts page.