From 72f09f039a92df9fc5df1d2d3602527a70202b24 Mon Sep 17 00:00:00 2001 From: Orion Date: Tue, 4 Aug 2026 16:20:49 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(core):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E9=87=8D=E5=91=BD=E5=90=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=B9=B6=E5=AE=8C=E5=96=84=E5=9B=BD=E6=97=97=E9=87=8D=E7=BD=AE?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 按节点名中首次出现的国家/地区标识重置开头国旗图标 * 优化重命名工具函数的加载逻辑与版本校验机制 * 完善同名节点追加两位编号的防重复逻辑 * 修复重复执行脚本时可能导致订阅后缀叠加的问题 * 调整脚本注释说明与内部变量定义的组织结构 --- substore/rename.js | 144 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 142 insertions(+), 2 deletions(-) diff --git a/substore/rename.js b/substore/rename.js index f23d395..4fbed0d 100644 --- a/substore/rename.js +++ b/substore/rename.js @@ -1,6 +1,7 @@ /** * Sub-Store 整合订阅后缀脚本 * - 保留节点原名,只追加当前订阅的 `_subName` + * - 按节点名里最先出现的国家/地区标识重置开头国旗 * - 重复执行不会叠加相同的订阅后缀 * - 同名节点追加两位编号,避免节点名重复 */ @@ -31,6 +32,131 @@ async function loadRenameUtils() { return globalThis.SubStoreRenameUtils; } +const ISO_REGION_CODES = ` + AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ + CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO + FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE + JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO + MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW + PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM + TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW +`.trim().split(/\s+/); + +const EXTRA_REGION_ALIASES = [ + ['EU', 'EU'], ['Europe', 'EU'], ['European Union', 'EU'], ['欧洲', 'EU'], ['歐洲', 'EU'], + ['XK', 'XK'], ['Kosovo', 'XK'], ['科索沃', 'XK'], + ['UK', 'UK'], ['United Kingdom', 'UK'], ['Britain', 'UK'], ['Great Britain', 'UK'], ['England', 'UK'], ['英国', 'UK'], ['英國', 'UK'], + ['USA', 'US'], ['U.S.A.', 'US'], ['United States', 'US'], ['United States of America', 'US'], ['America', 'US'], ['美国', 'US'], ['美國', 'US'], ['美西', 'US'], ['美东', 'US'], ['美東', 'US'], + ['UAE', 'AE'], ['United Arab Emirates', 'AE'], ['阿联酋', 'AE'], ['阿聯酋', 'AE'], + ['Russia', 'RU'], ['Russian Federation', 'RU'], ['俄罗斯', 'RU'], ['俄羅斯', 'RU'], + ['South Korea', 'KR'], ['Korea', 'KR'], ['Republic of Korea', 'KR'], ['韩国', 'KR'], ['韓國', 'KR'], ['南韩', 'KR'], ['南韓', 'KR'], + ['North Korea', 'KP'], ['DPRK', 'KP'], ['朝鲜', 'KP'], ['北韩', 'KP'], ['北韓', 'KP'], + ['Hong Kong', 'HK'], ['香港', 'HK'], + ['Macau', 'MO'], ['Macao', 'MO'], ['澳门', 'MO'], ['澳門', 'MO'], + ['Taiwan', 'TW'], ['台湾', 'TW'], ['台灣', 'TW'], + ['Singapore', 'SG'], ['新加坡', 'SG'], ['狮城', 'SG'], ['獅城', 'SG'], + ['Japan', 'JP'], ['日本', 'JP'], + ['China', 'CN'], ['中国', 'CN'], ['中國', 'CN'], + ['Australia', 'AU'], ['澳大利亚', 'AU'], ['澳大利亞', 'AU'], ['澳洲', 'AU'], + ['Canada', 'CA'], ['加拿大', 'CA'], + ['Germany', 'DE'], ['德国', 'DE'], ['德國', 'DE'], + ['France', 'FR'], ['法国', 'FR'], ['法國', 'FR'], + ['Netherlands', 'NL'], ['Holland', 'NL'], ['荷兰', 'NL'], ['荷蘭', 'NL'], + ['Switzerland', 'CH'], ['瑞士', 'CH'], + ['Sweden', 'SE'], ['瑞典', 'SE'], + ['Norway', 'NO'], ['挪威', 'NO'], + ['Finland', 'FI'], ['芬兰', 'FI'], ['芬蘭', 'FI'], + ['Denmark', 'DK'], ['丹麦', 'DK'], ['丹麥', 'DK'], + ['Ireland', 'IE'], ['爱尔兰', 'IE'], ['愛爾蘭', 'IE'], + ['Italy', 'IT'], ['意大利', 'IT'], ['義大利', 'IT'], + ['Spain', 'ES'], ['西班牙', 'ES'], + ['Portugal', 'PT'], ['葡萄牙', 'PT'], + ['Poland', 'PL'], ['波兰', 'PL'], ['波蘭', 'PL'], + ['Ukraine', 'UA'], ['乌克兰', 'UA'], ['烏克蘭', 'UA'], + ['Turkey', 'TR'], ['Türkiye', 'TR'], ['土耳其', 'TR'], + ['India', 'IN'], ['印度', 'IN'], + ['Indonesia', 'ID'], ['印度尼西亚', 'ID'], ['印尼', 'ID'], + ['Malaysia', 'MY'], ['马来西亚', 'MY'], ['馬來西亞', 'MY'], + ['Thailand', 'TH'], ['泰国', 'TH'], ['泰國', 'TH'], + ['Vietnam', 'VN'], ['Viet Nam', 'VN'], ['越南', 'VN'], + ['Philippines', 'PH'], ['菲律宾', 'PH'], ['菲律賓', 'PH'], + ['Brazil', 'BR'], ['巴西', 'BR'], + ['Mexico', 'MX'], ['墨西哥', 'MX'], + ['Argentina', 'AR'], ['阿根廷', 'AR'], + ['Chile', 'CL'], ['智利', 'CL'], + ['South Africa', 'ZA'], ['南非', 'ZA'], + ['Israel', 'IL'], ['以色列', 'IL'], + ['Saudi Arabia', 'SA'], ['沙特', 'SA'], ['沙特阿拉伯', 'SA'] +]; + +const createFirstCountryResolver = utils => { + const aliases = []; + const seenAliases = Object.create(null); + const addAlias = (alias, regionCode) => { + const cleanAlias = String(alias || '').trim(); + const cleanRegionCode = String(regionCode || '').trim().toUpperCase(); + if (!cleanAlias || !cleanRegionCode) return; + + const seenKey = `${cleanAlias.toLowerCase()}\u0000${cleanRegionCode}`; + if (seenAliases[seenKey]) return; + seenAliases[seenKey] = true; + aliases.push([cleanAlias, cleanRegionCode]); + }; + + ISO_REGION_CODES.forEach(regionCode => addAlias(regionCode, regionCode)); + EXTRA_REGION_ALIASES.forEach(([alias, regionCode]) => addAlias(alias, regionCode)); + + if (typeof Intl !== 'undefined' && typeof Intl.DisplayNames === 'function') { + ['en', 'zh-CN', 'zh-Hans', 'zh-TW', 'zh-Hant'].forEach(locale => { + try { + const displayNames = new Intl.DisplayNames([locale], { type: 'region' }); + ISO_REGION_CODES.forEach(regionCode => addAlias(displayNames.of(regionCode), regionCode)); + } catch (error) { + // 部分 Sub-Store 运行环境没有完整 Intl 数据,静态别名仍可覆盖国家码。 + } + }); + } + + const compiledAliases = aliases.map(([alias, regionCode]) => { + const escapedAlias = utils.escapeRegex(alias); + const isLatinAlias = /^[A-Za-z][A-Za-z\s.'()-]*$/.test(alias); + return { + alias, + regionCode, + regex: isLatinAlias + ? new RegExp(`(^|[^A-Za-z])(${escapedAlias})(?=$|[^A-Za-z])`, 'i') + : new RegExp(`(${escapedAlias})`, 'i') + }; + }); + + return name => { + let firstMatch = null; + const cleanName = String(name || ''); + + compiledAliases.forEach(item => { + const match = cleanName.match(item.regex); + if (!match) return; + + const aliasText = match[2] || match[1] || match[0]; + const prefixLength = match[2] ? match[0].length - aliasText.length : 0; + const index = match.index + prefixLength; + if ( + !firstMatch || + index < firstMatch.index || + (index === firstMatch.index && item.alias.length > firstMatch.aliasLength) + ) { + firstMatch = { + index, + aliasLength: item.alias.length, + regionCode: item.regionCode + }; + } + }); + + return firstMatch ? firstMatch.regionCode : ''; + }; +}; + async function operator(proxies) { const utils = await loadRenameUtils(); const stripSubscriptionSuffix = utils.stripSubscriptionSuffix || ((name, subName) => { @@ -42,6 +168,20 @@ async function operator(proxies) { .replace(new RegExp(`\\s+-\\s*${escapeRegex(cleanSubName)}(?:\\s+\\d{2})?\\s*$`, 'i'), '') .trim(); }); + const firstCountryCodeOf = createFirstCountryResolver(utils); + const withFirstCountryFlag = name => { + const cleanName = utils.normalizeLeadingMojibakeFlag + ? utils.normalizeLeadingMojibakeFlag(name) + : String(name); + const splitName = utils.splitLeadingFlag + ? utils.splitLeadingFlag(cleanName) + : { flag: '', text: cleanName.replace(/^([\u{1F1E6}-\u{1F1FF}]{2})\s*/u, '').trim() }; + const regionCode = firstCountryCodeOf(splitName.text || cleanName); + if (!regionCode) return name; + + const flag = utils.regionCodeToFlag(regionCode); + return flag ? `${flag} ${splitName.text}`.trim() : name; + }; const nameCounts = Object.create(null); const currentCounts = Object.create(null); const baseNames = new Array(proxies.length); @@ -59,8 +199,8 @@ async function operator(proxies) { const subName = proxy && typeof proxy._subName === 'string' ? proxy._subName.trim() : ''; - // 没有订阅名时完全保留原字符串;公共工具的兼容实现会顺手 trim。 - const coreName = subName ? stripSubscriptionSuffix(proxyName, subName) : proxyName; + // 只在原后缀逻辑前补齐/修正首国旗,后缀与去重逻辑保持不变。 + const coreName = withFirstCountryFlag(subName ? stripSubscriptionSuffix(proxyName, subName) : proxyName); const baseName = subName ? (coreName ? `${coreName} - ${subName}` : subName) : coreName;