browser-extension-utils 0.1.18 → 0.2.0
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 +31 -8
- package/lib/index.js +16 -13
- package/lib/userscript.js +1 -1
- package/package.json +6 -4
package/lib/index.d.ts
CHANGED
|
@@ -68,15 +68,24 @@ export function removeEventListener(
|
|
|
68
68
|
type: string | Record<string, unknown>
|
|
69
69
|
): void
|
|
70
70
|
|
|
71
|
-
export function getAttribute(
|
|
71
|
+
export function getAttribute(
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
73
|
+
element: HTMLElement | null | undefined,
|
|
74
|
+
name: string
|
|
75
|
+
): string | undefined
|
|
72
76
|
|
|
73
77
|
export function setAttribute(
|
|
74
|
-
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
79
|
+
element: HTMLElement | null | undefined,
|
|
75
80
|
name: string,
|
|
76
81
|
value: string
|
|
77
82
|
): void
|
|
78
83
|
|
|
79
|
-
export function removeAttribute(
|
|
84
|
+
export function removeAttribute(
|
|
85
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
86
|
+
element: HTMLElement | null | undefined,
|
|
87
|
+
name: string
|
|
88
|
+
): void
|
|
80
89
|
|
|
81
90
|
export function setAttributes(
|
|
82
91
|
element: HTMLElement,
|
|
@@ -89,20 +98,34 @@ export function addAttribute(
|
|
|
89
98
|
value: string
|
|
90
99
|
): void
|
|
91
100
|
|
|
92
|
-
export function addClass(
|
|
101
|
+
export function addClass(
|
|
102
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
103
|
+
element: HTMLElement | null | undefined,
|
|
104
|
+
className: string
|
|
105
|
+
): void
|
|
93
106
|
|
|
94
|
-
export function removeClass(
|
|
107
|
+
export function removeClass(
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
109
|
+
element: HTMLElement | null | undefined,
|
|
110
|
+
className: string
|
|
111
|
+
): void
|
|
95
112
|
|
|
96
|
-
export function hasClass(
|
|
113
|
+
export function hasClass(
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
115
|
+
element: HTMLElement | null | undefined,
|
|
116
|
+
className: string
|
|
117
|
+
): boolean
|
|
97
118
|
|
|
98
119
|
export type SetStyle = (
|
|
99
|
-
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
121
|
+
element: HTMLElement | null | undefined,
|
|
100
122
|
style: string | Record<string, unknown>,
|
|
101
123
|
overwrite?: boolean
|
|
102
124
|
) => void
|
|
103
125
|
|
|
104
126
|
export function setStyle(
|
|
105
|
-
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
128
|
+
element: HTMLElement | null | undefined,
|
|
106
129
|
style: string | Record<string, unknown>,
|
|
107
130
|
overwrite?: boolean
|
|
108
131
|
): void
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export const doc = document
|
|
2
2
|
|
|
3
|
-
export const win =
|
|
3
|
+
export const win = globalThis
|
|
4
4
|
|
|
5
5
|
export const uniq = (array) => [...new Set(array)]
|
|
6
6
|
|
|
@@ -19,7 +19,7 @@ export const toCamelCase = function (text) {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export const $ = (selectors, element) =>
|
|
22
|
-
(element || doc).querySelector(selectors)
|
|
22
|
+
(element || doc).querySelector(selectors) || undefined
|
|
23
23
|
export const $$ = (selectors, element) => [
|
|
24
24
|
...(element || doc).querySelectorAll(selectors),
|
|
25
25
|
]
|
|
@@ -30,8 +30,8 @@ export const getRootElement = (type) =>
|
|
|
30
30
|
type === 1
|
|
31
31
|
? doc.head || doc.body || doc.documentElement
|
|
32
32
|
: type === 2
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
? doc.body || doc.documentElement
|
|
34
|
+
: doc.documentElement
|
|
35
35
|
|
|
36
36
|
export const createElement = (tagName, attributes) =>
|
|
37
37
|
setAttributes(doc.createElement(tagName), attributes)
|
|
@@ -102,11 +102,13 @@ export const removeEventListener = (element, type, listener, options) => {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
export const getAttribute = (element, name) =>
|
|
105
|
-
element ? element.getAttribute(name) :
|
|
105
|
+
element && element.getAttribute ? element.getAttribute(name) : undefined
|
|
106
106
|
export const setAttribute = (element, name, value) =>
|
|
107
|
-
element
|
|
107
|
+
element && element.setAttribute
|
|
108
|
+
? element.setAttribute(name, value)
|
|
109
|
+
: undefined
|
|
108
110
|
export const removeAttribute = (element, name) =>
|
|
109
|
-
element ? element.removeAttribute(name) : undefined
|
|
111
|
+
element && element.removeAttribute ? element.removeAttribute(name) : undefined
|
|
110
112
|
|
|
111
113
|
export const setAttributes = (element, attributes) => {
|
|
112
114
|
if (element && attributes) {
|
|
@@ -272,6 +274,7 @@ export const throttle = (func, interval) => {
|
|
|
272
274
|
// Polyfill for Object.hasOwn()
|
|
273
275
|
if (typeof Object.hasOwn !== "function") {
|
|
274
276
|
Object.hasOwn = (instance, prop) =>
|
|
277
|
+
// eslint-disable-next-line prefer-object-has-own
|
|
275
278
|
Object.prototype.hasOwnProperty.call(instance, prop)
|
|
276
279
|
}
|
|
277
280
|
|
|
@@ -285,19 +288,19 @@ export const extendHistoryApi = () => {
|
|
|
285
288
|
history.pushState = function () {
|
|
286
289
|
// eslint-disable-next-line prefer-rest-params
|
|
287
290
|
pushState.apply(history, arguments)
|
|
288
|
-
|
|
289
|
-
|
|
291
|
+
globalThis.dispatchEvent(new Event("pushstate"))
|
|
292
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
290
293
|
}
|
|
291
294
|
|
|
292
295
|
history.replaceState = function () {
|
|
293
296
|
// eslint-disable-next-line prefer-rest-params
|
|
294
297
|
replaceState.apply(history, arguments)
|
|
295
|
-
|
|
296
|
-
|
|
298
|
+
globalThis.dispatchEvent(new Event("replacestate"))
|
|
299
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
297
300
|
}
|
|
298
301
|
|
|
299
|
-
|
|
300
|
-
|
|
302
|
+
globalThis.addEventListener("popstate", function () {
|
|
303
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
301
304
|
})
|
|
302
305
|
|
|
303
306
|
// Usage example:
|
package/lib/userscript.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "browser-extension-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Utilities for developing browser extensions and userscripts",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://github.com/utags/browser-extension-utils#readme",
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"prettier": "^3.
|
|
33
|
-
"xo": "^0.
|
|
32
|
+
"prettier": "^3.5.3",
|
|
33
|
+
"xo": "^0.60.0"
|
|
34
34
|
},
|
|
35
35
|
"files": [
|
|
36
36
|
"lib/",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"README.md"
|
|
39
39
|
],
|
|
40
40
|
"engines": {
|
|
41
|
-
"node": "
|
|
41
|
+
"node": ">=16.9.0"
|
|
42
42
|
},
|
|
43
43
|
"xo": {
|
|
44
44
|
"space": 2,
|
|
@@ -56,7 +56,9 @@
|
|
|
56
56
|
"document"
|
|
57
57
|
],
|
|
58
58
|
"rules": {
|
|
59
|
+
"logical-assignment-operators": 0,
|
|
59
60
|
"prefer-destructuring": 0,
|
|
61
|
+
"unicorn/prevent-abbreviations": 0,
|
|
60
62
|
"capitalized-comments": 0
|
|
61
63
|
}
|
|
62
64
|
}
|