browser-extension-utils 0.1.3 → 0.1.5
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 +22 -0
- package/lib/index.js +69 -3
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export const doc: Document
|
|
2
2
|
|
|
3
|
+
export const win: Window
|
|
4
|
+
|
|
3
5
|
export function uniq(array: any[]): any[]
|
|
4
6
|
|
|
5
7
|
export function toCamelCase(text: string): string
|
|
@@ -111,6 +113,7 @@ export function createSetStyle(styleText: string): SetStyle
|
|
|
111
113
|
|
|
112
114
|
export function isUrl(text: string): boolean
|
|
113
115
|
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
114
117
|
export function throttle(func: Function, interval: number): Function
|
|
115
118
|
|
|
116
119
|
export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
|
|
@@ -131,3 +134,22 @@ export function getOffsetPosition(
|
|
|
131
134
|
top: number
|
|
132
135
|
left: number
|
|
133
136
|
}
|
|
137
|
+
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
139
|
+
export function runOnce(key: string, func: Function): any
|
|
140
|
+
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
142
|
+
export function runWhenBodyExists(func: Function): void
|
|
143
|
+
|
|
144
|
+
export async function sleep(time: number): void
|
|
145
|
+
|
|
146
|
+
export type Cache = {
|
|
147
|
+
get: (key: string | any[]) => any
|
|
148
|
+
add: (key: string | any[], value: any) => void
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const cache: Cache
|
|
152
|
+
|
|
153
|
+
export function isVisible(element: HTMLElement): boolean
|
|
154
|
+
|
|
155
|
+
export function parseInt10(number: string, defaultValue?: number): number
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export const doc = document
|
|
2
2
|
|
|
3
|
+
export const win = window
|
|
4
|
+
|
|
3
5
|
export const uniq = (array) => [...new Set(array)]
|
|
4
6
|
|
|
5
7
|
export const toCamelCase = function (text) {
|
|
@@ -122,7 +124,7 @@ export const addAttribute = (element, name, value) => {
|
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
export const addClass = (element, className) => {
|
|
125
|
-
if (!element) {
|
|
127
|
+
if (!element || !element.classList) {
|
|
126
128
|
return
|
|
127
129
|
}
|
|
128
130
|
|
|
@@ -130,7 +132,7 @@ export const addClass = (element, className) => {
|
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
export const removeClass = (element, className) => {
|
|
133
|
-
if (!element) {
|
|
135
|
+
if (!element || !element.classList) {
|
|
134
136
|
return
|
|
135
137
|
}
|
|
136
138
|
|
|
@@ -138,7 +140,7 @@ export const removeClass = (element, className) => {
|
|
|
138
140
|
}
|
|
139
141
|
|
|
140
142
|
export const hasClass = (element, className) => {
|
|
141
|
-
if (!element) {
|
|
143
|
+
if (!element || !element.classList) {
|
|
142
144
|
return false
|
|
143
145
|
}
|
|
144
146
|
|
|
@@ -170,6 +172,7 @@ export const setStyle = (element, values, overwrite) => {
|
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
// convert `font-size: 12px; color: red` to `{"fontSize": "12px"; "color": "red"}`
|
|
175
|
+
// eslint-disable-next-line no-unused-vars
|
|
173
176
|
const toStyleKeyValues = (styleText) => {
|
|
174
177
|
const result = {}
|
|
175
178
|
const keyValues = styleText.split(/\s*;\s*/)
|
|
@@ -294,3 +297,66 @@ export const getOffsetPosition = (element, referElement) => {
|
|
|
294
297
|
|
|
295
298
|
return position
|
|
296
299
|
}
|
|
300
|
+
|
|
301
|
+
const runOnceCache = {}
|
|
302
|
+
export const runOnce = (key, func) => {
|
|
303
|
+
if (!key) {
|
|
304
|
+
return func()
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (Object.hasOwn(runOnceCache, key)) {
|
|
308
|
+
return runOnceCache[key]
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const result = func()
|
|
312
|
+
runOnceCache[key] = result
|
|
313
|
+
return result
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const cacheStore = {}
|
|
317
|
+
const makeKey = (key /* string | any[] */) =>
|
|
318
|
+
Array.isArray(key) ? key.join(":") : key
|
|
319
|
+
export const cache = {
|
|
320
|
+
get: (key /* string | any[] */) => cacheStore[makeKey(key)],
|
|
321
|
+
add(key /* string | any[] */, value /* any */) {
|
|
322
|
+
cacheStore[makeKey(key)] = value
|
|
323
|
+
},
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export const sleep = async (time) => {
|
|
327
|
+
return new Promise((resolve) => {
|
|
328
|
+
setTimeout(() => {
|
|
329
|
+
resolve(1)
|
|
330
|
+
}, time)
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
export const parseInt10 = (number, defaultValue) => {
|
|
335
|
+
if (!number) {
|
|
336
|
+
number = ""
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return Number.parseInt(number, 10) || defaultValue || Number.NaN
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Run function when document.body exsits. The function may be executed before DOMContentLoaded.
|
|
344
|
+
*/
|
|
345
|
+
export const runWhenBodyExists = (func) => {
|
|
346
|
+
if (!doc.body) {
|
|
347
|
+
setTimeout(() => {
|
|
348
|
+
runWhenBodyExists(func)
|
|
349
|
+
}, 10)
|
|
350
|
+
return
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
func()
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
export const isVisible = (element) => {
|
|
357
|
+
if (typeof element.checkVisibility === "function") {
|
|
358
|
+
return element.checkVisibility()
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return element.offsetParent !== null
|
|
362
|
+
}
|