browser-extension-utils 0.3.1 → 0.3.2

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 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
@@ -55,7 +55,7 @@ export const addElement =
55
55
  }
56
56
 
57
57
  try {
58
- const element = GM_addElement(tagName, attributes1)
58
+ const element = GM_addElement(tagName, attributes1 || {})
59
59
  setAttributes(element, attributes2)
60
60
  parentNode.append(element)
61
61
  return element
@@ -76,19 +76,34 @@ export const addStyle = (styleText: string): HTMLElement | undefined =>
76
76
  addElement(null, 'style', { textContent: styleText })
77
77
 
78
78
  // Only register menu on top frame
79
- export const registerMenuCommand = (
79
+ export const registerMenuCommand = async (
80
80
  name: string,
81
81
  callback: (event?: any) => void,
82
82
  options?: Parameters<typeof GM_registerMenuCommand>[2]
83
- ): any => {
83
+ ): Promise<number> => {
84
84
  if (globalThis.self !== globalThis.top) {
85
- return
85
+ return 0
86
86
  }
87
87
 
88
88
  if (typeof GM.registerMenuCommand !== 'function') {
89
89
  console.warn('Do not support GM.registerMenuCommand!')
90
- return
90
+ return 0
91
91
  }
92
92
 
93
- return GM.registerMenuCommand(name, callback, options)
93
+ try {
94
+ return await GM.registerMenuCommand(name, callback, options)
95
+ } catch (error) {
96
+ if (typeof options === 'object') {
97
+ // Don't support object options
98
+ try {
99
+ return await GM.registerMenuCommand(name, callback, options.accessKey)
100
+ } catch (error_) {
101
+ console.error('GM.registerMenuCommand error:', error_)
102
+ }
103
+ } else {
104
+ console.error('GM.registerMenuCommand error:', error)
105
+ }
106
+
107
+ return 0
108
+ }
94
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.ts",