diff --git a/backend/package.json b/backend/package.json index ea26a661..fad60236 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.20.58", + "version": "2.20.59", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index d4638246..603ab1f8 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -99,6 +99,7 @@ async function processFn( source, $options, ) { + let context = {}; for (const item of operators) { if (item.disabled) { $.log( @@ -222,6 +223,7 @@ async function processFn( $arguments, source, $options, + context, ); } else { processor = PROXY_PROCESSORS[item.type](item.args || {}); diff --git a/backend/src/core/proxy-utils/processors/index.js b/backend/src/core/proxy-utils/processors/index.js index 9c2cd1cb..9b04aa9f 100644 --- a/backend/src/core/proxy-utils/processors/index.js +++ b/backend/src/core/proxy-utils/processors/index.js @@ -371,7 +371,16 @@ function RegexDeleteOperator(regex) { 1. This function name should be `operator`! 2. Always declare variables before using them! */ -function ScriptOperator(script, targetPlatform, $arguments, source, $options) { +function ScriptOperator( + script, + targetPlatform, + $arguments, + source, + $options, + context, +) { + context.source = source; + context.env = env; return { name: 'Script Operator', func: async (proxies) => { @@ -423,7 +432,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) { $arguments, $options, ); - output = operator(proxies, targetPlatform, { source, ...env }); + output = operator(proxies, targetPlatform, context); })(); return output; }, @@ -432,7 +441,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) { await (async function () { const operator = createDynamicFunction( 'operator', - `async function operator(input = []) { + `async function operator(input = [], targetPlatform, context) { if (input && (input.$files || input.$content)) { let { $content, $files, $options, $file } = input if($file.type === 'mihomoProfile') { @@ -475,7 +484,7 @@ function ScriptOperator(script, targetPlatform, $arguments, source, $options) { $arguments, $options, ); - output = operator(proxies, targetPlatform, { source, ...env }); + output = operator(proxies, targetPlatform, context); })(); return output; }, @@ -949,7 +958,16 @@ function TypeFilter(input) { 1. This function name should be `filter`! 2. Always declare variables before using them! */ -function ScriptFilter(script, targetPlatform, $arguments, source, $options) { +function ScriptFilter( + script, + targetPlatform, + $arguments, + source, + $options, + context, +) { + context.source = source; + context.env = env; return { name: 'Script Filter', func: async (proxies) => { @@ -961,7 +979,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) { $arguments, $options, ); - output = filter(proxies, targetPlatform, { source, ...env }); + output = filter(proxies, targetPlatform, context); })(); return output; }, @@ -970,7 +988,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) { await (async function () { const filter = createDynamicFunction( 'filter', - `async function filter(input = []) { + `async function filter(input = [], targetPlatform, context) { let proxies = input let list = [] const fn = async ($server) => { @@ -984,7 +1002,7 @@ function ScriptFilter(script, targetPlatform, $arguments, source, $options) { $arguments, $options, ); - output = filter(proxies, targetPlatform, { source, ...env }); + output = filter(proxies, targetPlatform, context); })(); return output; }, diff --git a/scripts/demo.js b/scripts/demo.js index d1e93b50..f26a7a7f 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -257,7 +257,9 @@ function operator(proxies = [], targetPlatform, context) { // 可参考: // 1. https://t.me/zhetengsha/948 - // context 为传入的上下文 + // context 为传入的上下文, 可在多个脚本中共享使用 + // 其中 env 为 环境信息, 包含运行版本和其他后端信息 + // 其中 source 为 订阅和组合订阅的数据, 有三种情况, 按需判断 (若只需要取订阅/组合订阅名称 直接用 `_subName` `_subDisplayName` `_collectionName` `_collectionDisplayName` 即可) // 若存在 `source._collection` 且 `source._collection.subscriptions` 中的 key 在 `source` 上也存在, 说明输出结果为组合订阅, 但是脚本设置在单条订阅上