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

File diff suppressed because one or more lines are too long