diff --git a/apps/extensions/lib/parent/ext-tabs.js b/apps/extensions/lib/parent/ext-tabs.js index 3cc0bd1..9b394de 100644 --- a/apps/extensions/lib/parent/ext-tabs.js +++ b/apps/extensions/lib/parent/ext-tabs.js @@ -86,6 +86,32 @@ this.tabs = class extends ExtensionAPIPersistent { return { tabs: { + async get(tabId) { + const window = [ + ...lazy.WindowTracker.registeredWindows.values(), + ].find((window) => + window.windowTabs().some((tab) => tab.view.browserId === tabId), + ) + + if (!window) { + return Promise.reject({ + message: `Cannot find tab matching the id ${tabId}`, + }) + } + + const tab = window + .windowTabs() + .find((tab) => tab.view.browserId === tabId) + + if (!tab) { + return Promise.reject({ + message: `Cannot find tab matching the id ${tabId}`, + }) + } + + return serialize(extension)([tab, window]) + }, + async query(queryInfo) { return query(queryInfo).map(serialize(extension)) }, diff --git a/apps/extensions/lib/schemaTypes/tabs.d.ts b/apps/extensions/lib/schemaTypes/tabs.d.ts index e9df212..643dfce 100644 --- a/apps/extensions/lib/schemaTypes/tabs.d.ts +++ b/apps/extensions/lib/schemaTypes/tabs.d.ts @@ -33,6 +33,7 @@ declare module tabs__tabs { type TabStatus = 'loading' | 'complete' type ApiGetterReturn = { tabs: { + get: (tabId: number) => Promise query: (queryInfo: QueryInfo) => Promise remove: (tabIds: number | number[]) => unknown update: (tabId: number, updateProperties: UpdateInfo) => unknown diff --git a/apps/extensions/lib/schemas/tabs.json b/apps/extensions/lib/schemas/tabs.json index 4f443e7..45063db 100644 --- a/apps/extensions/lib/schemas/tabs.json +++ b/apps/extensions/lib/schemas/tabs.json @@ -106,6 +106,20 @@ } ], "functions": [ + { + "name": "get", + "type": "function", + "async": true, + "parameters": [ + { + "name": "tabId", + "type": "integer" + } + ], + "returns": { + "$ref": "Tab" + } + }, { "name": "query", "type": "function", diff --git a/apps/tests/integrations/extensions/tabs.mjs b/apps/tests/integrations/extensions/tabs.mjs index b76c115..2ce99ac 100644 --- a/apps/tests/integrations/extensions/tabs.mjs +++ b/apps/tests/integrations/extensions/tabs.mjs @@ -306,5 +306,44 @@ await TestManager.withBrowser( .then((e) => e.awaitMsg('done')) .then((e) => e.unload()) }) + + await TestManager.test('tabs - Get', async (test) => { + const extension = ExtensionTestUtils.loadExtension( + { + manifest: { + permissions: ['tabs'], + }, + async background() { + /** @type {import('resource://app/modules/ExtensionTestUtils.sys.mjs').TestBrowser} */ + const b = this.browser + + b.test.onMessage.addListener(async (msg) => { + const windowId = Number(msg) + + let windowResults = await b.tabs.query({ + windowId, + }) + const tab = await b.tabs.get(windowResults[0].id || -1) + + b.test.assertEq( + JSON.stringify(windowResults[0]), + JSON.stringify(tab), + 'Fetch result should be the same as the query result', + ) + + b.test.sendMessage('done') + }) + }, + }, + test, + ) + + await extension + .testCount(1) + .startup() + .then((e) => e.sendMsg(window.windowId.toString())) + .then((e) => e.awaitMsg('done')) + .then((e) => e.unload()) + }) }, )