Skip to content

Commit

Permalink
✨ Implement forward and back
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Apr 13, 2024
1 parent 6e89242 commit eece6a4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 1 deletion.
55 changes: 55 additions & 0 deletions apps/extensions/lib/parent/ext-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,61 @@ this.tabs = class extends ExtensionAPIPersistent {
return serialize(extension)([tab, window])
},

async goBack(tabId) {
let tab

if (tabId) {
tab = await get(tabId).then((all) => all.tab)
} else {
tab = lazy.WindowTracker.getActiveWindow()?.activeTab()
if (!tab) {
return
}
}
const complete = new Promise((res) => {
/** @param {boolean} isLoading */
function complete(isLoading) {
if (isLoading) {
return
}
tab.view.events.off('loadingChange', complete)
res(undefined)
}

tab.view.events.on('loadingChange', complete)
})
tab.view.browser.goBack()
return complete
},

async goForward(tabId) {
let tab

if (tabId) {
tab = await get(tabId).then((all) => all.tab)
} else {
tab = lazy.WindowTracker.getActiveWindow()?.activeTab()
if (!tab) {
return
}
}

const complete = new Promise((res) => {
/** @param {boolean} isLoading */
function complete(isLoading) {
if (isLoading) {
return
}
tab.view.events.off('loadingChange', complete)
res(undefined)
}

tab.view.events.on('loadingChange', complete)
})
tab.view.browser.goForward()
return complete
},

async query(queryInfo) {
return query(queryInfo).map(serialize(extension))
},
Expand Down
2 changes: 2 additions & 0 deletions apps/extensions/lib/schemaTypes/tabs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ declare module tabs__tabs {
type ApiGetterReturn = {
tabs: {
get: (tabId: number) => Promise<Tab>
goBack: (tabId?: number) => unknown
goForward: (tabId?: number) => unknown
query: (queryInfo: QueryInfo) => Promise<Tab[]>
remove: (tabIds: number | number[]) => unknown
reload: (tabIds: number | number[]) => unknown
Expand Down
12 changes: 12 additions & 0 deletions apps/extensions/lib/schemas/tabs.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@
"$ref": "Tab"
}
},
{
"name": "goBack",
"type": "function",
"async": true,
"parameters": [{ "name": "tabId", "type": "integer", "optional": true }]
},
{
"name": "goForward",
"type": "function",
"async": true,
"parameters": [{ "name": "tabId", "type": "integer", "optional": true }]
},
{
"name": "query",
"type": "function",
Expand Down
2 changes: 1 addition & 1 deletion apps/extensions/scripts/buildTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function generateTypeNode(type) {
undefined,
undefined,
factory.createIdentifier(param.name),
typeof type.optional !== 'undefined' && type.optional
typeof param.optional !== 'undefined' && param.optional
? QUESTION_TOKEN
: undefined,
generateTypeNode(param),
Expand Down
58 changes: 58 additions & 0 deletions apps/tests/integrations/extensions/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,63 @@ await TestManager.withBrowser(
.then((e) => e.awaitMsg('done'))
.then((e) => e.unload())
})

await TestManager.test('tabs - Navigation', 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,
})

// History stack already setup on the tab at index 0, so we can
// use that here instead. Its probibly going to cause flaky tests
// in the future, but it stops spawning a new window

b.test.assertEq(
'about:blank',
windowResults[0].url,
'Previous tests should leave as about:blank',
)

await b.tabs.goBack(windowResults[0].id)

b.test.assertEq(
'https://example.com/',
await b.tabs.get(windowResults[0].id || -1).then((t) => t.url),
'New url should be last page',
)

await b.tabs.goForward(windowResults[0].id)

b.test.assertEq(
'about:blank',
await b.tabs.get(windowResults[0].id || -1).then((t) => t.url),
'New url should be next page',
)

b.test.sendMessage('done')
})
},
},
test,
)

await extension
.testCount(3)
.startup()
.then((e) => e.sendMsg(window.windowId.toString()))
.then((e) => e.awaitMsg('done'))
.then((e) => e.unload())
})
},
)

0 comments on commit eece6a4

Please sign in to comment.