browser-extension-utils 0.2.0 → 0.2.2
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/README.md +1 -3
- package/lib/index.d.ts +12 -2
- package/lib/index.js +47 -0
- package/lib/userscript.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,4 @@ Copyright (c) 2023 [Pipecraft](https://www.pipecraft.net). Licensed under the [M
|
|
|
19
19
|
## >\_
|
|
20
20
|
|
|
21
21
|
[](https://www.pipecraft.net)
|
|
22
|
-
[](https://utags.
|
|
23
|
-
[](https://dto.pipecraft.net)
|
|
24
|
-
[](https://www.bestxtools.com)
|
|
22
|
+
[](https://utags.link)
|
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 | number
|
|
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
|
-
|
|
149
|
-
):
|
|
156
|
+
options?: RegisterMenuCommandOptions
|
|
157
|
+
): Promise<string | number | 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,
|
|
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,
|
|
84
|
+
return GM.registerMenuCommand(name, callback, options)
|
|
85
85
|
}
|
|
86
86
|
/* eslint-enable new-cap, camelcase */
|