browser-extension-utils 0.1.14 → 0.1.15
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.js +17 -6
- package/lib/userscript.js +30 -13
- package/package.json +1 -1
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 (
|
|
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 (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
68
|
+
getRootElement(1).append(element)
|
|
58
69
|
return element
|
|
59
70
|
}
|
|
60
71
|
|
package/lib/userscript.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
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 (
|
|
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 (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
39
|
-
|
|
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
|
-
|
|
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) => {
|