browser-extension-utils 0.0.9 → 0.0.11

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 CHANGED
@@ -2,33 +2,62 @@ export function uniq(array: any[]): any[]
2
2
 
3
3
  export function toCamelCase(text: string): string
4
4
 
5
+ export function $(selectors: string): HTMLElement
6
+
5
7
  export function $(
6
8
  element: HTMLElement | Document | string,
7
- selectors?: string
9
+ selectors: string
8
10
  ): HTMLElement
9
11
 
12
+ export function querySelector(selectors: string): HTMLElement
13
+
10
14
  export function querySelector(
11
15
  element: HTMLElement | Document | string,
12
- selectors?: string
16
+ selectors: string
13
17
  ): HTMLElement
14
18
 
19
+ export function $$(selectors: string): HTMLElement[]
20
+
15
21
  export function $$(
16
22
  element: HTMLElement | Document | string,
17
- selectors?: string
23
+ selectors: string
18
24
  ): HTMLElement[]
19
25
 
26
+ export function querySelectorAll(selectors: string): HTMLElement[]
27
+
20
28
  export function querySelectorAll(
21
29
  element: HTMLElement | Document | string,
22
- selectors?: string
30
+ selectors: string
23
31
  ): HTMLElement[]
24
32
 
25
- export function createElement(tagName: string): HTMLElement
33
+ export function createElement(
34
+ tagName: string,
35
+ attributes?: Record<string, unknown>
36
+ ): HTMLElement
37
+
38
+ export function addElement(
39
+ tagName: string,
40
+ attributes?: Record<string, unknown>
41
+ ): HTMLElement
42
+
43
+ export function addElement(
44
+ parentNode: HTMLElement,
45
+ tagName: string,
46
+ attributes?: Record<string, unknown>
47
+ ): HTMLElement
48
+
49
+ export function addStyle(styleText: string): HTMLElement
26
50
 
27
51
  export function addEventListener(
28
52
  element: HTMLElement | Document | EventTarget,
29
- type: string | Record<string, unknown>,
30
- listener?
31
- ): void
53
+ type: string,
54
+ listener: Function
55
+ ): () => void
56
+
57
+ export function addEventListener(
58
+ element: HTMLElement | Document | EventTarget,
59
+ type: string | Record<string, unknown>
60
+ ): () => void
32
61
 
33
62
  export function getAttribute(element: HTMLElement, name: string): string
34
63
 
package/lib/index.js CHANGED
@@ -20,19 +20,65 @@ export const $$ = (element, selectors) =>
20
20
  export const querySelector = $
21
21
  export const querySelectorAll = $$
22
22
 
23
- export const createElement = doc.createElement.bind(doc)
23
+ export const createElement = (tagName, attributes) => {
24
+ const element = doc.createElement(tagName)
25
+ if (attributes) {
26
+ for (const name in attributes) {
27
+ if (Object.hasOwn(attributes, name)) {
28
+ const value = attributes[name]
29
+ if (name === "textContent") {
30
+ element[name] = value
31
+ } else if (name === "style") {
32
+ setStyle(element, value)
33
+ } else {
34
+ setAttribute(element, name, value)
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ return element
41
+ }
42
+
43
+ export const addElement = (parentNode, tagName, attributes) => {
44
+ if (typeof parentNode === "string") {
45
+ attributes = tagName
46
+ tagName = parentNode
47
+ parentNode = doc.head
48
+ }
49
+
50
+ const element = createElement(tagName, attributes)
51
+ parentNode.append(element)
52
+ return element
53
+ }
54
+
55
+ export const addStyle = (styleText) => {
56
+ const element = createElement("style", { textContent: styleText })
57
+ doc.head.append(element)
58
+ return element
59
+ }
24
60
 
25
- export const addEventListener = (element, type, listener) => {
61
+ export const addEventListener = (element, type, listener, options) => {
26
62
  if (typeof type === "object") {
63
+ const removers = []
27
64
  for (const type1 in type) {
28
65
  if (Object.hasOwn(type, type1)) {
29
66
  element.addEventListener(type1, type[type1])
67
+ removers.push(() => element.removeEventListener(type1, type[type1]))
30
68
  }
31
69
  }
32
- } else if (typeof type === "string" && typeof listener === "function") {
33
- element.addEventListener(type, listener)
70
+
71
+ return () => {
72
+ for (const remover of removers) remover()
73
+ }
74
+ }
75
+
76
+ if (typeof type === "string" && typeof listener === "function") {
77
+ element.addEventListener(type, listener, options)
78
+ return () => {
79
+ element.removeEventListener(type, listener, options)
80
+ }
34
81
  }
35
- // TODO: return remover function
36
82
  }
37
83
 
38
84
  export const getAttribute = (element, name) => element.getAttribute(name)
@@ -44,8 +90,8 @@ export const setStyle = (element, values, overwrite) => {
44
90
  const style = element.style
45
91
 
46
92
  if (typeof values === "string") {
47
- style.cssText = overwrite? values : style.cssText + ";" +values
48
- return;
93
+ style.cssText = overwrite ? values : style.cssText + ";" + values
94
+ return
49
95
  }
50
96
 
51
97
  if (overwrite) {
@@ -0,0 +1,20 @@
1
+ export * from "./index.js"
2
+ /* eslint-disable camelcase */
3
+ // eslint-disable-next-line no-unused-expressions, n/prefer-global/process
4
+ process.env.PLASMO_TAG === "dev" &&
5
+ (() => {
6
+ if (
7
+ typeof GM_addElement !== "function" &&
8
+ typeof document.GM_addElement === "function"
9
+ ) {
10
+ // eslint-disable-next-line no-global-assign
11
+ GM_addElement = document.GM_addElement
12
+ // eslint-disable-next-line no-global-assign
13
+ GM_addStyle = document.GM_addStyle
14
+ }
15
+ })()
16
+
17
+ export const addElement = GM_addElement
18
+
19
+ export const addStyle = GM_addStyle
20
+ /* eslint-enable camelcase */
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
7
- "exports": "./lib/index.js",
7
+ "exports": {
8
+ ".": "./lib/index.js",
9
+ "./userscript": "./lib/userscript.js"
10
+ },
8
11
  "scripts": {
9
12
  "p": "prettier --write .",
10
13
  "lint": "prettier --write . && xo --fix",
@@ -41,6 +44,8 @@
41
44
  "space": 2,
42
45
  "prettier": true,
43
46
  "globals": [
47
+ "GM_addElement",
48
+ "GM_addStyle",
44
49
  "document"
45
50
  ],
46
51
  "rules": {