browser-extension-utils 0.1.2 → 0.1.4

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
@@ -1,5 +1,7 @@
1
1
  export const doc: Document
2
2
 
3
+ export const win: Window
4
+
3
5
  export function uniq(array: any[]): any[]
4
6
 
5
7
  export function toCamelCase(text: string): string
@@ -85,6 +87,12 @@ export function addAttribute(
85
87
  value: string
86
88
  ): void
87
89
 
90
+ export function addClass(element: HTMLElement, className: string): void
91
+
92
+ export function removeClass(element: HTMLElement, className: string): void
93
+
94
+ export function hasClass(element: HTMLElement, className: string): boolean
95
+
88
96
  export type SetStyle = (
89
97
  element: HTMLElement,
90
98
  style: string | Record<string, unknown>,
@@ -105,6 +113,7 @@ export function createSetStyle(styleText: string): SetStyle
105
113
 
106
114
  export function isUrl(text: string): boolean
107
115
 
116
+ // eslint-disable-next-line @typescript-eslint/ban-types
108
117
  export function throttle(func: Function, interval: number): Function
109
118
 
110
119
  export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
@@ -115,3 +124,16 @@ export function registerMenuCommand(
115
124
  ): void
116
125
 
117
126
  export function extendHistoryApi(): void
127
+
128
+ export const actionHref: string
129
+
130
+ export function getOffsetPosition(
131
+ element: HTMLElement | undefined,
132
+ referElement?: HTMLElement | undefined
133
+ ): {
134
+ top: number
135
+ left: number
136
+ }
137
+
138
+ // eslint-disable-next-line @typescript-eslint/ban-types
139
+ export function runOnce(key: string, func: Function): any
package/lib/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export const doc = document
2
2
 
3
+ export const win = window
4
+
3
5
  export const uniq = (array) => [...new Set(array)]
4
6
 
5
7
  export const toCamelCase = function (text) {
@@ -121,6 +123,30 @@ export const addAttribute = (element, name, value) => {
121
123
  }
122
124
  }
123
125
 
126
+ export const addClass = (element, className) => {
127
+ if (!element || !element.classList) {
128
+ return
129
+ }
130
+
131
+ element.classList.add(className)
132
+ }
133
+
134
+ export const removeClass = (element, className) => {
135
+ if (!element || !element.classList) {
136
+ return
137
+ }
138
+
139
+ element.classList.remove(className)
140
+ }
141
+
142
+ export const hasClass = (element, className) => {
143
+ if (!element || !element.classList) {
144
+ return false
145
+ }
146
+
147
+ return element.classList.contains(className)
148
+ }
149
+
124
150
  export const setStyle = (element, values, overwrite) => {
125
151
  if (!element) {
126
152
  return
@@ -146,6 +172,7 @@ export const setStyle = (element, values, overwrite) => {
146
172
  }
147
173
 
148
174
  // convert `font-size: 12px; color: red` to `{"fontSize": "12px"; "color": "red"}`
175
+ // eslint-disable-next-line no-unused-vars
149
176
  const toStyleKeyValues = (styleText) => {
150
177
  const result = {}
151
178
  const keyValues = styleText.split(/\s*;\s*/)
@@ -254,3 +281,34 @@ export const extendHistoryApi = () => {
254
281
  // console.log("onlocationchange event occurred!")
255
282
  // })
256
283
  }
284
+
285
+ // eslint-disable-next-line no-script-url
286
+ export const actionHref = "javascript:;"
287
+
288
+ export const getOffsetPosition = (element, referElement) => {
289
+ const position = { top: 0, left: 0 }
290
+ referElement = referElement || doc.body
291
+
292
+ while (element && element !== referElement) {
293
+ position.top += element.offsetTop
294
+ position.left += element.offsetLeft
295
+ element = element.offsetParent
296
+ }
297
+
298
+ return position
299
+ }
300
+
301
+ const runOnceCache = {}
302
+ export const runOnce = (key, func) => {
303
+ if (!key) {
304
+ return func()
305
+ }
306
+
307
+ if (Object.hasOwn(runOnceCache, key)) {
308
+ return runOnceCache[key]
309
+ }
310
+
311
+ const result = func()
312
+ runOnceCache[key] = result
313
+ return result
314
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",