提供更方便的脚本操作接口

This commit is contained in:
Peng-YM 2020-09-15 11:13:56 +08:00
parent a0d8e79d3c
commit 910f2c1b67
2 changed files with 38 additions and 75 deletions

View File

@ -202,11 +202,6 @@ async function parseSub(sub, platform) {
$.info(`Parsers initialized.`); $.info(`Parsers initialized.`);
let proxies = $parser.parse(raw); let proxies = $parser.parse(raw);
// filters
const $filter = ProxyFilter();
// operators
const $operator = ProxyOperator();
for (const item of sub.process || []) { for (const item of sub.process || []) {
let script; let script;
// process script // process script
@ -234,13 +229,14 @@ async function parseSub(sub, platform) {
JSON.stringify(item.args) || "None" JSON.stringify(item.args) || "None"
}` }`
); );
if (item.type.indexOf("Script") !== -1) {
$filter.setFilter(filter(script));
} else {
$filter.setFilter(filter(item.args));
}
try { try {
proxies = $filter.process(proxies); if (item.type.indexOf("Script") !== -1) {
proxies = processFilter(filter(script), proxies);
} else {
proxies = processFilter(filter(item.args), proxies);
}
} catch (err) { } catch (err) {
$.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`); $.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`);
} }
@ -253,13 +249,12 @@ async function parseSub(sub, platform) {
JSON.stringify(item.args) || "None" JSON.stringify(item.args) || "None"
}` }`
); );
if (item.type.indexOf("Script") !== -1) {
$operator.setOperator(operator(script));
} else {
$operator.setOperator(operator(item.args));
}
try { try {
proxies = $operator.process(proxies); if (item.type.indexOf("Script") !== -1) {
proxies = processOperator(operator(script), proxies);
} else {
proxies = processOperator(operator(item.args), proxies);
}
} catch (err) { } catch (err) {
`Failed to apply operator "${item.type}"!\n REASON: ${err}`; `Failed to apply operator "${item.type}"!\n REASON: ${err}`;
} }
@ -729,53 +724,28 @@ function ProxyParser(targetPlatform) {
}; };
} }
function ProxyFilter() { function processFilter(filter, proxies) {
let filter;
function setFilter(arg) {
filter = arg;
}
// select proxies // select proxies
function process(proxies) { let selected = FULL(proxies.length, true);
let selected = FULL(proxies.length, true); try {
try { selected = AND(selected, filter.func(proxies));
selected = AND(selected, filter.func(proxies)); } catch (err) {
} catch (err) { console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
}
return proxies.filter((_, i) => selected[i]);
} }
return proxies.filter((_, i) => selected[i]);
return {
process,
setFilter,
};
} }
function ProxyOperator() { function processOperator(operator, proxies) {
let operator; let output = objClone(proxies);
try {
function setOperator(arg) { const output_ = operator.func(output);
operator = arg; if (output_) output = output_;
} catch (err) {
// print log and skip this operator
console.log(`ERROR: cannot apply operator ${op.name}! Reason: ${err}`);
} }
// run all operators return output;
function process(proxies) {
let output = objClone(proxies);
try {
const output_ = operator.func(output);
if (output_) output = output_;
} catch (err) {
// print log and skip this operator
console.log(`ERROR: cannot apply operator ${op.name}! Reason: ${err}`);
}
return output;
}
return { setOperator, process };
} }
/**************************** URI Format ***************************************/ /**************************** URI Format ***************************************/
@ -1799,7 +1769,6 @@ function URI_Producer() {
); );
break; break;
default: default:
console.log(`FUCK`);
throw new Error(`Unsupported plugin option: ${proxy.plugin}`); throw new Error(`Unsupported plugin option: ${proxy.plugin}`);
} }
} }
@ -2018,13 +1987,16 @@ function ScriptOperator(script) {
(function () { (function () {
// interface to get internal operators // interface to get internal operators
const $get = (name, args) => { const $get = (name, args) => {
const operator = AVAILABLE_OPERATORS[name]; const item = AVAILABLE_OPERATORS[name] || AVAILABLE_FILTERS[name];
if (operator) { return item(args);
return operator(args).func;
} else {
throw new Error(`No operator named ${name} is found!`);
}
}; };
const $process = (item, proxies) => {
if (item.name.indexOf("Filter") !== -1) {
return processOperator(item, proxies);
} else if (item.name.indexOf("Operator") !== -1) {
return processFilter(item, proxies);
}
}
eval(script); eval(script);
output = operator(proxies); output = operator(proxies);
})(); })();
@ -2132,15 +2104,6 @@ function ScriptFilter(script) {
func: (proxies) => { func: (proxies) => {
let output = FULL(proxies.length, true); let output = FULL(proxies.length, true);
!(function () { !(function () {
// interface to get internal filters
const $get = (name, args) => {
const filter = AVAILABLE_FILTERS[name];
if (filter) {
return filter(args).func;
} else {
throw new Error(`No filter named ${name} is found!`);
}
};
eval(script); eval(script);
output = filter(proxies); output = filter(proxies);
})(); })();

File diff suppressed because one or more lines are too long