browser-extension-utils 0.0.1
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/LICENSE +21 -0
- package/README.md +24 -0
- package/lib/index.js +105 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Pipecraft
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# browser-extension-utils
|
|
2
|
+
|
|
3
|
+
Utilities for developing browser extensions and userscripts
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i browser-extension-utils
|
|
9
|
+
# or
|
|
10
|
+
pnpm add browser-extension-utils
|
|
11
|
+
# or
|
|
12
|
+
yarn add browser-extension-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
Copyright (c) 2023 [Pipecraft](https://www.pipecraft.net). Licensed under the [MIT License](LICENSE).
|
|
18
|
+
|
|
19
|
+
## >\_
|
|
20
|
+
|
|
21
|
+
[](https://www.pipecraft.net)
|
|
22
|
+
[](https://utags.pipecraft.net)
|
|
23
|
+
[](https://dto.pipecraft.net)
|
|
24
|
+
[](https://www.bestxtools.com)
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const doc = document
|
|
2
|
+
|
|
3
|
+
export const uniq = (array) => [...new Set(array)]
|
|
4
|
+
|
|
5
|
+
export const toCamelCase = function (text) {
|
|
6
|
+
return text.replace(/^([A-Z])|[\s-_](\w)/g, function (match, p1, p2) {
|
|
7
|
+
if (p2) return p2.toUpperCase()
|
|
8
|
+
return p1.toLowerCase()
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const querySelector = (element, selectors) =>
|
|
13
|
+
typeof element === "object"
|
|
14
|
+
? element.querySelector(selectors)
|
|
15
|
+
: doc.querySelector(element)
|
|
16
|
+
export const querySelectorAll = (element, selectors) =>
|
|
17
|
+
typeof element === "object"
|
|
18
|
+
? [...element.querySelectorAll(selectors)]
|
|
19
|
+
: [...doc.querySelectorAll(element)]
|
|
20
|
+
export const $ = querySelector
|
|
21
|
+
export const $$ = querySelectorAll
|
|
22
|
+
|
|
23
|
+
export const createElement = doc.createElement.bind(doc)
|
|
24
|
+
|
|
25
|
+
export const addEventListener = (element, type, listener) => {
|
|
26
|
+
if (typeof type === "object") {
|
|
27
|
+
for (const type1 in type) {
|
|
28
|
+
if (Object.hasOwn(type, type1)) {
|
|
29
|
+
element.addEventListener(type1, type[type1])
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
} else if (typeof type === "string" && typeof listener === "function") {
|
|
33
|
+
element.addEventListener(type, listener)
|
|
34
|
+
}
|
|
35
|
+
// TODO: return remover function
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const setAttribute = (element, attr, value) =>
|
|
39
|
+
element.setAttribute(attr, value)
|
|
40
|
+
|
|
41
|
+
export const setStyle = (element, values) => {
|
|
42
|
+
// setAttribute(element, "style", value)
|
|
43
|
+
const { style } = element
|
|
44
|
+
if (typeof values === "string") {
|
|
45
|
+
values = toStyleKeyValues(values)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
for (const key in values) {
|
|
49
|
+
if (Object.hasOwn(values, key)) {
|
|
50
|
+
style[key] = values[key]
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// convert `font-size: 12px; color: red` to `{"fontSize": "12px"; "color": "red"}`
|
|
56
|
+
const toStyleKeyValues = (styleText) => {
|
|
57
|
+
const result = {}
|
|
58
|
+
const keyValues = styleText.split(/\s*;\s*/)
|
|
59
|
+
for (const keyValue of keyValues) {
|
|
60
|
+
const kv = keyValue.split(/\s*:\s*/)
|
|
61
|
+
const key = toCamelCase(kv[0])
|
|
62
|
+
if (key) {
|
|
63
|
+
result[key] = kv[1]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const toStyleMap = (styleText) => {
|
|
71
|
+
styleText = noStyleSpace(styleText)
|
|
72
|
+
console.log(styleText)
|
|
73
|
+
const map = {}
|
|
74
|
+
const keyValues = styleText.split("}")
|
|
75
|
+
for (const keyValue of keyValues) {
|
|
76
|
+
const kv = keyValue.split("{")
|
|
77
|
+
if (kv[0] && kv[1]) {
|
|
78
|
+
map[kv[0]] = toStyleKeyValues(kv[1])
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return map
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const noStyleSpace = (text) => text.replace(/\s*([^\w-])\s*/gm, "$1")
|
|
86
|
+
|
|
87
|
+
export const createSetStyle = (styleText) => {
|
|
88
|
+
const styleMap = toStyleMap(styleText)
|
|
89
|
+
return (element, key) => {
|
|
90
|
+
if (typeof key === "object") {
|
|
91
|
+
setStyle(element, key)
|
|
92
|
+
} else if (typeof key === "string") {
|
|
93
|
+
key = noStyleSpace(key)
|
|
94
|
+
const value = styleMap[key]
|
|
95
|
+
setStyle(element, value || key)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export const isUrl = (text) => /^https?:\/\//.test(text)
|
|
101
|
+
|
|
102
|
+
if (typeof Object.hasOwn !== "function") {
|
|
103
|
+
Object.hasOwn = (instance, prop) =>
|
|
104
|
+
Object.prototype.hasOwnProperty.call(instance, prop)
|
|
105
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "browser-extension-utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Utilities for developing browser extensions and userscripts",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./lib/index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"p": "prettier --write .",
|
|
9
|
+
"lint": "prettier --write . && xo --fix",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 0"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/PipecraftNet/browser-extension-utils.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"extensions",
|
|
18
|
+
"userscripts",
|
|
19
|
+
"utilities"
|
|
20
|
+
],
|
|
21
|
+
"author": "Pipecraft",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/PipecraftNet/browser-extension-utils/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/PipecraftNet/browser-extension-utils#readme",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"prettier": "^2.8.7",
|
|
29
|
+
"xo": "^0.53.1"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"lib/",
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
38
|
+
},
|
|
39
|
+
"xo": {
|
|
40
|
+
"space": 2,
|
|
41
|
+
"prettier": true,
|
|
42
|
+
"globals": [
|
|
43
|
+
"document"
|
|
44
|
+
],
|
|
45
|
+
"rules": {
|
|
46
|
+
"capitalized-comments": 0
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|