@nsnanocat/util 0.0.0-preview-ce381a0 → 0.0.0-preview-1422296
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/dist/getStorage.mjs +64 -47
- package/dist/index.d.ts +3 -11
- package/dist/index.js +3 -11
- package/dist/lib/app.mjs +25 -9
- package/dist/lib/done.mjs +58 -48
- package/dist/lib/environment.mjs +15 -16
- package/dist/lib/index.d.mts +5 -0
- package/dist/lib/index.mjs +5 -5
- package/dist/lib/notification.mjs +50 -29
- package/dist/lib/runScript.mjs +27 -18
- package/dist/lib/time.mjs +17 -4
- package/dist/lib/wait.mjs +5 -3
- package/dist/polyfill/Console.mjs +171 -145
- package/dist/polyfill/Lodash.mjs +39 -29
- package/dist/polyfill/StatusTexts.mjs +2 -3
- package/dist/polyfill/Storage.d.mts +12 -0
- package/dist/polyfill/Storage.mjs +167 -173
- package/dist/polyfill/fetch-node.d.mts +3 -0
- package/dist/polyfill/fetch-node.mjs +2 -2
- package/dist/polyfill/fetch.mjs +115 -79
- package/dist/polyfill/index.d.mts +5 -0
- package/dist/polyfill/index.mjs +5 -5
- package/package.json +4 -4
- package/dist/lib/index.d.ts +0 -5
- package/dist/polyfill/Storage.d.ts +0 -8
- package/dist/polyfill/fetch-node.d.ts +0 -1
- package/dist/polyfill/index.d.ts +0 -5
- /package/dist/{getStorage.d.ts → getStorage.d.mts} +0 -0
- /package/dist/lib/{app.d.ts → app.d.mts} +0 -0
- /package/dist/lib/{done.d.ts → done.d.mts} +0 -0
- /package/dist/lib/{environment.d.ts → environment.d.mts} +0 -0
- /package/dist/lib/{notification.d.ts → notification.d.mts} +0 -0
- /package/dist/lib/{runScript.d.ts → runScript.d.mts} +0 -0
- /package/dist/lib/{time.d.ts → time.d.mts} +0 -0
- /package/dist/lib/{wait.d.ts → wait.d.mts} +0 -0
- /package/dist/polyfill/{Console.d.ts → Console.d.mts} +0 -0
- /package/dist/polyfill/{Lodash.d.ts → Lodash.d.mts} +0 -0
- /package/dist/polyfill/{StatusTexts.d.ts → StatusTexts.d.mts} +0 -0
- /package/dist/polyfill/{fetch.d.ts → fetch.d.mts} +0 -0
package/dist/getStorage.mjs
CHANGED
|
@@ -1,62 +1,79 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
function getStorage(key, names, database) {
|
|
4
|
-
|
|
5
|
-
const nameList = Array.isArray(names) ? names : [
|
|
6
|
-
names
|
|
7
|
-
];
|
|
1
|
+
import { set } from './polyfill/Lodash.mjs';
|
|
2
|
+
import { Storage } from './polyfill/Storage.mjs';
|
|
3
|
+
export function getStorage(key, names, database) {
|
|
4
|
+
const nameList = Array.isArray(names) ? names : [names];
|
|
8
5
|
const Store = {
|
|
9
|
-
Settings:
|
|
10
|
-
Configs:
|
|
11
|
-
Caches: {}
|
|
6
|
+
Settings: database?.Default?.Settings || {},
|
|
7
|
+
Configs: database?.Default?.Configs || {},
|
|
8
|
+
Caches: {},
|
|
12
9
|
};
|
|
13
|
-
nameList.forEach((name)=>{
|
|
14
|
-
|
|
15
|
-
Store.
|
|
16
|
-
...Store.Settings,
|
|
17
|
-
...null == database ? void 0 : null === (_database_name = database[name]) || void 0 === _database_name ? void 0 : _database_name.Settings
|
|
18
|
-
};
|
|
19
|
-
Store.Configs = {
|
|
20
|
-
...Store.Configs,
|
|
21
|
-
...null == database ? void 0 : null === (_database_name1 = database[name]) || void 0 === _database_name1 ? void 0 : _database_name1.Configs
|
|
22
|
-
};
|
|
10
|
+
nameList.forEach((name) => {
|
|
11
|
+
Store.Settings = { ...Store.Settings, ...database?.[name]?.Settings };
|
|
12
|
+
Store.Configs = { ...Store.Configs, ...database?.[name]?.Configs };
|
|
23
13
|
});
|
|
24
|
-
if (
|
|
25
|
-
const parsedArgument = Object.fromEntries($argument.split('&').map((item)=>item.split('=', 2).map((i)=>i.replace(/\"/g, ''))));
|
|
26
|
-
Object.keys(parsedArgument).forEach((key)=>
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
14
|
+
if (typeof $argument === 'string') {
|
|
15
|
+
const parsedArgument = Object.fromEntries($argument.split('&').map((item) => item.split('=', 2).map((i) => i.replace(/\"/g, ''))));
|
|
16
|
+
Object.keys(parsedArgument).forEach((key) => set(Store.Settings, key, parsedArgument[key]));
|
|
17
|
+
}
|
|
18
|
+
else if (typeof $argument === 'object') {
|
|
19
|
+
Object.keys($argument).forEach((key) => set(Store.Settings, key, $argument[key]));
|
|
20
|
+
}
|
|
21
|
+
/***************** BoxJs *****************/
|
|
22
|
+
// 包装为局部变量,用完释放内存
|
|
23
|
+
// BoxJs的清空操作返回假值空字符串, 逻辑或操作符会在左侧操作数为假值时返回右侧操作数。
|
|
24
|
+
const BoxJs = Storage.getItem(key);
|
|
25
|
+
if (BoxJs) {
|
|
26
|
+
//Console.debug("BoxJs", `BoxJs类型: ${typeof BoxJs}`, `BoxJs内容: ${JSON.stringify(BoxJs || {})}`);
|
|
27
|
+
nameList.forEach((name) => {
|
|
28
|
+
const boxSettings = BoxJs?.[name]?.Settings;
|
|
29
|
+
const boxCaches = BoxJs?.[name]?.Caches;
|
|
30
|
+
if (typeof boxSettings === 'string') {
|
|
31
|
+
BoxJs[name].Settings = JSON.parse(boxSettings || '{}');
|
|
32
|
+
}
|
|
33
|
+
if (boxSettings) {
|
|
34
|
+
Store.Settings = { ...Store.Settings, ...BoxJs[name].Settings };
|
|
35
|
+
}
|
|
36
|
+
if (typeof boxCaches === 'string') {
|
|
37
|
+
BoxJs[name].Caches = JSON.parse(boxCaches || '{}');
|
|
38
|
+
}
|
|
39
|
+
if (boxCaches) {
|
|
40
|
+
Store.Caches = { ...Store.Caches, ...BoxJs[name].Caches };
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/***************** traverseObject *****************/
|
|
45
|
+
traverseObject(Store.Settings, (key, value) => {
|
|
46
|
+
//Console.debug("☑️ traverseObject", `${key}: ${typeof value}`, `${key}: ${JSON.stringify(value)}`);
|
|
45
47
|
let transformedValue = value;
|
|
46
|
-
if ('true'
|
|
47
|
-
|
|
48
|
+
if (transformedValue === 'true' || transformedValue === 'false')
|
|
49
|
+
transformedValue = JSON.parse(transformedValue); // 字符串转Boolean
|
|
50
|
+
else if (typeof transformedValue === 'string') {
|
|
51
|
+
if (transformedValue.includes(','))
|
|
52
|
+
transformedValue = transformedValue.split(',').map((item) => string2number(item)); // 字符串转数组转数字
|
|
53
|
+
else
|
|
54
|
+
transformedValue = string2number(transformedValue); // 字符串转数字
|
|
55
|
+
}
|
|
48
56
|
return transformedValue;
|
|
49
57
|
});
|
|
50
58
|
return Store;
|
|
51
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Recursively traverse and transform object properties.
|
|
62
|
+
*/
|
|
52
63
|
function traverseObject(obj, callback) {
|
|
53
|
-
Object.entries(obj).forEach(([key, value])=>{
|
|
54
|
-
if (value &&
|
|
55
|
-
|
|
64
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
65
|
+
if (value && typeof value === 'object') {
|
|
66
|
+
obj[key] = traverseObject(value, callback);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
obj[key] = callback(key, value);
|
|
70
|
+
}
|
|
56
71
|
});
|
|
57
72
|
return obj;
|
|
58
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Convert string to number if applicable.
|
|
76
|
+
*/
|
|
59
77
|
function string2number(value) {
|
|
60
78
|
return /^\d+$/.test(value) ? Number(value) : value;
|
|
61
79
|
}
|
|
62
|
-
export { getStorage };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
export * from './lib/
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './lib/time';
|
|
5
|
-
export * from './lib/wait';
|
|
6
|
-
export * from './polyfill/Console';
|
|
7
|
-
export * from './polyfill/fetch';
|
|
8
|
-
export * from './polyfill/Lodash';
|
|
9
|
-
export * from './polyfill/StatusTexts';
|
|
10
|
-
export * from './polyfill/Storage';
|
|
11
|
-
export * from './getStorage';
|
|
1
|
+
export * from './lib/index.mjs';
|
|
2
|
+
export * from './polyfill/index.mjs';
|
|
3
|
+
export * from './getStorage.mjs';
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from "./lib/time.mjs";
|
|
5
|
-
export * from "./lib/wait.mjs";
|
|
6
|
-
export * from "./polyfill/Console.mjs";
|
|
7
|
-
export * from "./polyfill/fetch.mjs";
|
|
8
|
-
export * from "./polyfill/Lodash.mjs";
|
|
9
|
-
export * from "./polyfill/StatusTexts.mjs";
|
|
10
|
-
export * from "./polyfill/Storage.mjs";
|
|
11
|
-
export * from "./getStorage.mjs";
|
|
1
|
+
export * from './lib/index.mjs';
|
|
2
|
+
export * from './polyfill/index.mjs';
|
|
3
|
+
export * from './getStorage.mjs';
|
package/dist/lib/app.mjs
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if ('
|
|
1
|
+
/**
|
|
2
|
+
* Current app name
|
|
3
|
+
*/
|
|
4
|
+
export const $app = (() => {
|
|
5
|
+
if ('$task' in globalThis) {
|
|
6
|
+
return 'Quantumult X';
|
|
7
|
+
}
|
|
8
|
+
if ('$loon' in globalThis) {
|
|
9
|
+
return 'Loon';
|
|
10
|
+
}
|
|
11
|
+
if ('$rocket' in globalThis) {
|
|
12
|
+
return 'Shadowrocket';
|
|
13
|
+
}
|
|
14
|
+
if ('Egern' in globalThis) {
|
|
15
|
+
return 'Egern';
|
|
16
|
+
}
|
|
6
17
|
if ('$environment' in globalThis) {
|
|
7
|
-
if (globalThis.$environment['surge-version'])
|
|
8
|
-
|
|
18
|
+
if (globalThis.$environment['surge-version']) {
|
|
19
|
+
return 'Surge';
|
|
20
|
+
}
|
|
21
|
+
if (globalThis.$environment['stash-version']) {
|
|
22
|
+
return 'Stash';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (typeof module !== 'undefined') {
|
|
26
|
+
return 'Node.js';
|
|
9
27
|
}
|
|
10
|
-
return 'Node.js';
|
|
11
28
|
})();
|
|
12
|
-
export { $app };
|
package/dist/lib/done.mjs
CHANGED
|
@@ -1,46 +1,62 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const transformQuantumultXBody = (object)=>{
|
|
6
|
-
if (object.body instanceof ArrayBuffer)
|
|
7
|
-
bodyBytes: object.body,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
1
|
+
import { pick, set } from '../polyfill/Lodash.mjs';
|
|
2
|
+
import { Console } from '../polyfill/Console.mjs';
|
|
3
|
+
import { StatusTexts } from '../polyfill/StatusTexts.mjs';
|
|
4
|
+
import { $app } from './app.mjs';
|
|
5
|
+
const transformQuantumultXBody = (object) => {
|
|
6
|
+
if (object.body instanceof ArrayBuffer) {
|
|
7
|
+
return { bodyBytes: object.body, body: undefined };
|
|
8
|
+
}
|
|
9
|
+
if (ArrayBuffer.isView(object.body)) {
|
|
10
|
+
return {
|
|
11
|
+
bodyBytes: object.body.buffer.slice(object.body.byteOffset, object.body.byteOffset + object.body.byteLength),
|
|
12
|
+
body: undefined,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
return { bodyBytes: undefined };
|
|
17
16
|
};
|
|
18
|
-
const transformQuantumultXStatus = (object)=>{
|
|
19
|
-
if (
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
const transformQuantumultXStatus = (object) => {
|
|
18
|
+
if (typeof object.status === 'number') {
|
|
19
|
+
return {
|
|
20
|
+
status: `HTTP/1.1 ${object.status} ${StatusTexts[object.status]}`,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
if (typeof object.status !== 'string' && object.status !== undefined) {
|
|
24
|
+
throw new TypeError(`${done.name}: 参数类型错误, status 必须为数字或字符串`);
|
|
25
|
+
}
|
|
23
26
|
return {};
|
|
24
27
|
};
|
|
25
|
-
const handleDoneFactory = (startTime)=>
|
|
26
|
-
|
|
28
|
+
const handleDoneFactory = (startTime) => {
|
|
29
|
+
return (result) => {
|
|
30
|
+
Console.log('🚩 执行结束!', startTime ? `🕛 ${((Date.now() - startTime) / 1000).toFixed(3)} 秒` : undefined);
|
|
27
31
|
$done(result);
|
|
28
32
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Complete the script execution
|
|
36
|
+
*/
|
|
37
|
+
export function done(object = {}) {
|
|
38
|
+
let startTime = $script?.startTime;
|
|
39
|
+
if ($app === 'Surge') {
|
|
40
|
+
startTime *= 1000;
|
|
41
|
+
}
|
|
32
42
|
const handleDone = handleDoneFactory(startTime);
|
|
33
|
-
switch(
|
|
43
|
+
switch ($app) {
|
|
34
44
|
case 'Surge':
|
|
35
|
-
if (object.policy)
|
|
45
|
+
if (object.policy) {
|
|
46
|
+
set(object, 'headers.X-Surge-Policy', object.policy);
|
|
47
|
+
}
|
|
36
48
|
handleDone(object);
|
|
37
49
|
break;
|
|
38
50
|
case 'Loon':
|
|
39
|
-
if (object.policy)
|
|
51
|
+
if (object.policy) {
|
|
52
|
+
object.node = object.policy;
|
|
53
|
+
}
|
|
40
54
|
handleDone(object);
|
|
41
55
|
break;
|
|
42
56
|
case 'Stash':
|
|
43
|
-
if (object.policy)
|
|
57
|
+
if (object.policy) {
|
|
58
|
+
set(object, 'headers.X-Stash-Selected-Proxy', encodeURI(object.policy));
|
|
59
|
+
}
|
|
44
60
|
handleDone(object);
|
|
45
61
|
break;
|
|
46
62
|
case 'Egern':
|
|
@@ -49,27 +65,21 @@ function done(object = {}) {
|
|
|
49
65
|
case 'Shadowrocket':
|
|
50
66
|
handleDone(object);
|
|
51
67
|
break;
|
|
52
|
-
case 'Quantumult X':
|
|
53
|
-
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
'bodyBytes'
|
|
61
|
-
]),
|
|
62
|
-
...transformQuantumultXStatus(object),
|
|
63
|
-
...transformQuantumultXBody(object)
|
|
64
|
-
};
|
|
65
|
-
if (object.policy) (0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(transformedObject, 'opts.policy', object.policy);
|
|
66
|
-
handleDone(transformedObject);
|
|
67
|
-
break;
|
|
68
|
+
case 'Quantumult X': {
|
|
69
|
+
const transformedObject = {
|
|
70
|
+
...pick(object, ['status', 'url', 'headers', 'body', 'bodyBytes']),
|
|
71
|
+
...transformQuantumultXStatus(object),
|
|
72
|
+
...transformQuantumultXBody(object),
|
|
73
|
+
};
|
|
74
|
+
if (object.policy) {
|
|
75
|
+
set(transformedObject, 'opts.policy', object.policy);
|
|
68
76
|
}
|
|
77
|
+
handleDone(transformedObject);
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
69
80
|
case 'Node.js':
|
|
70
81
|
default:
|
|
71
|
-
|
|
82
|
+
Console.log('🚩 执行结束!');
|
|
72
83
|
process.exit(1);
|
|
73
84
|
}
|
|
74
85
|
}
|
|
75
|
-
export { done };
|
package/dist/lib/environment.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
function
|
|
3
|
-
switch(
|
|
1
|
+
import { $app } from './app.mjs';
|
|
2
|
+
export function environment() {
|
|
3
|
+
switch ($app) {
|
|
4
4
|
case 'Surge':
|
|
5
5
|
$environment.app = 'Surge';
|
|
6
6
|
return $environment;
|
|
@@ -10,27 +10,26 @@ function environment_rslib_entry_environment() {
|
|
|
10
10
|
case 'Egern':
|
|
11
11
|
$environment.app = 'Egern';
|
|
12
12
|
return $environment;
|
|
13
|
-
case 'Loon':
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
13
|
+
case 'Loon': {
|
|
14
|
+
const environment = $loon.split(' ');
|
|
15
|
+
return {
|
|
16
|
+
device: environment[0],
|
|
17
|
+
ios: environment[1],
|
|
18
|
+
'loon-version': environment[2],
|
|
19
|
+
app: 'Loon',
|
|
20
|
+
};
|
|
21
|
+
}
|
|
23
22
|
case 'Quantumult X':
|
|
24
23
|
return {
|
|
25
|
-
app: 'Quantumult X'
|
|
24
|
+
app: 'Quantumult X',
|
|
26
25
|
};
|
|
27
26
|
case 'Node.js':
|
|
28
27
|
return {
|
|
29
28
|
...process.env,
|
|
30
|
-
app: 'Node.js'
|
|
29
|
+
app: 'Node.js',
|
|
31
30
|
};
|
|
32
31
|
default:
|
|
33
32
|
return {};
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
|
-
export
|
|
35
|
+
// export const $environment = environment();
|
package/dist/lib/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
1
|
+
export * from './app.mjs';
|
|
2
|
+
export * from './done.mjs';
|
|
3
|
+
export * from './notification.mjs';
|
|
4
|
+
export * from './time.mjs';
|
|
5
|
+
export * from './wait.mjs';
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
1
|
+
import { Console } from '../polyfill/Console.mjs';
|
|
2
|
+
import { $app } from './app.mjs';
|
|
3
|
+
/**
|
|
4
|
+
* 系统通知
|
|
5
|
+
*/
|
|
6
|
+
export function notification(title = `ℹ️ ${$app} 通知`, subtitle = '', body = '', content = {}) {
|
|
4
7
|
const mutableContent = getMutableContent(content);
|
|
5
|
-
switch(
|
|
8
|
+
switch ($app) {
|
|
6
9
|
case 'Quantumult X':
|
|
7
10
|
$notify(title, subtitle, body, mutableContent);
|
|
8
11
|
break;
|
|
@@ -12,29 +15,31 @@ function notification(title = `ℹ️ ${__WEBPACK_EXTERNAL_MODULE__app_mjs__.$ap
|
|
|
12
15
|
$notification.post(title, subtitle, body, mutableContent);
|
|
13
16
|
break;
|
|
14
17
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
Console.group('📣 系统通知');
|
|
19
|
+
Console.log(title, subtitle, body, JSON.stringify(mutableContent, null, 2));
|
|
20
|
+
Console.groupEnd();
|
|
18
21
|
}
|
|
19
22
|
function getMutableContent(content) {
|
|
20
23
|
const mutableContent = {};
|
|
21
|
-
switch(typeof content){
|
|
24
|
+
switch (typeof content) {
|
|
22
25
|
case 'string':
|
|
23
26
|
case 'number':
|
|
24
27
|
case 'boolean':
|
|
25
28
|
assignSimpleContent(mutableContent, content);
|
|
26
29
|
break;
|
|
27
30
|
case 'object':
|
|
28
|
-
if (content)
|
|
31
|
+
if (content) {
|
|
32
|
+
assignObjectContent(mutableContent, content);
|
|
33
|
+
}
|
|
29
34
|
break;
|
|
30
35
|
default:
|
|
31
|
-
|
|
36
|
+
Console.error(`不支持的通知参数类型: ${typeof content}`);
|
|
32
37
|
break;
|
|
33
38
|
}
|
|
34
39
|
return mutableContent;
|
|
35
40
|
}
|
|
36
41
|
function assignSimpleContent(mutableContent, content) {
|
|
37
|
-
switch(
|
|
42
|
+
switch ($app) {
|
|
38
43
|
case 'Quantumult X':
|
|
39
44
|
mutableContent['open-url'] = content;
|
|
40
45
|
break;
|
|
@@ -51,15 +56,20 @@ function assignObjectContent(mutableContent, content) {
|
|
|
51
56
|
const openUrl = content.open || content['open-url'] || content.url || content.openUrl;
|
|
52
57
|
const copyUrl = content.copy || content['update-pasteboard'] || content.updatePasteboard;
|
|
53
58
|
const mediaUrl = content.media || content['media-url'] || content.mediaUrl;
|
|
54
|
-
switch(
|
|
59
|
+
switch ($app) {
|
|
55
60
|
case 'Quantumult X':
|
|
56
|
-
if (openUrl)
|
|
57
|
-
|
|
58
|
-
if (
|
|
61
|
+
if (openUrl)
|
|
62
|
+
mutableContent['open-url'] = openUrl;
|
|
63
|
+
if (mediaUrl?.startsWith('http'))
|
|
64
|
+
mutableContent['media-url'] = mediaUrl;
|
|
65
|
+
if (copyUrl)
|
|
66
|
+
mutableContent['update-pasteboard'] = copyUrl;
|
|
59
67
|
break;
|
|
60
68
|
case 'Loon':
|
|
61
|
-
if (openUrl)
|
|
62
|
-
|
|
69
|
+
if (openUrl)
|
|
70
|
+
mutableContent.openUrl = openUrl;
|
|
71
|
+
if (mediaUrl?.startsWith('http'))
|
|
72
|
+
mutableContent.mediaUrl = mediaUrl;
|
|
63
73
|
break;
|
|
64
74
|
default:
|
|
65
75
|
if (openUrl) {
|
|
@@ -70,32 +80,43 @@ function assignObjectContent(mutableContent, content) {
|
|
|
70
80
|
mutableContent.action = 'clipboard';
|
|
71
81
|
mutableContent.text = copyUrl;
|
|
72
82
|
}
|
|
73
|
-
if (mediaUrl)
|
|
74
|
-
|
|
75
|
-
|
|
83
|
+
if (mediaUrl) {
|
|
84
|
+
handleMediaContent(mutableContent, mediaUrl, content.mime);
|
|
85
|
+
}
|
|
86
|
+
if (content['auto-dismiss'])
|
|
87
|
+
mutableContent['auto-dismiss'] = content['auto-dismiss'];
|
|
88
|
+
if (content.sound)
|
|
89
|
+
mutableContent.sound = content.sound;
|
|
76
90
|
break;
|
|
77
91
|
}
|
|
78
92
|
}
|
|
79
93
|
function handleMediaContent(mutableContent, mediaUrl, mime) {
|
|
80
|
-
if (mediaUrl.startsWith('http'))
|
|
94
|
+
if (mediaUrl.startsWith('http')) {
|
|
95
|
+
mutableContent['media-url'] = mediaUrl;
|
|
96
|
+
}
|
|
81
97
|
else if (mediaUrl.startsWith('data:')) {
|
|
82
98
|
const base64RegExp = /^data:(?<MIME>\w+\/\w+);base64,(?<Base64>.+)/;
|
|
83
99
|
const match = mediaUrl.match(base64RegExp);
|
|
84
|
-
if (
|
|
100
|
+
if (match?.groups) {
|
|
85
101
|
mutableContent['media-base64'] = match.groups.Base64;
|
|
86
102
|
mutableContent['media-base64-mime'] = mime || match.groups.MIME;
|
|
87
103
|
}
|
|
88
|
-
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
89
106
|
mutableContent['media-base64'] = mediaUrl;
|
|
90
107
|
mutableContent['media-base64-mime'] = detectMimeType(mediaUrl);
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
110
|
function detectMimeType(base64) {
|
|
94
|
-
if (base64.startsWith('JVBERi0'))
|
|
95
|
-
|
|
96
|
-
if (base64.startsWith('
|
|
97
|
-
|
|
98
|
-
if (base64.startsWith('
|
|
111
|
+
if (base64.startsWith('JVBERi0'))
|
|
112
|
+
return 'application/pdf';
|
|
113
|
+
if (base64.startsWith('R0lGODdh') || base64.startsWith('R0lGODlh'))
|
|
114
|
+
return 'image/gif';
|
|
115
|
+
if (base64.startsWith('iVBORw0KGgo'))
|
|
116
|
+
return 'image/png';
|
|
117
|
+
if (base64.startsWith('/9j/'))
|
|
118
|
+
return 'image/jpeg';
|
|
119
|
+
if (base64.startsWith('Qk02U'))
|
|
120
|
+
return 'image/bmp';
|
|
99
121
|
return 'application/octet-stream';
|
|
100
122
|
}
|
|
101
|
-
export { notification };
|
package/dist/lib/runScript.mjs
CHANGED
|
@@ -1,33 +1,42 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
async function runScript(script, runOpts = {}) {
|
|
1
|
+
import { Console } from '../polyfill/Console.mjs';
|
|
2
|
+
import { Storage } from '../polyfill/Storage.mjs';
|
|
3
|
+
import { fetch } from '../polyfill/fetch.mjs';
|
|
4
|
+
export async function runScript(script, runOpts = {}) {
|
|
5
5
|
try {
|
|
6
|
-
|
|
7
|
-
const httpapi =
|
|
8
|
-
if (!httpapi)
|
|
9
|
-
|
|
6
|
+
// 获取 httpapi 配置
|
|
7
|
+
const httpapi = Storage.getItem('@chavy_boxjs_userCfgs.httpapi')?.replace(/\n/g, '')?.trim();
|
|
8
|
+
if (!httpapi) {
|
|
9
|
+
throw new Error('httpapi 配置未找到,请检查配置项!');
|
|
10
|
+
}
|
|
11
|
+
// 设置超时时间,优先使用参数传入的值
|
|
12
|
+
const httpapiTimeoutFromConfig = Number.parseInt(Storage.getItem('@chavy_boxjs_userCfgs.httpapi_timeout') || '20', 10);
|
|
10
13
|
const timeout = runOpts.timeout ?? httpapiTimeoutFromConfig;
|
|
14
|
+
// 解析 httpapi 的地址和密码
|
|
11
15
|
const [password, address] = httpapi.split('@');
|
|
12
|
-
if (!password || !address)
|
|
16
|
+
if (!password || !address) {
|
|
17
|
+
throw new Error('httpapi 配置格式错误,应为 password@address 格式!');
|
|
18
|
+
}
|
|
19
|
+
// 构建请求对象
|
|
13
20
|
const request = {
|
|
14
21
|
url: `http://${address}/v1/scripting/evaluate`,
|
|
15
22
|
body: JSON.stringify({
|
|
16
23
|
script_text: script,
|
|
17
24
|
mock_type: 'cron',
|
|
18
|
-
timeout
|
|
25
|
+
timeout,
|
|
19
26
|
}),
|
|
20
27
|
headers: {
|
|
21
28
|
'X-Key': password,
|
|
22
|
-
Accept: '*/*'
|
|
29
|
+
Accept: '*/*',
|
|
23
30
|
},
|
|
24
|
-
timeout
|
|
31
|
+
timeout,
|
|
25
32
|
};
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
// 发起请求
|
|
34
|
+
const response = await fetch(request);
|
|
35
|
+
return response.body; // 返回响应体
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
// 捕获错误并打印
|
|
39
|
+
Console.error('运行脚本时发生错误:', error.message);
|
|
40
|
+
throw error; // 如果需要,可以重新抛出错误
|
|
31
41
|
}
|
|
32
42
|
}
|
|
33
|
-
export { runScript };
|
package/dist/lib/time.mjs
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* time
|
|
3
|
+
* 时间格式化
|
|
4
|
+
* [version of ISO8601]{@link https://262.ecma-international.org/5.1/#sec-15.9.1.15}
|
|
5
|
+
* 示例:time("yyyy-MM-dd qq HH:mm:ss.S") YYYY-MM-DDTHH:mm:ss.sssZ
|
|
6
|
+
* :time("yyyyMMddHHmmssS")
|
|
7
|
+
* YY:年 MM:月 dd:日 S:季 HH:时 m:分 ss:秒 sss:毫秒 Z:时区
|
|
8
|
+
* 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符
|
|
9
|
+
* @param {string} format 格式化参数
|
|
10
|
+
* @param {number} ts 可选: 根据指定时间戳返回格式化日期
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export function time(format, ts) {
|
|
2
14
|
const date = ts ? new Date(ts) : new Date();
|
|
3
15
|
const Time = {
|
|
4
16
|
YY: date.getFullYear().toString().substring(3),
|
|
@@ -9,10 +21,11 @@ function time(format, ts) {
|
|
|
9
21
|
mm: date.getMinutes().toString().padStart(2, '0'),
|
|
10
22
|
sss: date.getMilliseconds().toString().padStart(3, '0'),
|
|
11
23
|
ss: date.getSeconds().toString().padStart(2, '0'),
|
|
12
|
-
S: `${Math.floor(date.getMonth() / 3) + 1}
|
|
24
|
+
S: `${Math.floor(date.getMonth() / 3) + 1}`,
|
|
13
25
|
};
|
|
14
26
|
let result = format;
|
|
15
|
-
for (const [key, value] of Object.entries(Time))
|
|
27
|
+
for (const [key, value] of Object.entries(Time)) {
|
|
28
|
+
result = result.replace(key, value);
|
|
29
|
+
}
|
|
16
30
|
return result;
|
|
17
31
|
}
|
|
18
|
-
export { time };
|