improve error alert.

This commit is contained in:
NobyDa 2021-06-17 21:17:25 +08:00
parent b84bb0b1da
commit f1f9740caf
No known key found for this signature in database
GPG Key ID: E6B0AC788D373C5B
1 changed files with 34 additions and 16 deletions

View File

@ -5,7 +5,7 @@
如需禁用豆瓣评分或策略通知, 可前往BoxJs设置.
BoxJs订阅地址: https://raw.githubusercontent.com/NobyDa/Script/master/NobyDa_BoxJs.json
Update: 2021.05.02
Update: 2021.06.17
Author: @NobyDa
Use: Surge, QuanX, Loon
@ -80,7 +80,7 @@ async function SwitchRegion(play) {
const CN = $.read('BiliArea_CN') || 'DIRECT'; //Your China sub-policy name.
const TW = $.read('BiliArea_TW') || '🇹🇼 sub-policy'; //Your Taiwan sub-policy name.
const HK = $.read('BiliArea_HK') || '🇭🇰 sub-policy'; //Your HongKong sub-policy name.
const current = await $.getPolicy(Group) || 'Policy error ⚠️';
const current = await $.getPolicy(Group);
const area = (() => {
if (/\u50c5[\u4e00-\u9fa5]+\u6e2f|%20%E6%B8%AF&/.test(play)) {
if (current != HK) return HK;
@ -92,14 +92,33 @@ async function SwitchRegion(play) {
if (area) {
const change = await $.setPolicy(Group, area);
const notify = $.read('BiliAreaNotify') === 'true';
const msg = `${current} => ${change?area:'sub-policy error ⚠️'} => ${change?`🟢`:`🔴`}`;
if (!notify) $.notify(/^http/.test(play) || !play ? `` : play, ``, msg);
else console.log(`${/^http/.test(play)||!play?``:play}\n${msg}`);
if (change) return true;
const msg = SwitchStatus(change, current, area);
if (!notify) {
$.notify(/^http/.test(play) || !play ? `` : play, ``, msg);
} else {
console.log(`${/^http/.test(play)||!play?``:play}\n${msg}`);
}
if (change) {
return true;
}
}
return false;
}
function SwitchStatus(status, original, newPolicy) {
if (status) {
return `${original} => ${newPolicy} => 🟢`;
} else if (original === 2) {
return `切换失败, 策略组名未填写或填写有误 ⚠️`
} else if (original === 3) {
return `切换失败, 不支持您的VPN应用版本 ⚠️`
} else if (status === 0) {
return `切换失败, 子策略名未填写或填写有误 ⚠️`
} else {
return `策略切换失败, 未知错误 ⚠️`
}
}
function EnvInfo() {
if (typeof($response) !== 'undefined') {
const raw = JSON.parse($response.body);
@ -234,29 +253,28 @@ function nobyda() {
return response;
}
const getPolicy = (groupName) => {
const m = `Version error ⚠️`
if (isSurge) {
if (typeof($httpAPI) === 'undefined') return m;
if (typeof($httpAPI) === 'undefined') return 3;
return new Promise((resolve) => {
$httpAPI("GET", "v1/policy_groups/select", {
group_name: encodeURIComponent(groupName)
}, (b) => resolve(b.policy))
}, (b) => resolve(b.policy || 2))
})
}
if (isLoon) {
if (typeof($config.getPolicy) === 'undefined') return m;
if (typeof($config.getPolicy) === 'undefined') return 3;
const getName = $config.getPolicy(groupName);
return getName;
return getName || 2;
}
if (isQuanX) {
if (typeof($configuration) === 'undefined') return m;
if (typeof($configuration) === 'undefined') return 3;
return new Promise((resolve) => {
$configuration.sendMessage({
action: "get_policy_state"
}).then(b => {
if (b.ret && b.ret[groupName]) {
resolve(b.ret[groupName][1]);
} else resolve();
} else resolve(2);
}, () => resolve());
})
}
@ -267,12 +285,12 @@ function nobyda() {
$httpAPI("POST", "v1/policy_groups/select", {
group_name: group,
policy: policy
}, (b) => resolve(!b.error))
}, (b) => resolve(!b.error || 0))
})
}
if (isLoon && typeof($config.getPolicy) !== 'undefined') {
const set = $config.setSelectPolicy(group, policy);
return set;
return set || 0;
}
if (isQuanX && typeof($configuration) !== 'undefined') {
return new Promise((resolve) => {
@ -281,7 +299,7 @@ function nobyda() {
content: {
[group]: policy
}
}).then((b) => resolve(!b.error), () => resolve());
}).then((b) => resolve(!b.error || 0), () => resolve());
})
}
}