browser-extension-utils 0.1.16 → 0.1.18

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
@@ -140,12 +140,18 @@ export function getOffsetPosition(
140
140
  // eslint-disable-next-line @typescript-eslint/ban-types
141
141
  export async function runOnce(key: string, func: Function): Promise<any>
142
142
 
143
+ // eslint-disable-next-line @typescript-eslint/ban-types
144
+ export function runWhenRootExists(func: Function): void
145
+
143
146
  // eslint-disable-next-line @typescript-eslint/ban-types
144
147
  export function runWhenHeadExists(func: Function): void
145
148
 
146
149
  // eslint-disable-next-line @typescript-eslint/ban-types
147
150
  export function runWhenBodyExists(func: Function): void
148
151
 
152
+ // eslint-disable-next-line @typescript-eslint/ban-types
153
+ export function runWhenDomReady(func: Function): void
154
+
149
155
  export async function sleep(time: number): Promise<void>
150
156
 
151
157
  export type Cache = {
package/lib/index.js CHANGED
@@ -372,6 +372,7 @@ export const parseInt10 = (number, defaultValue) => {
372
372
  return Number.isNaN(result) ? defaultValue : result
373
373
  }
374
374
 
375
+ const rootFuncArray = []
375
376
  const headFuncArray = []
376
377
  const bodyFuncArray = []
377
378
  let headBodyObserver
@@ -386,6 +387,14 @@ const startObserveHeadBodyExists = () => {
386
387
  headBodyObserver.disconnect()
387
388
  }
388
389
 
390
+ if (doc.documentElement && rootFuncArray.length > 0) {
391
+ for (const func of rootFuncArray) {
392
+ func()
393
+ }
394
+
395
+ rootFuncArray.length = 0
396
+ }
397
+
389
398
  if (doc.head && headFuncArray.length > 0) {
390
399
  for (const func of headFuncArray) {
391
400
  func()
@@ -409,6 +418,19 @@ const startObserveHeadBodyExists = () => {
409
418
  })
410
419
  }
411
420
 
421
+ /**
422
+ * Run function when document.documentElement exsits.
423
+ */
424
+ export const runWhenRootExists = (func) => {
425
+ if (!doc.documentElement) {
426
+ rootFuncArray.push(func)
427
+ startObserveHeadBodyExists()
428
+ return
429
+ }
430
+
431
+ func()
432
+ }
433
+
412
434
  /**
413
435
  * Run function when document.head exsits.
414
436
  */
@@ -435,6 +457,24 @@ export const runWhenBodyExists = (func) => {
435
457
  func()
436
458
  }
437
459
 
460
+ /**
461
+ * Equals to jQuery.domready
462
+ */
463
+ export const runWhenDomReady = (func) => {
464
+ if (doc.readyState === "interactive" || doc.readyState === "complete") {
465
+ return func()
466
+ }
467
+
468
+ const handler = () => {
469
+ if (doc.readyState === "interactive" || doc.readyState === "complete") {
470
+ func()
471
+ removeEventListener(doc, "readystatechange", handler)
472
+ }
473
+ }
474
+
475
+ addEventListener(doc, "readystatechange", handler)
476
+ }
477
+
438
478
  export const isVisible = (element) => {
439
479
  if (typeof element.checkVisibility === "function") {
440
480
  return element.checkVisibility()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -29,8 +29,8 @@
29
29
  },
30
30
  "homepage": "https://github.com/utags/browser-extension-utils#readme",
31
31
  "devDependencies": {
32
- "prettier": "^2.8.8",
33
- "xo": "^0.55.0"
32
+ "prettier": "^3.0.2",
33
+ "xo": "^0.56.0"
34
34
  },
35
35
  "files": [
36
36
  "lib/",