Skip to content

Commit 1f26662

Browse files
committed
improve WindowsApi
1 parent 7c05b14 commit 1f26662

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

Extension/src/common/api/extension/tabs.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import browser, { type Tabs } from 'webextension-polyfill';
1919

2020
import { Prefs } from '../../../background/prefs';
21-
import { UserAgent } from '../../user-agent';
21+
22+
import { WindowsApi } from './windows';
2223

2324
/**
2425
* Helper class for browser.tabs API.
@@ -47,17 +48,7 @@ export class TabsApi {
4748

4849
await browser.tabs.update(id, { active: true });
4950

50-
// browser.windows is not available in Firefox for Android
51-
// TODO: check how it works in Edge on Android
52-
if (UserAgent.isFirefox && (await UserAgent.getIsAndroid())) {
53-
return;
54-
}
55-
56-
if (!windowId) {
57-
return;
58-
}
59-
60-
await browser.windows.update(windowId, { focused: true });
51+
await WindowsApi.update(windowId, { focused: true });
6152
}
6253

6354
/**

Extension/src/common/api/extension/windows.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@
1717
*/
1818
import browser, { type Windows, type Tabs } from 'webextension-polyfill';
1919

20-
import { UserAgent } from '../../user-agent';
2120
import { getErrorMessage } from '../../error';
21+
import { logger } from '../../logger';
2222

2323
/**
2424
* Helper class for browser.windows API.
2525
*/
2626
export class WindowsApi {
27+
/**
28+
* Checks if browser.windows API is supported.
29+
*
30+
* Do not use browser.windows API if it is not supported,
31+
* for example on Android: not supported in Firefox and does not work in Edge.
32+
*
33+
* @returns True if browser.windows API is supported, false otherwise.
34+
*/
35+
private static isSupported() {
36+
return !!browser.windows
37+
&& typeof browser.windows.update === 'function'
38+
&& typeof browser.windows.create === 'function';
39+
}
40+
2741
/**
2842
* Calls browser.windows.create with fallback to browser.tabs.create.
2943
* In case of fallback, compatible data will be reused.
@@ -41,10 +55,7 @@ export class WindowsApi {
4155
public static async create(
4256
createData?: Windows.CreateCreateDataType,
4357
): Promise<Windows.Window | Tabs.Tab | null> {
44-
const isAndroid = await UserAgent.getIsAndroid();
45-
46-
// Do not use browser.windows API on Android, as it is not supported (Firefox) / does not work (Edge).
47-
if (browser.windows && !isAndroid) {
58+
if (WindowsApi.isSupported()) {
4859
return browser.windows.create(createData);
4960
}
5061

@@ -76,4 +87,27 @@ export class WindowsApi {
7687
return null;
7788
}
7889
}
90+
91+
/**
92+
* Updates the properties of a window with specified ID.
93+
*
94+
* @param windowId Window ID. May be undefined.
95+
* @param updateInfo Update info.
96+
*/
97+
public static async update(
98+
windowId: number | undefined,
99+
updateInfo: Windows.UpdateUpdateInfoType,
100+
): Promise<void> {
101+
if (!windowId) {
102+
logger.debug('windowId is not specified');
103+
return;
104+
}
105+
106+
if (!WindowsApi.isSupported()) {
107+
logger.debug('browser.windows API is not supported');
108+
return;
109+
}
110+
111+
await browser.windows.update(windowId, updateInfo);
112+
}
79113
}

0 commit comments

Comments
 (0)