Skip to content

Commit 6c553e4

Browse files
committed
feat: ⚠️ BREAKING CHANGE 脚本中的 第三个参数 context 改为脚本共享的上下文, 可自由操作. 原本的 env 信息, 现在为 context.env
1 parent 311a929 commit 6c553e4

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.20.58",
3+
"version": "2.20.59",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/core/proxy-utils/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ async function processFn(
9999
source,
100100
$options,
101101
) {
102+
let context = {};
102103
for (const item of operators) {
103104
if (item.disabled) {
104105
$.log(
@@ -222,6 +223,7 @@ async function processFn(
222223
$arguments,
223224
source,
224225
$options,
226+
context,
225227
);
226228
} else {
227229
processor = PROXY_PROCESSORS[item.type](item.args || {});

backend/src/core/proxy-utils/processors/index.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,16 @@ function RegexDeleteOperator(regex) {
371371
1. This function name should be `operator`!
372372
2. Always declare variables before using them!
373373
*/
374-
function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
374+
function ScriptOperator(
375+
script,
376+
targetPlatform,
377+
$arguments,
378+
source,
379+
$options,
380+
context,
381+
) {
382+
context.source = source;
383+
context.env = env;
375384
return {
376385
name: 'Script Operator',
377386
func: async (proxies) => {
@@ -423,7 +432,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
423432
$arguments,
424433
$options,
425434
);
426-
output = operator(proxies, targetPlatform, { source, ...env });
435+
output = operator(proxies, targetPlatform, context);
427436
})();
428437
return output;
429438
},
@@ -432,7 +441,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
432441
await (async function () {
433442
const operator = createDynamicFunction(
434443
'operator',
435-
`async function operator(input = []) {
444+
`async function operator(input = [], targetPlatform, context) {
436445
if (input && (input.$files || input.$content)) {
437446
let { $content, $files, $options, $file } = input
438447
if($file.type === 'mihomoProfile') {
@@ -475,7 +484,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) {
475484
$arguments,
476485
$options,
477486
);
478-
output = operator(proxies, targetPlatform, { source, ...env });
487+
output = operator(proxies, targetPlatform, context);
479488
})();
480489
return output;
481490
},
@@ -949,7 +958,16 @@ function TypeFilter(input) {
949958
1. This function name should be `filter`!
950959
2. Always declare variables before using them!
951960
*/
952-
function ScriptFilter(script, targetPlatform, $arguments, source, $options) {
961+
function ScriptFilter(
962+
script,
963+
targetPlatform,
964+
$arguments,
965+
source,
966+
$options,
967+
context,
968+
) {
969+
context.source = source;
970+
context.env = env;
953971
return {
954972
name: 'Script Filter',
955973
func: async (proxies) => {
@@ -961,7 +979,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) {
961979
$arguments,
962980
$options,
963981
);
964-
output = filter(proxies, targetPlatform, { source, ...env });
982+
output = filter(proxies, targetPlatform, context);
965983
})();
966984
return output;
967985
},
@@ -970,7 +988,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) {
970988
await (async function () {
971989
const filter = createDynamicFunction(
972990
'filter',
973-
`async function filter(input = []) {
991+
`async function filter(input = [], targetPlatform, context) {
974992
let proxies = input
975993
let list = []
976994
const fn = async ($server) => {
@@ -984,7 +1002,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) {
9841002
$arguments,
9851003
$options,
9861004
);
987-
output = filter(proxies, targetPlatform, { source, ...env });
1005+
output = filter(proxies, targetPlatform, context);
9881006
})();
9891007
return output;
9901008
},

scripts/demo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ function operator(proxies = [], targetPlatform, context) {
257257
// 可参考:
258258
// 1. https://t.me/zhetengsha/948
259259

260-
// context 为传入的上下文
260+
// context 为传入的上下文, 可在多个脚本中共享使用
261+
// 其中 env 为 环境信息, 包含运行版本和其他后端信息
262+
261263
// 其中 source 为 订阅和组合订阅的数据, 有三种情况, 按需判断 (若只需要取订阅/组合订阅名称 直接用 `_subName` `_subDisplayName` `_collectionName` `_collectionDisplayName` 即可)
262264

263265
// 若存在 `source._collection` 且 `source._collection.subscriptions` 中的 key 在 `source` 上也存在, 说明输出结果为组合订阅, 但是脚本设置在单条订阅上

0 commit comments

Comments
 (0)