browser-extension-utils 0.1.4 → 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 +16 -0
- package/lib/index.js +48 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -137,3 +137,19 @@ export function getOffsetPosition(
|
|
|
137
137
|
|
|
138
138
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
139
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
|
@@ -312,3 +312,51 @@ export const runOnce = (key, func) => {
|
|
|
312
312
|
runOnceCache[key] = result
|
|
313
313
|
return result
|
|
314
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
|
+
}
|