browser-extension-utils 0.0.4 → 0.0.6

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
@@ -36,9 +36,13 @@ export function setAttribute(
36
36
  value: string
37
37
  ): void
38
38
 
39
- export function setStyle(element, style: string | Record<string, unknown>): void
39
+ export function setStyle(
40
+ element: HTMLElement,
41
+ style: string | Record<string, unknown>,
42
+ overwrite?: boolean
43
+ ): void
40
44
 
41
- export function toStyleMap(styleText: string): Record<string, unknown>
45
+ export function toStyleMap(styleText: string): Record<string, string>
42
46
 
43
47
  export function noStyleSpace(text: string): string
44
48
 
package/lib/index.js CHANGED
@@ -38,9 +38,19 @@ export const addEventListener = (element, type, listener) => {
38
38
  export const setAttribute = (element, name, value) =>
39
39
  element.setAttribute(name, value)
40
40
 
41
- export const setStyle = (element, values) => {
42
- // setAttribute(element, "style", value)
41
+ export const setStyle = (element, values, overwrite) => {
42
+ // setAttribute(element, "style", value) -> Fail when violates CSP
43
43
  const style = element.style
44
+
45
+ if (overwrite) {
46
+ if (typeof values === "string") {
47
+ style.cssText = values
48
+ return
49
+ }
50
+
51
+ style.cssText = ""
52
+ }
53
+
44
54
  if (typeof values === "string") {
45
55
  values = toStyleKeyValues(values)
46
56
  }
@@ -74,14 +84,14 @@ export const toStyleMap = (styleText) => {
74
84
  for (const keyValue of keyValues) {
75
85
  const kv = keyValue.split("{")
76
86
  if (kv[0] && kv[1]) {
77
- map[kv[0]] = toStyleKeyValues(kv[1])
87
+ map[kv[0]] = kv[1]
78
88
  }
79
89
  }
80
90
 
81
91
  return map
82
92
  }
83
93
 
84
- export const noStyleSpace = (text) => text.replace(/\s*([^\w-])\s*/gm, "$1")
94
+ export const noStyleSpace = (text) => text.replace(/\s*([^\w-!])\s*/gm, "$1")
85
95
 
86
96
  export const createSetStyle = (styleText) => {
87
97
  const styleMap = toStyleMap(styleText)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",