browser-extension-utils 0.1.0 → 0.1.1

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 CHANGED
@@ -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, delay: number): 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 } delay
198
+ * @param { number } interval
199
199
  * @returns
200
200
  */
201
- export const throttle = (func, delay) => {
202
- let timer = null
203
- return function (...args) {
204
- if (!timer) {
205
- timer = setTimeout(() => {
206
- func.apply(this, args)
207
- timer = null
208
- }, delay)
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 (typeof parentNode === "string" || typeof tagName === "string") {
35
- const element = GM_addElement(parentNode, tagName)
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 || !GM || !GM.registerMenuCommand) {
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",