17
17
*/
18
18
import browser , { type Windows , type Tabs } from 'webextension-polyfill' ;
19
19
20
- import { UserAgent } from '../../user-agent' ;
21
20
import { getErrorMessage } from '../../error' ;
21
+ import { logger } from '../../logger' ;
22
22
23
23
/**
24
24
* Helper class for browser.windows API.
25
25
*/
26
26
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
+
27
41
/**
28
42
* Calls browser.windows.create with fallback to browser.tabs.create.
29
43
* In case of fallback, compatible data will be reused.
@@ -41,10 +55,7 @@ export class WindowsApi {
41
55
public static async create (
42
56
createData ?: Windows . CreateCreateDataType ,
43
57
) : 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 ( ) ) {
48
59
return browser . windows . create ( createData ) ;
49
60
}
50
61
@@ -76,4 +87,27 @@ export class WindowsApi {
76
87
return null ;
77
88
}
78
89
}
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
+ }
79
113
}
0 commit comments