browser-extension-utils 0.0.11 → 0.0.12

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.
Files changed (2) hide show
  1. package/lib/index.js +17 -4
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -10,11 +10,11 @@ export const toCamelCase = function (text) {
10
10
  }
11
11
 
12
12
  export const $ = (element, selectors) =>
13
- typeof element === "object"
13
+ element && typeof element === "object"
14
14
  ? element.querySelector(selectors)
15
15
  : doc.querySelector(element)
16
16
  export const $$ = (element, selectors) =>
17
- typeof element === "object"
17
+ element && typeof element === "object"
18
18
  ? [...element.querySelectorAll(selectors)]
19
19
  : [...doc.querySelectorAll(element)]
20
20
  export const querySelector = $
@@ -41,6 +41,10 @@ export const createElement = (tagName, attributes) => {
41
41
  }
42
42
 
43
43
  export const addElement = (parentNode, tagName, attributes) => {
44
+ if (!parentNode) {
45
+ return
46
+ }
47
+
44
48
  if (typeof parentNode === "string") {
45
49
  attributes = tagName
46
50
  tagName = parentNode
@@ -59,6 +63,10 @@ export const addStyle = (styleText) => {
59
63
  }
60
64
 
61
65
  export const addEventListener = (element, type, listener, options) => {
66
+ if (!element) {
67
+ return () => 0
68
+ }
69
+
62
70
  if (typeof type === "object") {
63
71
  const removers = []
64
72
  for (const type1 in type) {
@@ -81,11 +89,16 @@ export const addEventListener = (element, type, listener, options) => {
81
89
  }
82
90
  }
83
91
 
84
- export const getAttribute = (element, name) => element.getAttribute(name)
92
+ export const getAttribute = (element, name) =>
93
+ element ? element.getAttribute(name) : null
85
94
  export const setAttribute = (element, name, value) =>
86
- element.setAttribute(name, value)
95
+ element ? element.setAttribute(name, value) : undefined
87
96
 
88
97
  export const setStyle = (element, values, overwrite) => {
98
+ if (!element) {
99
+ return
100
+ }
101
+
89
102
  // element.setAttribute("style", value) -> Fail when violates CSP
90
103
  const style = element.style
91
104
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",