♻️ refactor(substore): 提取重构公共工具 common.js 并新增 liangxin 脚本

This commit is contained in:
Orion
2026-06-03 12:56:18 +08:00
parent d60d1f2709
commit ce9fac0504
8 changed files with 460 additions and 192 deletions
+27 -55
View File
@@ -4,7 +4,27 @@
* 按地区稳定归并后重新连续编号,避免专属/普通节点撞号。
*/
// 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 = [
['Hong Kong', 'HK'],
['香港', 'HK'],
@@ -47,57 +67,10 @@ function operator(proxies) {
['荷蘭', 'NL'],
['NL', 'NL']
];
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 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 regionCodeOf = name => {
const leadingFlagMatch = name.match(leadingFlagRegex);
if (leadingFlagMatch) return flagToRegionCode(leadingFlagMatch[1]);
const nameLower = name.toLowerCase();
for (const [alias, replacement, aliasLower] of sortedRegionAliases) {
if (/^[A-Z]{2}$/.test(alias)) {
const codeMatch = name.match(new RegExp(`(^|[^A-Za-z])${alias}([^A-Za-z]|$)`, 'i'));
if (codeMatch) return replacement;
continue;
}
if (nameLower.includes(aliasLower)) return replacement;
}
return '';
};
const splitLeadingFlag = name => {
const leadingFlagMatch = name.match(leadingFlagRegex);
if (!leadingFlagMatch) return { flag: '', text: name.trim() };
return {
flag: leadingFlagMatch[1],
text: name.slice(leadingFlagMatch[0].length).trim()
};
};
const { regionCodeOf } = utils.createRegionResolver(regionAliases);
const withFreshRegionSequence = (name, sequenceNumber) => {
const { flag, text } = splitLeadingFlag(name);
const { flag, text } = utils.splitLeadingFlag(name);
const sequenceText = String(sequenceNumber);
let core = text.trim();
let feature = '';
@@ -115,10 +88,9 @@ function operator(proxies) {
core = core
.replace(/\s*\d+(?!\d)(?!\s*(?:[xX倍]|\.\d))(?=\s|$)/, '')
.replace(/\s{2,}/g, ' ')
.trim();
.replace(/\s{2,}/g, ' ');
return `${flag}${core}${sequenceText}${feature ? ` ${feature}` : ''}`.trim();
return utils.normalizeWhitespace(`${flag}${core}${sequenceText}${feature ? ` ${feature}` : ''}`);
};
const normalizedItems = proxies.map((proxy, originalIndex) => {
@@ -136,8 +108,8 @@ function operator(proxies) {
.replace(/\s*(?:专属|家宽|高速|专线)(?:\s*(\d+)(?!\s*(?:[xX倍]|\.\d)))?/g, '$1')
.replace(/💎/gu, '')
.replace(/([\u4e00-\u9fff])AI\b/g, '$1 AI')
.replace(/\s{2,}/g, ' ')
.trim();
.replace(/\s{2,}/g, ' ');
proxy.name = utils.normalizeWhitespace(proxy.name);
return {
proxy,