♻️ refactor(substore): 提取重构公共工具 common.js 并新增 liangxin 脚本
This commit is contained in:
+28
-63
@@ -6,7 +6,27 @@
|
||||
* - 不追加订阅后缀,整合订阅最后仍交给 rename.js 处理
|
||||
*/
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
function operator(proxies) {
|
||||
const COMMON_SCRIPT_URL = 'https://git.orionc.me/orion/script/raw/branch/main/substore/common.js';
|
||||
|
||||
async function loadRenameUtils() {
|
||||
if (typeof globalThis !== 'undefined' && globalThis.SubStoreRenameUtils) {
|
||||
return globalThis.SubStoreRenameUtils;
|
||||
}
|
||||
|
||||
const cache = typeof scriptResourceCache !== 'undefined' ? scriptResourceCache : null;
|
||||
let script = cache && cache.get(COMMON_SCRIPT_URL);
|
||||
if (!script) {
|
||||
const response = await $substore.http.get({ url: COMMON_SCRIPT_URL });
|
||||
script = response && (response.body || response.data || response);
|
||||
if (cache) cache.set(COMMON_SCRIPT_URL, script);
|
||||
}
|
||||
|
||||
new Function(String(script))();
|
||||
return globalThis.SubStoreRenameUtils;
|
||||
}
|
||||
|
||||
async function operator(proxies) {
|
||||
const utils = await loadRenameUtils();
|
||||
const regionAliases = [
|
||||
['美国夏威夷', 'US HNL'],
|
||||
['家宽夏威夷', 'US HNL'],
|
||||
@@ -34,62 +54,12 @@ function operator(proxies) {
|
||||
['United Kingdom', 'UK'],
|
||||
['英国', 'UK']
|
||||
];
|
||||
const sortedRegionAliases = regionAliases
|
||||
.map(([alias, replacement]) => [alias, replacement, alias.toLowerCase()])
|
||||
.sort((firstAlias, secondAlias) => secondAlias[0].length - firstAlias[0].length);
|
||||
const leadingFlagRegex = /^([\u{1F1E6}-\u{1F1FF}]{2})\s*/u;
|
||||
const inlineFlagRegex = /^(.{1,24}?)([\u{1F1E6}-\u{1F1FF}]{2})\s*(.*)$/u;
|
||||
|
||||
const flagToRegionCode = flag => {
|
||||
if (!flag) return '';
|
||||
const codePoints = [...flag].map(char => char.codePointAt(0));
|
||||
if (
|
||||
codePoints.length !== 2 ||
|
||||
codePoints.some(codePoint => codePoint < 0x1F1E6 || codePoint > 0x1F1FF)
|
||||
) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const regionCode = codePoints
|
||||
.map(codePoint => String.fromCharCode(codePoint - 0x1F1E6 + 65))
|
||||
.join('');
|
||||
return regionCode === 'GB' ? 'UK' : regionCode;
|
||||
};
|
||||
|
||||
const regionCodeToFlag = regionCode => {
|
||||
const flagCode = regionCode === 'UK' ? 'GB' : regionCode;
|
||||
if (!/^[A-Z]{2}$/.test(flagCode)) return '';
|
||||
|
||||
return flagCode
|
||||
.split('')
|
||||
.map(char => String.fromCodePoint(char.charCodeAt(0) - 65 + 0x1F1E6))
|
||||
.join('');
|
||||
};
|
||||
const regionResolver = utils.createRegionResolver(regionAliases);
|
||||
|
||||
const stripExistingSuffix = name => name
|
||||
.replace(/\s+-\s*Alpha Air(?:\s+\d{2})?\s*$/i, '')
|
||||
.trim();
|
||||
|
||||
const stripFlag = name => {
|
||||
const leadingFlagMatch = name.match(leadingFlagRegex);
|
||||
if (leadingFlagMatch) {
|
||||
return {
|
||||
flag: leadingFlagMatch[1],
|
||||
text: name.slice(leadingFlagMatch[0].length).trim()
|
||||
};
|
||||
}
|
||||
|
||||
const inlineFlagMatch = name.match(inlineFlagRegex);
|
||||
if (!inlineFlagMatch) return { flag: '', text: name.trim() };
|
||||
|
||||
const prefix = inlineFlagMatch[1].trim();
|
||||
const rest = inlineFlagMatch[3].trim();
|
||||
return {
|
||||
flag: inlineFlagMatch[2],
|
||||
text: rest ? `${prefix} ${rest}`.trim() : prefix
|
||||
};
|
||||
};
|
||||
|
||||
const multiplierOf = name => {
|
||||
const match = name.match(/\b(\d+(?:\.\d+)?)(?:\s*[xX]|倍)\b/i);
|
||||
return match ? `${match[1]}X` : '';
|
||||
@@ -108,25 +78,20 @@ function operator(proxies) {
|
||||
return match[2] ? `${match[1]} ${match[2]}` : match[1];
|
||||
};
|
||||
|
||||
const aliasRegionPrefixOf = name => {
|
||||
const cleanNameLower = name.toLowerCase();
|
||||
for (const [, replacement, aliasLower] of sortedRegionAliases) {
|
||||
if (cleanNameLower.includes(aliasLower)) return replacement;
|
||||
}
|
||||
|
||||
const alphaAirRegionPrefixOf = name => {
|
||||
if (/\bhk\b/i.test(name)) return 'HK';
|
||||
if (/\bsg\b/i.test(name)) return 'SG';
|
||||
if (/\bjp\b/i.test(name)) return 'JP';
|
||||
if (/\btw\b/i.test(name)) return 'TW';
|
||||
if (/\bus\b/i.test(name)) return 'US';
|
||||
|
||||
return '';
|
||||
return regionResolver.aliasRegionPrefixOf(name);
|
||||
};
|
||||
|
||||
const regionPrefixOf = (name, flagRegionCode) => {
|
||||
if (/Hinet|Seednet/i.test(name)) return 'TW';
|
||||
|
||||
return aliasRegionPrefixOf(name) ||
|
||||
return alphaAirRegionPrefixOf(name) ||
|
||||
leadingRegionPrefixOf(name) ||
|
||||
flagRegionCode ||
|
||||
'';
|
||||
@@ -135,14 +100,14 @@ function operator(proxies) {
|
||||
const normalizeProxy = (proxy, originalIndex) => {
|
||||
const rawName = typeof proxy.name === 'string' ? proxy.name : '';
|
||||
const cleanName = stripExistingSuffix(rawName);
|
||||
const { flag, text } = stripFlag(cleanName);
|
||||
const flagRegionCode = flagToRegionCode(flag);
|
||||
const { flag, text } = utils.splitLeadingOrInlineFlag(cleanName);
|
||||
const flagRegionCode = utils.flagToRegionCode(flag);
|
||||
const regionPrefix = regionPrefixOf(text, flagRegionCode);
|
||||
const regionCode = regionPrefix.split(/\s+/)[0];
|
||||
const cityCode = regionPrefix.split(/\s+/)[1] || '';
|
||||
const multiplier = multiplierOf(text);
|
||||
const provider = multiplier ? '' : providerOf(text);
|
||||
const normalizedFlag = regionCodeToFlag(regionCode);
|
||||
const normalizedFlag = utils.regionCodeToFlag(regionCode);
|
||||
|
||||
return {
|
||||
proxy,
|
||||
|
||||
Reference in New Issue
Block a user