browser-extension-utils 0.0.13 → 0.0.15

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
@@ -42,7 +42,7 @@ export function addElement(
42
42
 
43
43
  export function addElement(
44
44
  parentNode: HTMLElement,
45
- tagName: string,
45
+ tagName: string | HTMLElement,
46
46
  attributes?: Record<string, unknown>
47
47
  ): HTMLElement
48
48
 
@@ -53,12 +53,24 @@ export function addEventListener(
53
53
  type: string,
54
54
  listener: EventListenerOrEventListenerObject,
55
55
  options?: boolean | AddEventListenerOptions
56
- ): () => void
56
+ ): void
57
57
 
58
58
  export function addEventListener(
59
59
  element: HTMLElement | Document | EventTarget,
60
60
  type: string | Record<string, unknown>
61
- ): () => void
61
+ ): void
62
+
63
+ export function removeEventListener(
64
+ element: HTMLElement | Document | EventTarget,
65
+ type: string,
66
+ listener: EventListenerOrEventListenerObject,
67
+ options?: boolean | AddEventListenerOptions
68
+ ): void
69
+
70
+ export function removeEventListener(
71
+ element: HTMLElement | Document | EventTarget,
72
+ type: string | Record<string, unknown>
73
+ ): void
62
74
 
63
75
  export function getAttribute(element: HTMLElement, name: string): string
64
76
 
@@ -68,6 +80,11 @@ export function setAttribute(
68
80
  value: string
69
81
  ): void
70
82
 
83
+ export function setAttributes(
84
+ element: HTMLElement,
85
+ attributes: Record<string, unknown>
86
+ ): void
87
+
71
88
  export type SetStyle = (
72
89
  element: HTMLElement,
73
90
  style: string | Record<string, unknown>,
@@ -87,3 +104,10 @@ export function noStyleSpace(text: string): string
87
104
  export function createSetStyle(styleText: string): SetStyle
88
105
 
89
106
  export function isUrl(text: string): boolean
107
+
108
+ export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
109
+ export function registerMenuCommand(
110
+ name: string,
111
+ callback: MenuCallback,
112
+ accessKey?: string
113
+ ): void
package/lib/index.js CHANGED
@@ -20,25 +20,8 @@ export const $$ = (element, selectors) =>
20
20
  export const querySelector = $
21
21
  export const querySelectorAll = $$
22
22
 
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
- }
23
+ export const createElement = (tagName, attributes) =>
24
+ setAttributes(doc.createElement(tagName), attributes)
42
25
 
