Now the "Script Operator" and "Script Filter" support async functions

This commit is contained in:
Peng-YM 2022-06-02 23:58:46 +08:00
parent 072398938f
commit cc628788fc
3 changed files with 26 additions and 24 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,10 @@
/* eslint-disable no-case-declarations */ /* eslint-disable no-case-declarations */
// eslint-disable-next-line no-unused-vars
import { AND, FULL, OR, NOT } from '../utils/logical';
import { safeLoad } from 'static-js-yaml'; import { safeLoad } from 'static-js-yaml';
import { Base64 } from 'js-base64';
import { AND, FULL } from '../utils/logical';
import download from '../utils/download'; import download from '../utils/download';
import { getFlag } from '../utils/geo'; import { getFlag } from '../utils/geo';
import { Base64 } from 'js-base64';
import $ from './app'; import $ from './app';
@ -1190,9 +1190,9 @@ const PROXY_PROCESSORS = (function () {
function ScriptOperator(script, targetPlatform, $arguments) { function ScriptOperator(script, targetPlatform, $arguments) {
return { return {
name: 'Script Operator', name: 'Script Operator',
func: (proxies) => { func: async (proxies) => {
let output = proxies; let output = proxies;
(function () { await (async function () {
// interface to get internal operators // interface to get internal operators
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
@ -1295,22 +1295,24 @@ const PROXY_PROCESSORS = (function () {
/** /**
Script Example Script Example
function func(proxies) {
const selected = FULL(proxies.length, true); function filter(proxies) {
// do something return proxies.map(p => {
return selected; return p.name.indexOf("🇭🇰") !== -1;
});
} }
WARNING: WARNING:
1. This function name should be `func`! 1. This function name should be `filter`!
2. Always declare variables before using them! 2. Always declare variables before using them!
*/ */
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
function ScriptFilter(script, targetPlatform, $arguments) { function ScriptFilter(script, targetPlatform, $arguments) {
return { return {
name: 'Script Filter', name: 'Script Filter',
func: (proxies) => { func: async (proxies) => {
let output = FULL(proxies.length, true); let output = FULL(proxies.length, true);
!(function () { await (async function () {
eval(script); eval(script);
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
output = filter(proxies, targetPlatform); output = filter(proxies, targetPlatform);
@ -1860,7 +1862,7 @@ export const ProxyUtils = (function () {
} }
} }
// if this is remote script, download it // if this is a remote script, download it
try { try {
script = await download(url.split('#')[0]); script = await download(url.split('#')[0]);
$.info(`Script loaded: >>>\n ${script}`); $.info(`Script loaded: >>>\n ${script}`);
@ -1896,7 +1898,7 @@ export const ProxyUtils = (function () {
} else { } else {
processor = PROXY_PROCESSORS[item.type](item.args); processor = PROXY_PROCESSORS[item.type](item.args);
} }
proxies = ApplyProcessor(processor, proxies); proxies = await ApplyProcessor(processor, proxies);
} }
return proxies; return proxies;
} }
@ -1949,12 +1951,12 @@ export const ProxyUtils = (function () {
}; };
})(); })();
export function ApplyProcessor(processor, objs) { export async function ApplyProcessor(processor, objs) {
function ApplyFilter(filter, objs) { async function ApplyFilter(filter, objs) {
// select proxies // select proxies
let selected = FULL(objs.length, true); let selected = FULL(objs.length, true);
try { try {
selected = AND(selected, filter.func(objs)); selected = AND(selected, await filter.func(objs));
} catch (err) { } catch (err) {
// print log and skip this filter // print log and skip this filter
console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`); console.log(`Cannot apply filter ${filter.name}\n Reason: ${err}`);
@ -1962,10 +1964,10 @@ export function ApplyProcessor(processor, objs) {
return objs.filter((_, i) => selected[i]); return objs.filter((_, i) => selected[i]);
} }
function ApplyOperator(operator, objs) { async function ApplyOperator(operator, objs) {
let output = clone(objs); let output = clone(objs);
try { try {
const output_ = operator.func(output); const output_ = await operator.func(output);
if (output_) output = output_; if (output_) output = output_;
} catch (err) { } catch (err) {
// print log and skip this operator // print log and skip this operator

File diff suppressed because one or more lines are too long