ba01be1c5d
Restructure the MV3 background worker from a monolithic core.ts/index.ts
into a class-based command architecture. Behavior is identical — the 83
registered commands dispatch byte-for-byte the same as before.
Structure
- One class per command group, each extending CommandGroup and exporting a
`commands` map keyed by the full command id ("tabs.close"). Groups:
Navigation, TabsMutation, TabsQuery, Groups, Windows, Dom (dom/extract/
page), BrowserData (storage/cookies), Session (session/clients + autosave
+ lazy-tab activation), Perf (perf + jobs.status/cancel), Extension.
- CommandRegistry merges the group maps (throws on duplicate ids), routes
background specs to JobManager and paginates array results via
makePagedData. JobManager owns the job map + lifecycle. NativeConnection
owns the native-port lifecycle and the inbound message router.
- index.ts is now thin wiring: JobManager -> ctx -> assembleRegistry ->
onActivated -> NativeConnection.start().
- Infra classes live in classes/ (PascalCase, file = class name); command
groups in commands/; shared helpers split out of core.ts into core/
(errors, throttle, scripting, tab-helpers, group-helpers, storage); all
types moved into types/ (json, jobs, session, tabs, messages,
command-args) behind a barrel.
DRY cleanup
- resolveTabUrl(tabId) and assertScriptableUrl(url, action) collapse the
tab/URL-guard boilerplate duplicated across dom.ts and browser-data.ts.
- processInBatches() centralizes the throttled, cancellable batch loop
shared by tabs.close, group.close and tabs.merge_windows.
- captureCurrentSession() dedups the snapshot-and-signature block shared by
session.save and the autosave path.
- DomArgs type alias replaces 21 inline ContentArgs & { tabId? } copies.
- Drop fetchTabHtml's redundant retry loop (executeScript already retries
transient frame/tab errors), a dead tabInfo import, and two stale
comments referencing a removed asArgs helper.
Type safety & tests
- Full noImplicitAny; no `any`/`unknown` annotations remain in src.
- JS unit-test harness using node --test + node:assert (zero new deps),
bundled via the existing esbuild. Covers JobManager retention/lifecycle
and the autosave listener-wiring/debounce with an in-memory chrome mock.
- The structural pytest checks track the new file homes and the centralized
processInBatches helper.
Verification: npm run check:extension green (tsc + esbuild 84.5kb +
node --check + 18 JS tests); uv run pytest -q -> 409 passed, 105 skipped.
No version bump.
114 lines
5.1 KiB
TypeScript
114 lines
5.1 KiB
TypeScript
import { getActiveTab, getAliases, isBrowserErrorUrl, resolveGroupId, tabInfo } from '../core';
|
|
import { CommandGroup } from '../classes/CommandGroup';
|
|
import type { CommandEntry } from '../classes/CommandGroup';
|
|
import type { NavOpenArgs, NavToArgs, NavTabArgs, NavFocusArgs, NavWaitArgs, NavOpenWaitArgs } from '../types';
|
|
|
|
export class NavigationCommands extends CommandGroup {
|
|
readonly namespace = "navigate";
|
|
readonly commands: Record<string, CommandEntry> = {
|
|
"navigate.open": (a: NavOpenArgs) => this.navOpen(a),
|
|
"navigate.to": (a: NavToArgs) => this.navTo(a),
|
|
"navigate.reload": (a: NavTabArgs) => this.navReload(a, false),
|
|
"navigate.hard_reload": (a: NavTabArgs) => this.navReload(a, true),
|
|
"navigate.back": (a: NavTabArgs) => this.navBack(a),
|
|
"navigate.forward": (a: NavTabArgs) => this.navForward(a),
|
|
"navigate.focus": (a: NavFocusArgs) => this.navFocus(a),
|
|
"navigate.wait": (a: NavWaitArgs) => this.navWait(a),
|
|
"navigate.open_wait": (a: NavOpenWaitArgs) => this.navOpenWait(a),
|
|
};
|
|
|
|
private async navOpen({ url, background, window: windowName, windowId: explicitWindowId, group: groupNameOrId }: NavOpenArgs) {
|
|
let windowId: number | undefined;
|
|
if (explicitWindowId != null) {
|
|
windowId = explicitWindowId;
|
|
} else if (windowName) {
|
|
const aliases = await getAliases();
|
|
const entry = Object.entries(aliases).find(([, v]) => v === windowName);
|
|
if (entry) windowId = parseInt(entry[0]);
|
|
}
|
|
const tab = await chrome.tabs.create({ url, active: !background, windowId });
|
|
if (groupNameOrId != null) {
|
|
let groupId;
|
|
try {
|
|
groupId = await resolveGroupId(groupNameOrId);
|
|
// Close any blank placeholder tabs that were created when the group was made
|
|
const groupTabs = await chrome.tabs.query({ groupId });
|
|
const placeholders = groupTabs.filter(t =>
|
|
t.id !== tab.id &&
|
|
(t.url === "chrome://newtab/" || t.url === "about:blank" || t.pendingUrl === "chrome://newtab/")
|
|
);
|
|
await chrome.tabs.group({ tabIds: [tab.id], groupId });
|
|
if (placeholders.length) await chrome.tabs.remove(placeholders.map(t => t.id));
|
|
} catch (e) {
|
|
if (!(e instanceof Error) || !e.message.startsWith("No tab group found")) throw e;
|
|
// Group doesn't exist — create it with the tab already in it
|
|
groupId = await chrome.tabs.group({ tabIds: [tab.id] });
|
|
await chrome.tabGroups.update(groupId, { title: String(groupNameOrId) });
|
|
}
|
|
}
|
|
return { id: tab.id, url: tab.url };
|
|
}
|
|
|
|
private async navTo({ tabId, url }: NavToArgs) {
|
|
const tab = await chrome.tabs.update(tabId, { url });
|
|
return { id: tab.id, url: tab.url || url };
|
|
}
|
|
|
|
private async navReload({ tabId }: NavTabArgs, bypassCache: boolean) {
|
|
const tab = tabId ? { id: tabId } : await getActiveTab();
|
|
await chrome.tabs.reload(tab.id, { bypassCache });
|
|
return { tabId: tab.id };
|
|
}
|
|
|
|
private async navBack({ tabId }: NavTabArgs) {
|
|
const tab = tabId ? { id: tabId } : await getActiveTab();
|
|
await chrome.tabs.goBack(tab.id);
|
|
return { tabId: tab.id };
|
|
}
|
|
|
|
private async navForward({ tabId }: NavTabArgs) {
|
|
const tab = tabId ? { id: tabId } : await getActiveTab();
|
|
await chrome.tabs.goForward(tab.id);
|
|
return { tabId: tab.id };
|
|
}
|
|
|
|
private async navFocus({ pattern }: NavFocusArgs) {
|
|
// If pattern is a plain integer, treat it as a tab ID
|
|
const asInt = parseInt(pattern);
|
|
let match: chrome.tabs.Tab | undefined;
|
|
if (!isNaN(asInt) && String(asInt) === String(pattern)) {
|
|
match = await chrome.tabs.get(asInt);
|
|
} else {
|
|
const all = await chrome.tabs.query({});
|
|
match = all.find(t => (t.url && t.url.includes(pattern)) || (t.pendingUrl && t.pendingUrl.includes(pattern)));
|
|
}
|
|
if (!match) return null;
|
|
await chrome.windows.update(match.windowId, { focused: true });
|
|
await chrome.tabs.update(match.id, { active: true });
|
|
return { id: match.id, url: match.url || match.pendingUrl, title: match.title };
|
|
}
|
|
|
|
private async navWait({ tabId, timeout = 30000, readyState = "complete" }: NavWaitArgs = {}) {
|
|
const tab = tabId ? { id: tabId } : await getActiveTab();
|
|
const deadline = Date.now() + timeout;
|
|
const interval = 200;
|
|
while (Date.now() < deadline) {
|
|
const t = await chrome.tabs.get(tab.id);
|
|
const currentUrl = t.url || t.pendingUrl || "";
|
|
if (isBrowserErrorUrl(currentUrl)) {
|
|
throw new Error(`Tab ${tab.id} is showing an error page while waiting for load (${currentUrl})`);
|
|
}
|
|
if (readyState === "complete" ? t.status === "complete" : t.status !== "loading") {
|
|
return tabInfo(t);
|
|
}
|
|
await new Promise(r => setTimeout(r, interval));
|
|
}
|
|
throw new Error(`Tab ${tab.id} did not reach status '${readyState}' within ${timeout}ms`);
|
|
}
|
|
|
|
private async navOpenWait({ url, timeout = 30000, background, window: windowName, group }: NavOpenWaitArgs = {}) {
|
|
const opened = await this.navOpen({ url, background, window: windowName, group });
|
|
return await this.navWait({ tabId: opened.id, timeout });
|
|
}
|
|
}
|