browser-extension-utils 0.1.13 → 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 +25 -6
- package/lib/userscript.js +30 -13
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4,6 +4,13 @@ export const win = window
|
|
|
4
4
|
|
|
5
5
|
export const uniq = (array) => [...new Set(array)]
|
|
6
6
|
|
|
7
|
+
// Polyfill for String.prototype.replaceAll()
|
|
8
|
+
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
|
|
9
|
+
if (typeof String.prototype.replaceAll !== "function") {
|
|
10
|
+
// eslint-disable-next-line no-use-extend-native/no-use-extend-native, no-extend-native
|
|
11
|
+
String.prototype.replaceAll = String.prototype.replace
|
|
12
|
+
}
|
|
13
|
+
|
|
7
14
|
export const toCamelCase = function (text) {
|
|
8
15
|
return text.replaceAll(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2) {
|
|
9
16
|
if (p2) return p2.toUpperCase()
|
|
@@ -19,18 +26,29 @@ export const $$ = (selectors, element) => [
|
|
|
19
26
|
export const querySelector = $
|
|
20
27
|
export const querySelectorAll = $$
|
|
21
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
|
+
|
|
22
36
|
export const createElement = (tagName, attributes) =>
|
|
23
37
|
setAttributes(doc.createElement(tagName), attributes)
|
|
24
38
|
|
|
25
39
|
export const addElement = (parentNode, tagName, attributes) => {
|
|
26
|
-
if (
|
|
40
|
+
if (typeof parentNode === "string") {
|
|
41
|
+
return addElement(null, parentNode, tagName)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!tagName) {
|
|
27
45
|
return
|
|
28
46
|
}
|
|
29
47
|
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
if (!parentNode) {
|
|
49
|
+
parentNode = /^(script|link|style|meta)$/.test(tagName)
|
|
50
|
+
? getRootElement(1)
|
|
51
|
+
: getRootElement(2)
|
|
34
52
|
}
|
|
35
53
|
|
|
36
54
|
if (typeof tagName === "string") {
|
|
@@ -47,7 +65,7 @@ export const addElement = (parentNode, tagName, attributes) => {
|
|
|
47
65
|
|
|
48
66
|
export const addStyle = (styleText) => {
|
|
49
67
|
const element = createElement("style", { textContent: styleText })
|
|
50
|
-
|
|
68
|
+
getRootElement(1).append(element)
|
|
51
69
|
return element
|
|
52
70
|
}
|
|
53
71
|
|
|
@@ -251,6 +269,7 @@ export const throttle = (func, interval) => {
|
|
|
251
269
|
return handler
|
|
252
270
|
}
|
|
253
271
|
|
|
272
|
+
// Polyfill for Object.hasOwn()
|
|
254
273
|
if (typeof Object.hasOwn !== "function") {
|
|
255
274
|
Object.hasOwn = (instance, prop) =>
|
|
256
275
|
Object.prototype.hasOwnProperty.call(instance, prop)
|
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) => {
|