browser-extension-utils 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/global.d.ts +6 -0
- package/lib/index.ts +3 -0
- package/lib/userscript.ts +23 -7
- package/package.json +1 -1
package/lib/global.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ declare module 'css:*' {
|
|
|
3
3
|
export default cssText
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
declare const browser: typeof chrome
|
|
7
|
+
|
|
6
8
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
7
9
|
declare const GM_info: {
|
|
8
10
|
scriptHandler: string
|
|
@@ -13,6 +15,10 @@ declare function GM_addValueChangeListener(
|
|
|
13
15
|
cb: (key: string, oldValue: any, newValue: any, remote: boolean) => void
|
|
14
16
|
): number
|
|
15
17
|
|
|
18
|
+
declare type RegisterMenuCommandOptions = Parameters<
|
|
19
|
+
typeof GM_registerMenuCommand
|
|
20
|
+
>[2]
|
|
21
|
+
|
|
16
22
|
declare function GM_registerMenuCommand(
|
|
17
23
|
caption: string,
|
|
18
24
|
onClick: () => void,
|
package/lib/index.ts
CHANGED
|
@@ -13,6 +13,9 @@ export * from './dom-utils'
|
|
|
13
13
|
export * from './set-attributes'
|
|
14
14
|
export * from './create-element'
|
|
15
15
|
export * from './add-element'
|
|
16
|
+
export type RegisterMenuCommandOptions = Parameters<
|
|
17
|
+
typeof GM_registerMenuCommand
|
|
18
|
+
>[2]
|
|
16
19
|
|
|
17
20
|
export const uniq = <T>(array: T[]): T[] => [...new Set(array)]
|
|
18
21
|
|
package/lib/userscript.ts
CHANGED
|
@@ -40,7 +40,8 @@ export const addElement =
|
|
|
40
40
|
for (const entry of Object.entries(attributes)) {
|
|
41
41
|
// Some userscript managers do not support innerHTML
|
|
42
42
|
// Stay do not support multiple classes: GM_addElement('div', {"class": "a b"}). Remove `|class` when it is supported
|
|
43
|
-
|
|
43
|
+
// Stay do not support data-* attributes
|
|
44
|
+
if (/^(on\w+|innerHTML|class|data-.+)$/.test(entry[0])) {
|
|
44
45
|
entries2.push(entry)
|
|
45
46
|
} else {
|
|
46
47
|
entries1.push(entry)
|
|
@@ -55,7 +56,7 @@ export const addElement =
|
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
try {
|
|
58
|
-
const element = GM_addElement(tagName, attributes1)
|
|
59
|
+
const element = GM_addElement(tagName, attributes1 || {})
|
|
59
60
|
setAttributes(element, attributes2)
|
|
60
61
|
parentNode.append(element)
|
|
61
62
|
return element
|
|
@@ -76,19 +77,34 @@ export const addStyle = (styleText: string): HTMLElement | undefined =>
|
|
|
76
77
|
addElement(null, 'style', { textContent: styleText })
|
|
77
78
|
|
|
78
79
|
// Only register menu on top frame
|
|
79
|
-
export const registerMenuCommand = (
|
|
80
|
+
export const registerMenuCommand = async (
|
|
80
81
|
name: string,
|
|
81
82
|
callback: (event?: any) => void,
|
|
82
83
|
options?: Parameters<typeof GM_registerMenuCommand>[2]
|
|
83
|
-
):
|
|
84
|
+
): Promise<number> => {
|
|
84
85
|
if (globalThis.self !== globalThis.top) {
|
|
85
|
-
return
|
|
86
|
+
return 0
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
if (typeof GM.registerMenuCommand !== 'function') {
|
|
89
90
|
console.warn('Do not support GM.registerMenuCommand!')
|
|
90
|
-
return
|
|
91
|
+
return 0
|
|
91
92
|
}
|
|
92
93
|
|
|
93
|
-
|
|
94
|
+
try {
|
|
95
|
+
return await GM.registerMenuCommand(name, callback, options)
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (typeof options === 'object') {
|
|
98
|
+
// Don't support object options
|
|
99
|
+
try {
|
|
100
|
+
return await GM.registerMenuCommand(name, callback, options.accessKey)
|
|
101
|
+
} catch (error_) {
|
|
102
|
+
console.error('GM.registerMenuCommand error:', error_)
|
|
103
|
+
}
|
|
104
|
+
} else {
|
|
105
|
+
console.error('GM.registerMenuCommand error:', error)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return 0
|
|
109
|
+
}
|
|
94
110
|
}
|