-
Notifications
You must be signed in to change notification settings - Fork 860
Handle failures when opening extension tabs #1015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PeterDaveHello
wants to merge
1
commit into
ChatGPTBox-dev:master
Choose a base branch
from
PeterDaveHello:fix-open-url-rejection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,16 @@ | ||
| import Browser from 'webextension-polyfill' | ||
|
|
||
| export function openUrl(url) { | ||
| Browser.tabs.query({ url, currentWindow: true }).then((tabs) => { | ||
| if (tabs.length > 0) { | ||
| Browser.tabs.update(tabs[0].id, { active: true }) | ||
| } else { | ||
| Browser.tabs.create({ url }) | ||
| } | ||
| }) | ||
| return Browser.tabs | ||
| .query({ url, currentWindow: true }) | ||
| .then(async (tabs) => { | ||
| if (tabs.length > 0) { | ||
| await Browser.tabs.update(tabs[0].id, { active: true }) | ||
| } else { | ||
| await Browser.tabs.create({ url }) | ||
| } | ||
| }) | ||
| .catch((error) => { | ||
| console.error('failed to open url', error) | ||
| }) | ||
| } | ||
|
PeterDaveHello marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { test } from 'node:test' | ||
| import Browser from 'webextension-polyfill' | ||
| import { openUrl } from '../../../src/utils/open-url.mjs' | ||
|
|
||
| const overrideTabs = (t, methods) => { | ||
| for (const [name, value] of Object.entries(methods)) { | ||
| const originalDescriptor = Object.getOwnPropertyDescriptor(Browser.tabs, name) | ||
| Object.defineProperty(Browser.tabs, name, { | ||
| value, | ||
| writable: true, | ||
| configurable: true, | ||
| }) | ||
| t.after(() => { | ||
| if (originalDescriptor) { | ||
| Object.defineProperty(Browser.tabs, name, originalDescriptor) | ||
| } else { | ||
| delete Browser.tabs[name] | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| test('openUrl activates an existing matching tab', async (t) => { | ||
| const queryCalls = [] | ||
| const updateCalls = [] | ||
| let createCalled = false | ||
| overrideTabs(t, { | ||
| query: async (queryInfo) => { | ||
| queryCalls.push(queryInfo) | ||
| return [{ id: 42 }] | ||
| }, | ||
| update: async (...args) => { | ||
| updateCalls.push(args) | ||
| }, | ||
| create: async () => { | ||
| createCalled = true | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(await openUrl('https://example.com'), undefined) | ||
| assert.deepEqual(queryCalls, [{ url: 'https://example.com', currentWindow: true }]) | ||
| assert.deepEqual(updateCalls, [[42, { active: true }]]) | ||
| assert.equal(createCalled, false) | ||
| }) | ||
|
|
||
| test('openUrl creates a tab when no existing tab matches', async (t) => { | ||
| let updateCalled = false | ||
| const createCalls = [] | ||
| overrideTabs(t, { | ||
| query: async () => [], | ||
| update: async () => { | ||
| updateCalled = true | ||
| }, | ||
| create: async (...args) => { | ||
| createCalls.push(args) | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(await openUrl('https://example.com'), undefined) | ||
| assert.equal(updateCalled, false) | ||
| assert.deepEqual(createCalls, [[{ url: 'https://example.com' }]]) | ||
| }) | ||
|
|
||
| test('openUrl logs a tabs.query failure and fulfills', async (t) => { | ||
| const error = new Error('query failed') | ||
| let updateCalled = false | ||
| let createCalled = false | ||
| const loggedErrors = [] | ||
| t.mock.method(console, 'error', (...args) => { | ||
| loggedErrors.push(args) | ||
| }) | ||
| overrideTabs(t, { | ||
| query: async () => { | ||
| throw error | ||
| }, | ||
| update: async () => { | ||
| updateCalled = true | ||
| }, | ||
| create: async () => { | ||
| createCalled = true | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(await openUrl('https://example.com'), undefined) | ||
| assert.deepEqual(loggedErrors, [['failed to open url', error]]) | ||
| assert.equal(updateCalled, false) | ||
| assert.equal(createCalled, false) | ||
| }) | ||
|
|
||
| test('openUrl logs a tabs.update failure and fulfills', async (t) => { | ||
| const error = new Error('update failed') | ||
| const loggedErrors = [] | ||
| t.mock.method(console, 'error', (...args) => { | ||
| loggedErrors.push(args) | ||
| }) | ||
| overrideTabs(t, { | ||
| query: async () => [{ id: 42 }], | ||
| update: async () => { | ||
| throw error | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(await openUrl('https://example.com'), undefined) | ||
| assert.deepEqual(loggedErrors, [['failed to open url', error]]) | ||
| }) | ||
|
|
||
| test('openUrl logs a tabs.create failure and fulfills', async (t) => { | ||
| const error = new Error('create failed') | ||
| const loggedErrors = [] | ||
| t.mock.method(console, 'error', (...args) => { | ||
| loggedErrors.push(args) | ||
| }) | ||
| overrideTabs(t, { | ||
| query: async () => [], | ||
| create: async () => { | ||
| throw error | ||
| }, | ||
| }) | ||
|
|
||
| assert.equal(await openUrl('https://example.com'), undefined) | ||
| assert.deepEqual(loggedErrors, [['failed to open url', error]]) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.