Skip to content

Commit

Permalink
✨ Implement tab get api
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Apr 13, 2024
1 parent 5c039c6 commit 5a49146
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
26 changes: 26 additions & 0 deletions apps/extensions/lib/parent/ext-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
Expand Down
1 change: 1 addition & 0 deletions apps/extensions/lib/schemaTypes/tabs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ declare module tabs__tabs {
type TabStatus = 'loading' | 'complete'
type ApiGetterReturn = {
tabs: {
get: (tabId: number) => Promise<Tab>
query: (queryInfo: QueryInfo) => Promise<Tab[]>
remove: (tabIds: number | number[]) => unknown
update: (tabId: number, updateProperties: UpdateInfo) => unknown
Expand Down
14 changes: 14 additions & 0 deletions apps/extensions/lib/schemas/tabs.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@
}
],
"functions": [
{
"name": "get",
"type": "function",
"async": true,
"parameters": [
{
"name": "tabId",
"type": "integer"
}
],
"returns": {
"$ref": "Tab"
}
},
{
"name": "query",
"type": "function",
Expand Down
39 changes: 39 additions & 0 deletions apps/tests/integrations/extensions/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
},
)

0 comments on commit 5a49146

Please sign in to comment.