browser-extension-utils 0.2.0 → 0.2.1

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
@@ -142,11 +142,19 @@ export function isUrl(text: string | undefined): boolean
142
142
  export function throttle(func: Function, interval: number): Function
143
143
 
144
144
  export type MenuCallback = (event?: MouseEvent | KeyboardEvent) => void
145
+ export type RegisterMenuCommandOptions = {
146
+ id?: string
147
+ title?: string
148
+ autoClose?: boolean
149
+ // O - Tampermonkey
150
+ // X - Violentmonkey
151
+ accessKey?: string
152
+ }
145
153
  export function registerMenuCommand(
146
154
  name: string,
147
155
  callback: MenuCallback,
148
- accessKey?: string
149
- ): void
156
+ options?: RegisterMenuCommandOptions
157
+ ): string | undefined
150
158
 
151
159
  export function extendHistoryApi(): void
152
160
 
@@ -195,3 +203,5 @@ export function parseInt10(
195
203
 
196
204
  // eslint-disable-next-line @typescript-eslint/naming-convention
197
205
  export function createHTML(html: string): string
206
+
207
+ export function compareVersions(v1: string, v2: string): number
package/lib/index.js CHANGED
@@ -499,3 +499,50 @@ const escapeHTMLPolicy =
499
499
  export const createHTML = (html) => {
500
500
  return escapeHTMLPolicy ? escapeHTMLPolicy.createHTML(html) : html
501
501
  }
502
+
503
+ /**
504
+ * Compare two semantic version strings
505
+ * @param {string} v1 - First version string (e.g., "1.2.0")
506
+ * @param {string} v2 - Second version string (e.g., "1.1.5")
507
+ * @returns {number} - Returns 1 if v1 > v2, -1 if v1 < v2, 0 if equal
508
+ * @throws {Error} - Throws error for invalid version strings
509
+ */
510
+ export function compareVersions(v1, v2) {
511
+ // Input validation
512
+ if (typeof v1 !== "string" || typeof v2 !== "string") {
513
+ throw new TypeError("Version strings must be of type string")
514
+ }
515
+
516
+ if (!v1.trim() || !v2.trim()) {
517
+ throw new Error("Version strings cannot be empty")
518
+ }
519
+
520
+ // Validate version format (basic semantic versioning)
521
+ const versionRegex = /^\d+(\.\d+)*$/
522
+ if (!versionRegex.test(v1) || !versionRegex.test(v2)) {
523
+ throw new Error(
524
+ "Invalid version format. Use semantic versioning (e.g., '1.2.3')"
525
+ )
526
+ }
527
+
528
+ const v1Parts = v1.split(".").map(Number)
529
+ const v2Parts = v2.split(".").map(Number)
530
+ const maxLength = Math.max(v1Parts.length, v2Parts.length)
531
+
532
+ for (let i = 0; i < maxLength; i++) {
533
+ const num1 = v1Parts[i] || 0 // Use logical OR for cleaner default assignment
534
+ const num2 = v2Parts[i] || 0
535
+
536
+ if (num1 !== num2) {
537
+ return num1 > num2 ? 1 : -1 // Simplified comparison
538
+ }
539
+ }
540
+
541
+ return 0 // Versions are equal
542
+ }
543
+
544
+ // Usage:
545
+ // console.log(compareVersions("1.2.0", "1.1.5")); // Output: 1
546
+ // console.log(compareVersions("1.0", "1.0.0")); // Output: 0
547
+ // console.log(compareVersions("2.0", "1.5.10")); // Output: 1
548
+ // console.log(compareVersions("1.0.0", "1.0.0.1")); // Output: -1
package/lib/userscript.js CHANGED
@@ -71,7 +71,7 @@ export const addStyle = (styleText) =>
71
71
  addElement(null, "style", { textContent: styleText })
72
72
 
73
73
  // Only register menu on top frame
74
- export const registerMenuCommand = (name, callback, accessKey) => {
74
+ export const registerMenuCommand = (name, callback, options) => {
75
75
  if (globalThis !== top) {
76
76
  return
77
77
  }
@@ -81,6 +81,6 @@ export const registerMenuCommand = (name, callback, accessKey) => {
81
81
  return
82
82
  }
83
83
 
84
- GM.registerMenuCommand(name, callback, accessKey)
84
+ return GM.registerMenuCommand(name, callback, options)
85
85
  }
86
86
  /* eslint-enable new-cap, camelcase */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-extension-utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Utilities for developing browser extensions and userscripts",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",