@jeniex/utils 1.0.1 → 1.0.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/browser/index.cjs +91 -0
- package/browser/index.d.ts +37 -1
- package/browser/index.mjs +85 -1
- package/package.json +2 -1
package/browser/index.cjs
CHANGED
|
@@ -40,6 +40,27 @@ async function sleep(milliSeconds) {
|
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
const scriptInfo = typeof GM === 'undefined' ? GM_info : GM.info;
|
|
44
|
+
const SCRIPT_NAME = scriptInfo.script.name;
|
|
45
|
+
/** The identifier of the script to be used in logging */
|
|
46
|
+
const LOG_ID = `[${SCRIPT_NAME}]:`;
|
|
47
|
+
/** The initial tab URL on the script run */
|
|
48
|
+
const TAB_URL = window.location.href;
|
|
49
|
+
|
|
50
|
+
function alert(message) {
|
|
51
|
+
if (message === undefined) {
|
|
52
|
+
window.alert(`[ ${SCRIPT_NAME} ]`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
window.alert(`[ ${SCRIPT_NAME} ]\n\n${message}`);
|
|
56
|
+
}
|
|
57
|
+
function confirm(message) {
|
|
58
|
+
return window.confirm(`[ ${SCRIPT_NAME} ]\n\n${message}`);
|
|
59
|
+
}
|
|
60
|
+
function prompt(message, _default) {
|
|
61
|
+
return window.prompt(`[ ${SCRIPT_NAME} ]\n\n${message}`, _default);
|
|
62
|
+
}
|
|
63
|
+
|
|
43
64
|
function $(selectors, parent) {
|
|
44
65
|
const element = (parent ?? document).querySelector(ensureJoin(selectors));
|
|
45
66
|
if (element === null) {
|
|
@@ -55,9 +76,78 @@ function $$(selectors, parent) {
|
|
|
55
76
|
return elements;
|
|
56
77
|
}
|
|
57
78
|
|
|
79
|
+
function handleFlag(flag) {
|
|
80
|
+
const { type, name, message: header, values, default: defaultValue } = flag;
|
|
81
|
+
let message = header;
|
|
82
|
+
if (values !== undefined) {
|
|
83
|
+
message += `\n\n${values.join(' | ')}`;
|
|
84
|
+
}
|
|
85
|
+
if (type === 'number' && flag.range !== undefined) {
|
|
86
|
+
message += '\n';
|
|
87
|
+
const [min, max] = flag.range ?? [];
|
|
88
|
+
if (min !== undefined) {
|
|
89
|
+
message += `\nMinimum: ${min}`;
|
|
90
|
+
}
|
|
91
|
+
if (max !== undefined) {
|
|
92
|
+
message += `\nMaximum: ${max}`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const flagValue = prompt(message, defaultValue.toString());
|
|
96
|
+
if (flagValue === null || flagValue === '') {
|
|
97
|
+
throw new Error(`You did not provide a value for "${name}" option.`);
|
|
98
|
+
}
|
|
99
|
+
switch (type) {
|
|
100
|
+
case 'boolean': {
|
|
101
|
+
if (flagValue !== 'yes' && flagValue !== 'no') {
|
|
102
|
+
throw new Error(`Invalid value for "${name}" option!`);
|
|
103
|
+
}
|
|
104
|
+
return [name, flagValue === 'yes'];
|
|
105
|
+
}
|
|
106
|
+
case 'string': {
|
|
107
|
+
if (values !== undefined && !values.includes(flagValue)) {
|
|
108
|
+
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
109
|
+
}
|
|
110
|
+
return [name, flagValue];
|
|
111
|
+
}
|
|
112
|
+
case 'number': {
|
|
113
|
+
const [min, max] = flag.range ?? [];
|
|
114
|
+
const flagValueAsNumber = Number(flagValue);
|
|
115
|
+
if (Number.isNaN(flagValueAsNumber)) {
|
|
116
|
+
throw new TypeError(`You need to provide a number for "${name}" option!`);
|
|
117
|
+
}
|
|
118
|
+
if (values !== undefined && !values.includes(flagValueAsNumber)) {
|
|
119
|
+
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
120
|
+
}
|
|
121
|
+
if (min !== undefined && flagValueAsNumber < min) {
|
|
122
|
+
throw new Error(`Provided value for "${name}" option is less than the minimum.`);
|
|
123
|
+
}
|
|
124
|
+
if (max !== undefined && flagValueAsNumber > max) {
|
|
125
|
+
throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
|
|
126
|
+
}
|
|
127
|
+
return [name, flagValueAsNumber];
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
throw new Error('Something went wrong while handling a flag');
|
|
131
|
+
}
|
|
132
|
+
async function createOptions(flags) {
|
|
133
|
+
const options = {};
|
|
134
|
+
for (const flag of flags) {
|
|
135
|
+
const [name, value] = handleFlag(flag);
|
|
136
|
+
options[name] = value;
|
|
137
|
+
await sleep(300);
|
|
138
|
+
}
|
|
139
|
+
return options;
|
|
140
|
+
}
|
|
141
|
+
|
|
58
142
|
exports.$ = $;
|
|
59
143
|
exports.$$ = $$;
|
|
144
|
+
exports.LOG_ID = LOG_ID;
|
|
145
|
+
exports.SCRIPT_NAME = SCRIPT_NAME;
|
|
146
|
+
exports.TAB_URL = TAB_URL;
|
|
147
|
+
exports.alert = alert;
|
|
60
148
|
exports.asserted = asserted;
|
|
149
|
+
exports.confirm = confirm;
|
|
150
|
+
exports.createOptions = createOptions;
|
|
61
151
|
exports.ensureJoin = ensureJoin;
|
|
62
152
|
exports.isFalsy = isFalsy;
|
|
63
153
|
exports.isNotNullish = isNotNullish;
|
|
@@ -65,4 +155,5 @@ exports.isNullish = isNullish;
|
|
|
65
155
|
exports.isString = isString;
|
|
66
156
|
exports.isTruthy = isTruthy;
|
|
67
157
|
exports.noop = noop;
|
|
158
|
+
exports.prompt = prompt;
|
|
68
159
|
exports.sleep = sleep;
|
package/browser/index.d.ts
CHANGED
|
@@ -15,9 +15,45 @@ declare function sleep(milliSeconds: number): Promise<void>;
|
|
|
15
15
|
type Nullable<T> = T | null | undefined;
|
|
16
16
|
type Fn<T = any, U = void> = (param: T) => U;
|
|
17
17
|
|
|
18
|
+
declare const SCRIPT_NAME: string;
|
|
19
|
+
/** The identifier of the script to be used in logging */
|
|
20
|
+
declare const LOG_ID: string;
|
|
21
|
+
/** The initial tab URL on the script run */
|
|
22
|
+
declare const TAB_URL: string;
|
|
23
|
+
|
|
24
|
+
declare function alert(message?: string): void;
|
|
25
|
+
declare function confirm(message: string): boolean;
|
|
26
|
+
declare function prompt(message: string, _default?: string): string | null;
|
|
27
|
+
|
|
18
28
|
type Parent = Document | HTMLElement;
|
|
19
29
|
type Selectors = string[] | string;
|
|
20
30
|
declare function $<T = HTMLElement>(selectors: Selectors, parent?: Parent): T;
|
|
21
31
|
declare function $$<T extends Element = HTMLElement>(selectors: Selectors, parent?: Parent): NodeListOf<T>;
|
|
22
32
|
|
|
23
|
-
|
|
33
|
+
interface FlagBase {
|
|
34
|
+
name: string;
|
|
35
|
+
message: string;
|
|
36
|
+
}
|
|
37
|
+
interface FlagBoolean extends FlagBase {
|
|
38
|
+
type: 'boolean';
|
|
39
|
+
values?: ['no', 'yes'] | ['yes', 'no'];
|
|
40
|
+
default: 'no' | 'yes';
|
|
41
|
+
}
|
|
42
|
+
interface FlagString extends FlagBase {
|
|
43
|
+
type: 'string';
|
|
44
|
+
values?: string[];
|
|
45
|
+
default: string;
|
|
46
|
+
}
|
|
47
|
+
interface FlagNumber extends FlagBase {
|
|
48
|
+
type: 'number';
|
|
49
|
+
values?: number[];
|
|
50
|
+
range?: [number, number] | [number, undefined] | [undefined, number];
|
|
51
|
+
default: number;
|
|
52
|
+
}
|
|
53
|
+
type Flag = FlagBoolean | FlagNumber | FlagString;
|
|
54
|
+
|
|
55
|
+
type FlagValue = boolean | number | string;
|
|
56
|
+
type Options = Record<string, FlagValue>;
|
|
57
|
+
declare function createOptions(flags: Flag[]): Promise<Options>;
|
|
58
|
+
|
|
59
|
+
export { $, $$, Flag, Fn, LOG_ID, Nullable, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, createOptions, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
|
package/browser/index.mjs
CHANGED
|
@@ -38,6 +38,27 @@ async function sleep(milliSeconds) {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
const scriptInfo = typeof GM === 'undefined' ? GM_info : GM.info;
|
|
42
|
+
const SCRIPT_NAME = scriptInfo.script.name;
|
|
43
|
+
/** The identifier of the script to be used in logging */
|
|
44
|
+
const LOG_ID = `[${SCRIPT_NAME}]:`;
|
|
45
|
+
/** The initial tab URL on the script run */
|
|
46
|
+
const TAB_URL = window.location.href;
|
|
47
|
+
|
|
48
|
+
function alert(message) {
|
|
49
|
+
if (message === undefined) {
|
|
50
|
+
window.alert(`[ ${SCRIPT_NAME} ]`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
window.alert(`[ ${SCRIPT_NAME} ]\n\n${message}`);
|
|
54
|
+
}
|
|
55
|
+
function confirm(message) {
|
|
56
|
+
return window.confirm(`[ ${SCRIPT_NAME} ]\n\n${message}`);
|
|
57
|
+
}
|
|
58
|
+
function prompt(message, _default) {
|
|
59
|
+
return window.prompt(`[ ${SCRIPT_NAME} ]\n\n${message}`, _default);
|
|
60
|
+
}
|
|
61
|
+
|
|
41
62
|
function $(selectors, parent) {
|
|
42
63
|
const element = (parent ?? document).querySelector(ensureJoin(selectors));
|
|
43
64
|
if (element === null) {
|
|
@@ -53,4 +74,67 @@ function $$(selectors, parent) {
|
|
|
53
74
|
return elements;
|
|
54
75
|
}
|
|
55
76
|
|
|
56
|
-
|
|
77
|
+
function handleFlag(flag) {
|
|
78
|
+
const { type, name, message: header, values, default: defaultValue } = flag;
|
|
79
|
+
let message = header;
|
|
80
|
+
if (values !== undefined) {
|
|
81
|
+
message += `\n\n${values.join(' | ')}`;
|
|
82
|
+
}
|
|
83
|
+
if (type === 'number' && flag.range !== undefined) {
|
|
84
|
+
message += '\n';
|
|
85
|
+
const [min, max] = flag.range ?? [];
|
|
86
|
+
if (min !== undefined) {
|
|
87
|
+
message += `\nMinimum: ${min}`;
|
|
88
|
+
}
|
|
89
|
+
if (max !== undefined) {
|
|
90
|
+
message += `\nMaximum: ${max}`;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const flagValue = prompt(message, defaultValue.toString());
|
|
94
|
+
if (flagValue === null || flagValue === '') {
|
|
95
|
+
throw new Error(`You did not provide a value for "${name}" option.`);
|
|
96
|
+
}
|
|
97
|
+
switch (type) {
|
|
98
|
+
case 'boolean': {
|
|
99
|
+
if (flagValue !== 'yes' && flagValue !== 'no') {
|
|
100
|
+
throw new Error(`Invalid value for "${name}" option!`);
|
|
101
|
+
}
|
|
102
|
+
return [name, flagValue === 'yes'];
|
|
103
|
+
}
|
|
104
|
+
case 'string': {
|
|
105
|
+
if (values !== undefined && !values.includes(flagValue)) {
|
|
106
|
+
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
107
|
+
}
|
|
108
|
+
return [name, flagValue];
|
|
109
|
+
}
|
|
110
|
+
case 'number': {
|
|
111
|
+
const [min, max] = flag.range ?? [];
|
|
112
|
+
const flagValueAsNumber = Number(flagValue);
|
|
113
|
+
if (Number.isNaN(flagValueAsNumber)) {
|
|
114
|
+
throw new TypeError(`You need to provide a number for "${name}" option!`);
|
|
115
|
+
}
|
|
116
|
+
if (values !== undefined && !values.includes(flagValueAsNumber)) {
|
|
117
|
+
throw new Error(`Provided value for "${name}" option is not in the list.`);
|
|
118
|
+
}
|
|
119
|
+
if (min !== undefined && flagValueAsNumber < min) {
|
|
120
|
+
throw new Error(`Provided value for "${name}" option is less than the minimum.`);
|
|
121
|
+
}
|
|
122
|
+
if (max !== undefined && flagValueAsNumber > max) {
|
|
123
|
+
throw new Error(`Provided value for "${name}" option is greater than the maximum.`);
|
|
124
|
+
}
|
|
125
|
+
return [name, flagValueAsNumber];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
throw new Error('Something went wrong while handling a flag');
|
|
129
|
+
}
|
|
130
|
+
async function createOptions(flags) {
|
|
131
|
+
const options = {};
|
|
132
|
+
for (const flag of flags) {
|
|
133
|
+
const [name, value] = handleFlag(flag);
|
|
134
|
+
options[name] = value;
|
|
135
|
+
await sleep(300);
|
|
136
|
+
}
|
|
137
|
+
return options;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { $, $$, LOG_ID, SCRIPT_NAME, TAB_URL, alert, asserted, confirm, createOptions, ensureJoin, isFalsy, isNotNullish, isNullish, isString, isTruthy, noop, prompt, sleep };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeniex/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"author": "JenieX",
|
|
5
5
|
"homepage": "https://github.com/JenieX/utils",
|
|
6
6
|
"type": "module",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"@jeniex/eslint-config": "^0.0.3",
|
|
32
32
|
"@rollup/plugin-typescript": "^11.1.3",
|
|
33
33
|
"@types/node": "^20.5.7",
|
|
34
|
+
"@violentmonkey/types": "^0.1.5",
|
|
34
35
|
"rollup": "^3.28.0",
|
|
35
36
|
"rollup-plugin-dts": "^5.3.1",
|
|
36
37
|
"tslib": "^2.6.2"
|