browser-extension-utils 0.1.9 → 0.1.10

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
@@ -111,7 +111,7 @@ export function noStyleSpace(text: string): string
111
111
 
112
112
  export function createSetStyle(styleText: string): SetStyle
113
113
 
114
- export function isUrl(text: string): boolean
114
+ export function isUrl(text: string | undefined): boolean
115
115
 
116
116
  // eslint-disable-next-line @typescript-eslint/ban-types
117
117
  export function throttle(func: Function, interval: number): Function
@@ -138,6 +138,9 @@ export function getOffsetPosition(
138
138
  // eslint-disable-next-line @typescript-eslint/ban-types
139
139
  export function runOnce(key: string, func: Function): any
140
140
 
141
+ // eslint-disable-next-line @typescript-eslint/ban-types
142
+ export function runWhenHeadExists(func: Function): void
143
+
141
144
  // eslint-disable-next-line @typescript-eslint/ban-types
142
145
  export function runWhenBodyExists(func: Function): void
143
146
 
package/lib/index.js CHANGED
@@ -348,14 +348,63 @@ export const parseInt10 = (number, defaultValue) => {
348
348
  return Number.isNaN(result) ? defaultValue : result
349
349
  }
350
350
 
351
+ const headFuncArray = []
352
+ const bodyFuncArray = []
353
+ let headBodyObserver
354
+
355
+ const startObserveHeadBodyExists = () => {
356
+ if (headBodyObserver) {
357
+ return
358
+ }
359
+
360
+ headBodyObserver = new MutationObserver(() => {
361
+ if (doc.head && doc.body) {
362
+ headBodyObserver.disconnect()
363
+ }
364
+
365
+ if (doc.head && headFuncArray.length > 0) {
366
+ for (const func of headFuncArray) {
367
+ func()
368
+ }
369
+
370
+ headFuncArray.length = 0
371
+ }
372
+
373
+ if (doc.body && bodyFuncArray.length > 0) {
374
+ for (const func of bodyFuncArray) {
375
+ func()
376
+ }
377
+
378
+ bodyFuncArray.length = 0
379
+ }
380
+ })
381
+
382
+ headBodyObserver.observe(doc, {
383
+ childList: true,
384
+ subtree: true,
385
+ })
386
+ }
387
+
351
388
  /**
352
- * Run function when document.body exsits. The function may be executed before DOMContentLoaded.
389
+ * Run function when document.head exsits.
390
+ */
391
+ export const runWhenHeadExists = (func) => {
392
+ if (!doc.head) {
393
+ headFuncArray.push(func)
394
+ startObserveHeadBodyExists()
395
+ return
396
+ }
397
+
398
+ func()
399
+ }
400
+
401
+ /**
402
+ * Run function when document.body exsits. The function executed before DOMContentLoaded.
353
403
  */
354
404
  export const runWhenBodyExists = (func) => {
355
405
  if (!doc.body) {
356
- setTimeout(() => {
357
- runWhenBodyExists(func)
358
- }, 10)
406
+ bodyFuncArray.push(func)
407
+ startObserveHeadBodyExists()
359
408
  return
360
409
  }
361
410
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -48,6 +48,7 @@
48
48
  "GM_addElement",
49
49
  "GM_addStyle",
50
50
  "GM_registerMenuCommand",
51
+ "MutationObserver",
51
52
  "history",
52
53
  "window",
53
54
  "top",