browser-extension-utils 0.1.14 → 0.1.16

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
@@ -138,7 +138,7 @@ export function getOffsetPosition(
138
138
  }
139
139
 
140
140
  // eslint-disable-next-line @typescript-eslint/ban-types
141
- export function runOnce(key: string, func: Function): any
141
+ export async function runOnce(key: string, func: Function): Promise<any>
142
142
 
143
143
  // eslint-disable-next-line @typescript-eslint/ban-types
144
144
  export function runWhenHeadExists(func: Function): void
package/lib/index.js CHANGED
@@ -26,18 +26,29 @@ export const $$ = (selectors, element) => [
26
26
  export const querySelector = $
27
27
  export const querySelectorAll = $$
28
28
 
29
+ export const getRootElement = (type) =>
30
+ type === 1
31
+ ? doc.head || doc.body || doc.documentElement
32
+ : type === 2
33
+ ? doc.body || doc.documentElement
34
+ : doc.documentElement
35
+
29
36
  export const createElement = (tagName, attributes) =>
30
37
  setAttributes(doc.createElement(tagName), attributes)
31
38
 
32
39
  export const addElement = (parentNode, tagName, attributes) => {
33
- if (!parentNode) {
40
+ if (typeof parentNode === "string") {
41
+ return addElement(null, parentNode, tagName)
42
+ }
43
+
44
+ if (!tagName) {
34
45
  return
35
46
  }
36
47
 
37
- if (typeof parentNode === "string") {
38
- attributes = tagName
39
- tagName = parentNode
40
- parentNode = doc.head
48
+ if (!parentNode) {
49
+ parentNode = /^(script|link|style|meta)$/.test(tagName)
50
+ ? getRootElement(1)
51
+ : getRootElement(2)
41
52
  }
42
53
 
43
54
  if (typeof tagName === "string") {
@@ -54,7 +65,7 @@ export const addElement = (parentNode, tagName, attributes) => {
54
65
 
55
66
  export const addStyle = (styleText) => {
56
67
  const element = createElement("style", { textContent: styleText })
57
- doc.head.append(element)
68
+ getRootElement(1).append(element)
58
69
  return element
59
70
  }
60
71
 
@@ -312,17 +323,17 @@ export const getOffsetPosition = (element, referElement) => {
312
323
  }
313
324
 
314
325
  const runOnceCache = {}
315
- export const runOnce = (key, func) => {
316
- if (!key) {
317
- return func()
318
- }
319
-
326
+ export const runOnce = async (key, func) => {
320
327
  if (Object.hasOwn(runOnceCache, key)) {
321
328
  return runOnceCache[key]
322
329
  }
323
330
 
324
- const result = func()
325
- runOnceCache[key] = result
331
+ const result = await func()
332
+
333
+ if (key) {
334
+ runOnceCache[key] = result
335
+ }
336
+
326
337
  return result
327
338
  }
328
339
 
package/lib/userscript.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import {
2
- doc,
2
+ getRootElement,
3
3
  setAttributes,
4
4
  addElement as _addElement,
5
- addStyle as _addStyle,
6
5
  } from "./index.js"
7
6
 
8
7
  export * from "./index.js"
@@ -24,19 +23,39 @@ process.env.PLASMO_TAG === "dev" &&
24
23
  export const addElement =
25
24
  typeof GM_addElement === "function"
26
25
  ? (parentNode, tagName, attributes) => {
27
- if (!parentNode) {
26
+ if (typeof parentNode === "string") {
27
+ return addElement(null, parentNode, tagName)
28
+ }
29
+
30
+ if (!tagName) {
28
31
  return
29
32
  }
30
33
 
31
- if (typeof parentNode === "string") {
32
- attributes = tagName
33
- tagName = parentNode
34
- parentNode = doc.head
34
+ if (!parentNode) {
35
+ parentNode = /^(script|link|style|meta)$/.test(tagName)
36
+ ? getRootElement(1)
37
+ : getRootElement(2)
35
38
  }
36
39
 
37
40
  if (typeof tagName === "string") {
38
- const element = GM_addElement(tagName)
39
- setAttributes(element, attributes)
41
+ let attributes2
42
+ if (attributes) {
43
+ const entries1 = []
44
+ const entries2 = []
45
+ for (const entry of Object.entries(attributes)) {
46
+ if (/^(on\w+|innerHTML)$/.test(entry[0])) {
47
+ entries2.push(entry)
48
+ } else {
49
+ entries1.push(entry)
50
+ }
51
+ }
52
+
53
+ attributes = Object.fromEntries(entries1)
54
+ attributes2 = Object.fromEntries(entries2)
55
+ }
56
+
57
+ const element = GM_addElement(null, tagName, attributes)
58
+ setAttributes(element, attributes2)
40
59
  parentNode.append(element)
41
60
  return element
42
61
  }
@@ -48,10 +67,8 @@ export const addElement =
48
67
  }
49
68
  : _addElement
50
69
 
51
- export const addStyle =
52
- typeof GM_addStyle === "function"
53
- ? (styleText) => GM_addStyle(styleText)
54
- : _addStyle
70
+ export const addStyle = (styleText) =>
71
+ addElement(null, "style", { textContent: styleText })
55
72
 
56
73
  // Only register menu on top frame
57
74
  export const registerMenuCommand = (name, callback, accessKey) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "repository": {
17
17
  "type": "git",
18
- "url": "git+https://github.com/PipecraftNet/browser-extension-utils.git"
18
+ "url": "git+https://github.com/utags/browser-extension-utils.git"
19
19
  },
20
20
  "keywords": [
21
21
  "extensions",
@@ -25,9 +25,9 @@
25
25
  "author": "Pipecraft",
26
26
  "license": "MIT",
27
27
  "bugs": {
28
- "url": "https://github.com/PipecraftNet/browser-extension-utils/issues"
28
+ "url": "https://github.com/utags/browser-extension-utils/issues"
29
29
  },
30
- "homepage": "https://github.com/PipecraftNet/browser-extension-utils#readme",
30
+ "homepage": "https://github.com/utags/browser-extension-utils#readme",
31
31
  "devDependencies": {
32
32
  "prettier": "^2.8.8",
33
33
  "xo": "^0.55.0"