Files
tab-park/test/harness/test-hook-background.js
2026-11-11 20:24:24 +01:00

66 lines
2.2 KiB
JavaScript

// Test-only background hook. Appended after common.js and background.js, so
// the real helpers (tuUnloadOtherTabs, tuUnloadActiveTab, tuSweep, ...) are in
// scope. It exists so the Selenium driver can trigger the extension from a
// normal page and read back the browser's own tab state.
async function tuTestState() {
const tabs = await browser.tabs.query({});
return tabs.map(tab => ({
id: tab.id,
index: tab.index,
windowId: tab.windowId,
url: tab.url,
title: tab.title,
active: tab.active,
pinned: tab.pinned,
discarded: tab.discarded,
audible: tab.audible,
lastAccessed: tab.lastAccessed
}));
}
browser.runtime.onMessage.addListener((message, sender) => {
if (!message || message.type !== 'tu-test') return;
const windowId = sender.tab ? sender.tab.windowId : undefined;
switch (message.cmd) {
case 'state':
return tuTestState();
case 'unload-others':
return tuUnloadOtherTabs(windowId, message.tabId);
case 'park':
return tuCreateParkingTab(windowId);
case 'unload-current':
return browser.tabs.query({ active: true, windowId })
.then(([tab]) => tab ? tuUnloadActiveTab(tab) : false);
case 'pin':
return browser.tabs.update(message.tabId, { pinned: true });
case 'unpin':
return browser.tabs.update(message.tabId, { pinned: false });
case 'activate':
return browser.tabs.update(message.tabId, { active: true });
case 'close':
return browser.tabs.remove(message.tabId);
case 'discard-all':
return browser.tabs.query({ windowId })
.then(tabs => tuDiscard(tuCandidates(tabs)));
case 'settings':
return browser.storage.local.set(message.values);
case 'sweep':
return tuSweep();
case 'alarms':
return browser.alarms.getAll();
case 'parking':
return tuParkingTabId();
case 'protect':
return tuSetProtected(message.tabId, message.value);
case 'protected':
return tuProtectedIds().then(ids => [...ids]);
case 'candidates':
return Promise.resolve(
tuCandidates(message.tabs, { skipAudible: message.skipAudible }).map(tab => tab.id)
);
case 'commands':
return browser.commands.getAll();
}
});