Skip to content

Commit

Permalink
fix: get correct active tab when tab ID not specified
Browse files Browse the repository at this point in the history
relates to #79
  • Loading branch information
samuelmaddock committed Jun 3, 2023
1 parent 1a3ca8b commit 3673cc8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
83 changes: 81 additions & 2 deletions packages/electron-chrome-extensions/spec/chrome-tabs-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ describe('chrome.tabs', () => {
})

describe('getCurrent()', () => {
it('fails to get the active tab from a non-tab context', async () => {
it('gets details of the active tab', async () => {
const result = await browser.crx.exec('tabs.getCurrent')
expect(result).to.not.be.an('object')
expect(result).to.be.an('object')
})
})

Expand Down Expand Up @@ -128,6 +128,21 @@ describe('chrome.tabs', () => {
})
})

describe('reload()', () => {
it('reloads the active tab', async () => {
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.reload')
await navigatePromise
})

it('reloads a specified tab', async () => {
const tabId = browser.window.webContents.id
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.reload', tabId)
await navigatePromise
})
})

describe('update()', () => {
it('navigates the tab', async () => {
const tabId = browser.window.webContents.id
Expand All @@ -138,13 +153,77 @@ describe('chrome.tabs', () => {
expect(browser.window.webContents.getURL()).to.equal(updateUrl)
})

it('navigates the active tab', async () => {
const updateUrl = `${server.getUrl()}foo`
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.update', { url: updateUrl })
await navigatePromise
expect(browser.window.webContents.getURL()).to.equal(updateUrl)
})

it('fails on chrome:// URLs', async () => {
const tabId = browser.webContents.id
const tabInfo = await browser.crx.exec('tabs.update', tabId, { url: 'chrome://kill' })
expect(tabInfo).to.be.a('null')
})
})

describe('goForward()', () => {
it('navigates the active tab forward', async () => {
const initialUrl = browser.window.webContents.getURL()
const targetUrl = `${server.getUrl()}foo`
await browser.window.webContents.loadURL(targetUrl)
expect(browser.window.webContents.canGoBack()).to.be.true
browser.window.webContents.goBack()
await emittedOnce(browser.window.webContents, 'did-navigate')
expect(browser.window.webContents.canGoForward()).to.be.true
expect(browser.window.webContents.getURL()).to.equal(initialUrl)
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.goForward')
await navigatePromise
expect(browser.window.webContents.getURL()).to.equal(targetUrl)
})

it('navigates a specified tab forward', async () => {
const tabId = browser.window.webContents.id
const initialUrl = browser.window.webContents.getURL()
const targetUrl = `${server.getUrl()}foo`
await browser.window.webContents.loadURL(targetUrl)
expect(browser.window.webContents.canGoBack()).to.be.true
browser.window.webContents.goBack()
await emittedOnce(browser.window.webContents, 'did-navigate')
expect(browser.window.webContents.canGoForward()).to.be.true
expect(browser.window.webContents.getURL()).to.equal(initialUrl)
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.goForward', tabId)
await navigatePromise
expect(browser.window.webContents.getURL()).to.equal(targetUrl)
})
})

describe('goBack()', () => {
it('navigates the active tab back', async () => {
const initialUrl = browser.window.webContents.getURL()
await browser.window.webContents.loadURL(`${server.getUrl()}foo`)
expect(browser.window.webContents.canGoBack()).to.be.true
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.goBack')
await navigatePromise
expect(browser.window.webContents.getURL()).to.equal(initialUrl)
})

it('navigates a specified tab back', async () => {
const tabId = browser.window.webContents.id
const initialUrl = browser.window.webContents.getURL()
await browser.window.webContents.loadURL(`${server.getUrl()}foo`)
expect(browser.window.webContents.canGoBack()).to.be.true
const navigatePromise = emittedOnce(browser.window.webContents, 'did-navigate')
browser.crx.exec('tabs.goBack', tabId)
await navigatePromise
expect(browser.window.webContents.getURL()).to.equal(initialUrl)
})
})

describe('executeScript()', () => {
it('injects code into a tab', async () => {
const tabId = browser.window.webContents.id
Expand Down
1 change: 1 addition & 0 deletions packages/electron-chrome-extensions/spec/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const useServer = () => {

before(async () => {
server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(emptyPage)
})
await new Promise<void>((resolve) =>
Expand Down
11 changes: 5 additions & 6 deletions packages/electron-chrome-extensions/src/browser/api/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class TabsAPI {
}

private getCurrent(event: ExtensionEvent) {
const tab = this.ctx.store.getActiveTabFromWebContents(event.sender)
const tab = this.ctx.store.getActiveTabOfCurrentWindow()
return tab ? this.getTabDetails(tab) : undefined
}

Expand Down Expand Up @@ -236,8 +236,7 @@ export class TabsAPI {

const tab = tabId
? this.ctx.store.getTabById(tabId)
: this.ctx.store.getActiveTabFromWebContents(event.sender)

: this.ctx.store.getActiveTabOfCurrentWindow()
if (!tab) return

if (reloadProperties?.bypassCache) {
Expand All @@ -254,7 +253,7 @@ export class TabsAPI {

const tab = tabId
? this.ctx.store.getTabById(tabId)
: this.ctx.store.getActiveTabFromWebContents(event.sender)
: this.ctx.store.getActiveTabOfCurrentWindow()
if (!tab) return

tabId = tab.id
Expand Down Expand Up @@ -287,7 +286,7 @@ export class TabsAPI {
const tabId = typeof arg1 === 'number' ? arg1 : undefined
const tab = tabId
? this.ctx.store.getTabById(tabId)
: this.ctx.store.getActiveTabFromWebContents(event.sender)
: this.ctx.store.getActiveTabOfCurrentWindow()
if (!tab) return
tab.goForward()
}
Expand All @@ -296,7 +295,7 @@ export class TabsAPI {
const tabId = typeof arg1 === 'number' ? arg1 : undefined
const tab = tabId
? this.ctx.store.getTabById(tabId)
: this.ctx.store.getActiveTabFromWebContents(event.sender)
: this.ctx.store.getActiveTabOfCurrentWindow()
if (!tab) return
tab.goBack()
}
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-chrome-extensions/src/browser/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export class ExtensionStore extends EventEmitter {

getActiveTabFromWebContents(wc: Electron.WebContents): Electron.WebContents | undefined {
const win = this.tabToWindow.get(wc) || BrowserWindow.fromWebContents(wc)
return win ? this.getActiveTabFromWindow(win) : undefined
const activeTab = win ? this.getActiveTabFromWindow(win) : undefined
return activeTab
}

getActiveTabOfCurrentWindow() {
Expand Down

0 comments on commit 3673cc8

Please sign in to comment.