Skip to content

Commit 2a5e0ff

Browse files
committed
refact: update types
1 parent ab6a2f5 commit 2a5e0ff

File tree

4 files changed

+47
-21
lines changed

4 files changed

+47
-21
lines changed

lib/index.d.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,24 @@ export function removeEventListener(
6868
type: string | Record<string, unknown>
6969
): void
7070

71-
export function getAttribute(element: HTMLElement, name: string): string
71+
export function getAttribute(
72+
// eslint-disable-next-line @typescript-eslint/ban-types
73+
element: HTMLElement | null | undefined,
74+
name: string
75+
): string | undefined
7276

7377
export function setAttribute(
74-
element: HTMLElement,
78+
// eslint-disable-next-line @typescript-eslint/ban-types
79+
element: HTMLElement | null | undefined,
7580
name: string,
7681
value: string
7782
): void
7883

79-
export function removeAttribute(element: HTMLElement, name: string): void
84+
export function removeAttribute(
85+
// eslint-disable-next-line @typescript-eslint/ban-types
86+
element: HTMLElement | null | undefined,
87+
name: string
88+
): void
8089

8190
export function setAttributes(
8291
element: HTMLElement,
@@ -89,20 +98,34 @@ export function addAttribute(
8998
value: string
9099
): void
91100

92-
export function addClass(element: HTMLElement, className: string): void
101+
export function addClass(
102+
// eslint-disable-next-line @typescript-eslint/ban-types
103+
element: HTMLElement | null | undefined,
104+
className: string
105+
): void
93106

94-
export function removeClass(element: HTMLElement, className: string): void
107+
export function removeClass(
108+
// eslint-disable-next-line @typescript-eslint/ban-types
109+
element: HTMLElement | null | undefined,
110+
className: string
111+
): void
95112

96-
export function hasClass(element: HTMLElement, className: string): boolean
113+
export function hasClass(
114+
// eslint-disable-next-line @typescript-eslint/ban-types
115+
element: HTMLElement | null | undefined,
116+
className: string
117+
): boolean
97118

98119
export type SetStyle = (
99-
element: HTMLElement,
120+
// eslint-disable-next-line @typescript-eslint/ban-types
121+
element: HTMLElement | null | undefined,
100122
style: string | Record<string, unknown>,
101123
overwrite?: boolean
102124
) => void
103125

104126
export function setStyle(
105-
element: HTMLElement,
127+
// eslint-disable-next-line @typescript-eslint/ban-types
128+
element: HTMLElement | null | undefined,
106129
style: string | Record<string, unknown>,
107130
overwrite?: boolean
108131
): void

lib/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export const doc = document
22

3-
export const win = window
3+
export const win = globalThis
44

55
export const uniq = (array) => [...new Set(array)]
66

@@ -19,7 +19,7 @@ export const toCamelCase = function (text) {
1919
}
2020

2121
export const $ = (selectors, element) =>
22-
(element || doc).querySelector(selectors)
22+
(element || doc).querySelector(selectors) || undefined
2323
export const $$ = (selectors, element) => [
2424
...(element || doc).querySelectorAll(selectors),
2525
]
@@ -30,8 +30,8 @@ export const getRootElement = (type) =>
3030
type === 1
3131
? doc.head || doc.body || doc.documentElement
3232
: type === 2
33-
? doc.body || doc.documentElement
34-
: doc.documentElement
33+
? doc.body || doc.documentElement
34+
: doc.documentElement
3535

3636
export const createElement = (tagName, attributes) =>
3737
setAttributes(doc.createElement(tagName), attributes)
@@ -102,7 +102,7 @@ export const removeEventListener = (element, type, listener, options) => {
102102
}
103103

104104
export const getAttribute = (element, name) =>
105-
element && element.getAttribute ? element.getAttribute(name) : null
105+
element && element.getAttribute ? element.getAttribute(name) : undefined
106106
export const setAttribute = (element, name, value) =>
107107
element && element.setAttribute
108108
? element.setAttribute(name, value)
@@ -274,6 +274,7 @@ export const throttle = (func, interval) => {
274274
// Polyfill for Object.hasOwn()
275275
if (typeof Object.hasOwn !== "function") {
276276
Object.hasOwn = (instance, prop) =>
277+
// eslint-disable-next-line prefer-object-has-own
277278
Object.prototype.hasOwnProperty.call(instance, prop)
278279
}
279280

@@ -287,19 +288,19 @@ export const extendHistoryApi = () => {
287288
history.pushState = function () {
288289
// eslint-disable-next-line prefer-rest-params
289290
pushState.apply(history, arguments)
290-
window.dispatchEvent(new Event("pushstate"))
291-
window.dispatchEvent(new Event("locationchange"))
291+
globalThis.dispatchEvent(new Event("pushstate"))
292+
globalThis.dispatchEvent(new Event("locationchange"))
292293
}
293294

294295
history.replaceState = function () {
295296
// eslint-disable-next-line prefer-rest-params
296297
replaceState.apply(history, arguments)
297-
window.dispatchEvent(new Event("replacestate"))
298-
window.dispatchEvent(new Event("locationchange"))
298+
globalThis.dispatchEvent(new Event("replacestate"))
299+
globalThis.dispatchEvent(new Event("locationchange"))
299300
}
300301

301-
window.addEventListener("popstate", function () {
302-
window.dispatchEvent(new Event("locationchange"))
302+
globalThis.addEventListener("popstate", function () {
303+
globalThis.dispatchEvent(new Event("locationchange"))
303304
})
304305

305306
// Usage example:

lib/userscript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const addStyle = (styleText) =>
7272

7373
// Only register menu on top frame
7474
export const registerMenuCommand = (name, callback, accessKey) => {
75-
if (window !== top) {
75+
if (globalThis !== top) {
7676
return
7777
}
7878

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"README.md"
3939
],
4040
"engines": {
41-
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
41+
"node": ">=16.9.0"
4242
},
4343
"xo": {
4444
"space": 2,
@@ -56,7 +56,9 @@
5656
"document"
5757
],
5858
"rules": {
59+
"logical-assignment-operators": 0,
5960
"prefer-destructuring": 0,
61+
"unicorn/prevent-abbreviations": 0,
6062
"capitalized-comments": 0
6163
}
6264
}

0 commit comments

Comments
 (0)