browser-extension-utils 0.1.9 → 0.1.11

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
@@ -76,6 +76,8 @@ export function setAttribute(
76
76
  value: string
77
77
  ): void
78
78
 
79
+ export function removeAttribute(element: HTMLElement, name: string): void
80
+
79
81
  export function setAttributes(
80
82
  element: HTMLElement,
81
83
  attributes: Record<string, unknown>
@@ -111,7 +113,7 @@ export function noStyleSpace(text: string): string
111
113
 
112
114
  export function createSetStyle(styleText: string): SetStyle
113
115
 
114
- export function isUrl(text: string): boolean
116
+ export function isUrl(text: string | undefined): boolean
115
117
 
116
118
  // eslint-disable-next-line @typescript-eslint/ban-types
117
119
  export function throttle(func: Function, interval: number): Function
@@ -138,6 +140,9 @@ export function getOffsetPosition(
138
140
  // eslint-disable-next-line @typescript-eslint/ban-types
139
141
  export function runOnce(key: string, func: Function): any
140
142
 
143
+ // eslint-disable-next-line @typescript-eslint/ban-types
144
+ export function runWhenHeadExists(func: Function): void
145
+
141
146
  // eslint-disable-next-line @typescript-eslint/ban-types
142
147
  export function runWhenBodyExists(func: Function): void
143
148
 
@@ -158,3 +163,6 @@ export function parseInt10(
158
163
  number: string | undefined,
159
164
  defaultValue?: number
160
165
  ): number
166
+
167
+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-redundant-type-constituents
168
+ export function createHTML(html: string): TrustedHTML | string
package/lib/index.js CHANGED
@@ -87,6 +87,8 @@ export const getAttribute = (element, name) =>
87
87
  element ? element.getAttribute(name) : null
88
88
  export const setAttribute = (element, name, value) =>
89
89
  element ? element.setAttribute(name, value) : undefined
90
+ export const removeAttribute = (element, name) =>
91
+ element ? element.removeAttribute(name) : undefined
90
92
 
91
93
  export const setAttributes = (element, attributes) => {
92
94
  if (element && attributes) {
@@ -348,14 +350,63 @@ export const parseInt10 = (number, defaultValue) => {
348
350
  return Number.isNaN(result) ? defaultValue : result
349
351
  }
350
352
 
353
+ const headFuncArray = []
354
+ const bodyFuncArray = []
355
+ let headBodyObserver
356
+
357
+ const startObserveHeadBodyExists = () => {
358
+ if (headBodyObserver) {
359
+ return
360
+ }
361
+
362
+ headBodyObserver = new MutationObserver(() => {
363
+ if (doc.head && doc.body) {
364
+ headBodyObserver.disconnect()
365
+ }
366
+
367
+ if (doc.head && headFuncArray.length > 0) {
368
+ for (const func of headFuncArray) {
369
+ func()
370
+ }
371
+
372
+ headFuncArray.length = 0
373
+ }
374
+
375
+ if (doc.body && bodyFuncArray.length > 0) {
376
+ for (const func of bodyFuncArray) {
377
+ func()
378
+ }
379
+
380
+ bodyFuncArray.length = 0
381
+ }
382
+ })
383
+
384
+ headBodyObserver.observe(doc, {
385
+ childList: true,
386
+ subtree: true,
387
+ })
388
+ }
389
+
351
390
  /**
352
- * Run function when document.body exsits. The function may be executed before DOMContentLoaded.
391
+ * Run function when document.head exsits.
392
+ */
393
+ export const runWhenHeadExists = (func) => {
394
+ if (!doc.head) {
395
+ headFuncArray.push(func)
396
+ startObserveHeadBodyExists()
397
+ return
398
+ }
399
+
400
+ func()
401
+ }
402
+
403
+ /**
404
+ * Run function when document.body exsits. The function executed before DOMContentLoaded.
353
405
  */
354
406
  export const runWhenBodyExists = (func) => {
355
407
  if (!doc.body) {
356
- setTimeout(() => {
357
- runWhenBodyExists(func)
358
- }, 10)
408
+ bodyFuncArray.push(func)
409
+ startObserveHeadBodyExists()
359
410
  return
360
411
  }
361
412
 
@@ -371,3 +422,15 @@ export const isVisible = (element) => {
371
422
  }
372
423
 
373
424
  export const isTouchScreen = () => "ontouchstart" in win
425
+
426
+ const escapeHTMLPolicy =
427
+ typeof trustedTypes !== "undefined" &&
428
+ typeof trustedTypes.createPolicy === "function"
429
+ ? trustedTypes.createPolicy("beuEscapePolicy", {
430
+ createHTML: (string) => string,
431
+ })
432
+ : undefined
433
+
434
+ export const createHTML = (html) => {
435
+ return escapeHTMLPolicy ? escapeHTMLPolicy.createHTML(html) : html
436
+ }
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.11",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -48,6 +48,8 @@
48
48
  "GM_addElement",
49
49
  "GM_addStyle",
50
50
  "GM_registerMenuCommand",
51
+ "trustedTypes",
52
+ "MutationObserver",
51
53
  "history",
52
54
  "window",
53
55
  "top",