browser-extension-utils 0.1.0 → 0.1.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/index.d.ts +5 -5
- package/lib/index.js +18 -9
- package/lib/userscript.js +20 -11
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -7,22 +7,22 @@ export function toCamelCase(text: string): string
|
|
|
7
7
|
export function $(
|
|
8
8
|
selectors: string,
|
|
9
9
|
element?: HTMLElement | Document
|
|
10
|
-
):
|
|
10
|
+
): HTMLElement | undefined
|
|
11
11
|
|
|
12
12
|
export function querySelector(
|
|
13
13
|
selectors: string,
|
|
14
14
|
element?: HTMLElement | Document
|
|
15
|
-
):
|
|
15
|
+
): HTMLElement | undefined
|
|
16
16
|
|
|
17
17
|
export function $$(
|
|
18
18
|
selectors: string,
|
|
19
19
|
element?: HTMLElement | Document
|
|
20
|
-
):
|
|
20
|
+
): HTMLElement[]
|
|
21
21
|
|
|
22
22
|
export function querySelectorAll(
|
|
23
23
|
selectors: string,
|
|
24
24
|
element?: HTMLElement | Document
|
|
25
|
-
):
|
|
25
|
+
): HTMLElement[]
|
|
26
26
|
|
|
27
27
|
export function createElement(
|
|
28
28
|
tagName: string,
|
|
@@ -105,7 +105,7 @@ export function createSetStyle(styleText: string): SetStyle
|
|
|
105
105
|
|
|
106
106
|
export function isUrl(text: string): boolean
|
|
107
107
|
|
|
108
|
-
export function throttle(func: Function,
|
|
108
|
+
export function throttle(func: Function, interval: number): Function
|
|
109
109
|
|
|
110
110
|
export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
|
|
111
111
|
export function registerMenuCommand(
|
package/lib/index.js
CHANGED
|
@@ -195,19 +195,28 @@ export const isUrl = (text) => /^https?:\/\//.test(text)
|
|
|
195
195
|
/**
|
|
196
196
|
*
|
|
197
197
|
* @param { function } func
|
|
198
|
-
* @param { number }
|
|
198
|
+
* @param { number } interval
|
|
199
199
|
* @returns
|
|
200
200
|
*/
|
|
201
|
-
export const throttle = (func,
|
|
202
|
-
let
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
201
|
+
export const throttle = (func, interval) => {
|
|
202
|
+
let timeoutId = null
|
|
203
|
+
let next = false
|
|
204
|
+
const handler = (...args) => {
|
|
205
|
+
if (timeoutId) {
|
|
206
|
+
next = true
|
|
207
|
+
} else {
|
|
208
|
+
func.apply(this, args)
|
|
209
|
+
timeoutId = setTimeout(() => {
|
|
210
|
+
timeoutId = null
|
|
211
|
+
if (next) {
|
|
212
|
+
next = false
|
|
213
|
+
handler()
|
|
214
|
+
}
|
|
215
|
+
}, interval)
|
|
209
216
|
}
|
|
210
217
|
}
|
|
218
|
+
|
|
219
|
+
return handler
|
|
211
220
|
}
|
|
212
221
|
|
|
213
222
|
if (typeof Object.hasOwn !== "function") {
|
package/lib/userscript.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
doc,
|
|
2
3
|
setAttributes,
|
|
3
4
|
addElement as _addElement,
|
|
4
5
|
addStyle as _addStyle,
|
|
@@ -9,14 +10,6 @@ export * from "./index.js"
|
|
|
9
10
|
// eslint-disable-next-line no-unused-expressions, n/prefer-global/process
|
|
10
11
|
process.env.PLASMO_TAG === "dev" &&
|
|
11
12
|
(() => {
|
|
12
|
-
/* eslint-disable camelcase */
|
|
13
|
-
console.log(
|
|
14
|
-
typeof GM_addElement,
|
|
15
|
-
typeof GM_addStyle,
|
|
16
|
-
typeof GM_registerMenuCommand,
|
|
17
|
-
typeof GM
|
|
18
|
-
)
|
|
19
|
-
/* eslint-enable camelcase */
|
|
20
13
|
const functions = document.GMFunctions
|
|
21
14
|
if (typeof functions === "object") {
|
|
22
15
|
for (const key in functions) {
|
|
@@ -31,9 +24,20 @@ process.env.PLASMO_TAG === "dev" &&
|
|
|
31
24
|
export const addElement =
|
|
32
25
|
typeof GM_addElement === "function"
|
|
33
26
|
? (parentNode, tagName, attributes) => {
|
|
34
|
-
if (
|
|
35
|
-
|
|
27
|
+
if (!parentNode) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof parentNode === "string") {
|
|
32
|
+
attributes = tagName
|
|
33
|
+
tagName = parentNode
|
|
34
|
+
parentNode = doc.head
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (typeof tagName === "string") {
|
|
38
|
+
const element = GM_addElement(tagName)
|
|
36
39
|
setAttributes(element, attributes)
|
|
40
|
+
parentNode.append(element)
|
|
37
41
|
return element
|
|
38
42
|
}
|
|
39
43
|
|
|
@@ -51,7 +55,12 @@ export const addStyle =
|
|
|
51
55
|
|
|
52
56
|
// Only register menu on top frame
|
|
53
57
|
export const registerMenuCommand = (name, callback, accessKey) => {
|
|
54
|
-
if (window !== top
|
|
58
|
+
if (window !== top) {
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (typeof GM.registerMenuCommand !== "function") {
|
|
63
|
+
console.warn("Do not support GM.registerMenuCommand!")
|
|
55
64
|
return
|
|
56
65
|
}
|
|
57
66
|
|