43
26
  export const addElement = (parentNode, tagName, attributes) => {
44
27
  if (!parentNode) {
@@ -51,9 +34,16 @@ export const addElement = (parentNode, tagName, attributes) => {
51
34
  parentNode = doc.head
52
35
  }
53
36
 
54
- const element = createElement(tagName, attributes)
55
- parentNode.append(element)
56
- return element
37
+ if (typeof tagName === "string") {
38
+ const element = createElement(tagName, attributes)
39
+ parentNode.append(element)
40
+ return element
41
+ }
42
+
43
+ // tagName: HTMLElement
44
+ setAttributes(tagName, attributes)
45
+ parentNode.append(tagName)
46
+ return tagName
57
47
  }
58
48
 
59
49
  export const addStyle = (styleText) => {
@@ -64,28 +54,33 @@ export const addStyle = (styleText) => {
64
54
 
65
55
  export const addEventListener = (element, type, listener, options) => {
66
56
  if (!element) {
67
- return () => 0
57
+ return
68
58
  }
69
59
 
70
60
  if (typeof type === "object") {
71
- const removers = []
72
61
  for (const type1 in type) {
73
62
  if (Object.hasOwn(type, type1)) {
74
63
  element.addEventListener(type1, type[type1])
75
- removers.push(() => element.removeEventListener(type1, type[type1]))
76
64
  }
77
65
  }
66
+ } else if (typeof type === "string" && typeof listener === "function") {
67
+ element.addEventListener(type, listener, options)
68
+ }
69
+ }
78
70
 
79
- return () => {
80
- for (const remover of removers) remover()
81
- }
71
+ export const removeEventListener = (element, type, listener, options) => {
72
+ if (!element) {
73
+ return
82
74
  }
83
75
 
84
- if (typeof type === "string" && typeof listener === "function") {
85
- element.addEventListener(type, listener, options)
86
- return () => {
87
- element.removeEventListener(type, listener, options)
76
+ if (typeof type === "object") {
77
+ for (const type1 in type) {
78
+ if (Object.hasOwn(type, type1)) {
79
+ element.removeEventListener(type1, type[type1])
80
+ }
88
81
  }
82
+ } else if (typeof type === "string" && typeof listener === "function") {
83
+ element.removeEventListener(type, listener, options)
89
84
  }
90
85
  }
91
86
 
@@ -94,6 +89,32 @@ export const getAttribute = (element, name) =>
94
89
  export const setAttribute = (element, name, value) =>
95
90
  element ? element.setAttribute(name, value) : undefined
96
91
 
92
+ export const setAttributes = (element, attributes) => {
93
+ if (element && attributes) {
94
+ for (const name in attributes) {
95
+ if (Object.hasOwn(attributes, name)) {
96
+ const value = attributes[name]
97
+ if (value === undefined) {
98
+ continue
99
+ }
100
+
101
+ if (/^(value|textContent|innerText|innerHTML)$/.test(name)) {
102
+ element[name] = value
103
+ } else if (name === "style") {
104
+ setStyle(element, value, true)
105
+ } else if (/on\w+/.test(name)) {
106
+ const type = name.slice(2)
107
+ addEventListener(element, type, value)
108
+ } else {
109
+ setAttribute(element, name, value)
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ return element
116
+ }
117
+
97
118
  export const setStyle = (element, values, overwrite) => {
98
119
  if (!element) {
99
120
  return
@@ -148,7 +169,7 @@ export const toStyleMap = (styleText) => {
148
169
  return map
149
170
  }
150
171
 
151
- export const noStyleSpace = (text) => text.replace(/\s*([^\w-!])\s*/gm, "$1")
172
+ export const noStyleSpace = (text) => text.replace(/\s*([^\w-+%!])\s*/gm, "$1")
152
173
 
153
174
  export const createSetStyle = (styleText) => {
154
175
  const styleMap = toStyleMap(styleText)
@@ -169,3 +190,5 @@ if (typeof Object.hasOwn !== "function") {
169
190
  Object.hasOwn = (instance, prop) =>
170
191
  Object.prototype.hasOwnProperty.call(instance, prop)
171
192
  }
193
+
194
+ export const registerMenuCommand = () => undefined
package/lib/userscript.js CHANGED
@@ -1,21 +1,37 @@
1
+ import { setAttributes } from "./index.js"
2
+
1
3
  export * from "./index.js"
2
- /* eslint-disable camelcase, new-cap */
4
+
3
5
  // eslint-disable-next-line no-unused-expressions, n/prefer-global/process
4
6
  process.env.PLASMO_TAG === "dev" &&
5
7
  (() => {
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
8
+ const functions = document.GMFunctions
9
+ if (typeof functions === "object") {
10
+ for (const key in functions) {
11
+ if (Object.hasOwn(functions, key)) {
12
+ window[key] = functions[key]
13
+ }
14
+ }
14
15
  }
15
16
  })()
16
17
 
17
- export const addElement = (parentNode, tagName, attributes) =>
18
- GM_addElement(parentNode, tagName, attributes)
18
+ /* eslint-disable new-cap */
19
+ export const addElement = (parentNode, tagName, attributes) => {
20
+ if (typeof parentNode === "string" || typeof tagName === "string") {
21
+ const element = GM_addElement(parentNode, tagName, attributes)
22
+ setAttributes(element, attributes)
23
+ return element
24
+ }
25
+
26
+ // tagName: HTMLElement
27
+ setAttributes(tagName, attributes)
28
+ parentNode.append(tagName)
29
+ return tagName
30
+ }
19
31
 
20
32
  export const addStyle = (styleText) => GM_addStyle(styleText)
21
- /* eslint-enable camelcase, new-cap */
33
+
34
+ // Only register menu on top frame
35
+ export const registerMenuCommand = (name, callback, accessKey) =>
36
+ window === top && GM_registerMenuCommand(name, callback, accessKey)
37
+ /* eslint-enable new-cap */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -46,6 +46,9 @@
46
46
  "globals": [
47
47
  "GM_addElement",
48
48
  "GM_addStyle",
49
+ "GM_registerMenuCommand",
50
+ "window",
51
+ "top",
49
52
  "document"
50
53
  ],
51
54
  "rules": {