browser-extension-utils 0.1.19 → 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 +12 -11
- 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,7 +102,7 @@ export const removeEventListener = (element, type, listener, options) => {
|
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
export const getAttribute = (element, name) =>
|
|
105
|
-
element && element.getAttribute ? element.getAttribute(name) :
|
|
105
|
+
element && element.getAttribute ? element.getAttribute(name) : undefined
|
|
106
106
|
export const setAttribute = (element, name, value) =>
|
|
107
107
|
element && element.setAttribute
|
|
108
108
|
? element.setAttribute(name, value)
|
|
@@ -274,6 +274,7 @@ export const throttle = (func, interval) => {
|
|
|
274
274
|
// Polyfill for Object.hasOwn()
|
|
275
275
|
if (typeof Object.hasOwn !== "function") {
|
|
276
276
|
Object.hasOwn = (instance, prop) =>
|
|
277
|
+
// eslint-disable-next-line prefer-object-has-own
|
|
277
278
|
Object.prototype.hasOwnProperty.call(instance, prop)
|
|
278
279
|
}
|
|
279
280
|
|
|
@@ -287,19 +288,19 @@ export const extendHistoryApi = () => {
|
|
|
287
288
|
history.pushState = function () {
|
|
288
289
|
// eslint-disable-next-line prefer-rest-params
|
|
289
290
|
pushState.apply(history, arguments)
|
|
290
|
-
|
|
291
|
-
|
|
291
|
+
globalThis.dispatchEvent(new Event("pushstate"))
|
|
292
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
292
293
|
}
|
|
293
294
|
|
|
294
295
|
history.replaceState = function () {
|
|
295
296
|
// eslint-disable-next-line prefer-rest-params
|
|
296
297
|
replaceState.apply(history, arguments)
|
|
297
|
-
|
|
298
|
-
|
|
298
|
+
globalThis.dispatchEvent(new Event("replacestate"))
|
|
299
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
299
300
|
}
|
|
300
301
|
|
|
301
|
-
|
|
302
|
-
|
|
302
|
+
globalThis.addEventListener("popstate", function () {
|
|
303
|
+
globalThis.dispatchEvent(new Event("locationchange"))
|
|
303
304
|
})
|
|
304
305
|
|
|
305
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
|
}
|