/** * Sub-Store 整合订阅后缀脚本 * - 保留节点原名,只追加当前订阅的 `_subName` * - 按节点名里最先出现的国家/地区标识重置开头国旗 * - 重复执行不会叠加相同的订阅后缀 * - 同名节点追加两位编号,避免节点名重复 */ // noinspection JSUnusedGlobalSymbols const COMMON_SCRIPT_VERSION = '20260702-1'; const COMMON_SCRIPT_URL = `https://git.orionc.me/orion/script/raw/branch/main/substore/common.js?v=${COMMON_SCRIPT_VERSION}`; const hasCurrentRenameUtils = utils => Boolean( utils && utils.version === COMMON_SCRIPT_VERSION ); async function loadRenameUtils() { if (typeof globalThis !== 'undefined' && hasCurrentRenameUtils(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))(); if (!hasCurrentRenameUtils(globalThis.SubStoreRenameUtils)) { throw new Error('Loaded common.js is missing current rename utilities'); } 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) => { const cleanSubName = String(subName || '').trim(); if (!cleanSubName) return String(name); const escapeRegex = utils.escapeRegex || (value => String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); return String(name) .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); const coreNames = new Array(proxies.length); const subNames = new Array(proxies.length); proxies.forEach((proxy, proxyIndex) => { // 非对象或非字符串名称不参与处理,避免改动无效节点数据。 if (!proxy || typeof proxy !== 'object' || typeof proxy.name !== 'string') { baseNames[proxyIndex] = null; return; } const proxyName = proxy.name; const subName = proxy && typeof proxy._subName === 'string' ? proxy._subName.trim() : ''; // 只在原后缀逻辑前补齐/修正首国旗,后缀与去重逻辑保持不变。 const coreName = withFirstCountryFlag(subName ? stripSubscriptionSuffix(proxyName, subName) : proxyName); const baseName = subName ? (coreName ? `${coreName} - ${subName}` : subName) : coreName; baseNames[proxyIndex] = baseName; coreNames[proxyIndex] = coreName; subNames[proxyIndex] = subName; nameCounts[baseName] = (nameCounts[baseName] || 0) + 1; }); return proxies.map((proxy, proxyIndex) => { const baseName = baseNames[proxyIndex]; if (baseName === null || !proxy || typeof proxy !== 'object') return proxy; if (nameCounts[baseName] > 1) { currentCounts[baseName] = (currentCounts[baseName] || 0) + 1; const duplicateNumber = String(currentCounts[baseName]).padStart(2, '0'); proxy.name = subNames[proxyIndex] ? `${coreNames[proxyIndex]} ${duplicateNumber} - ${subNames[proxyIndex]}` : `${baseName} ${duplicateNumber}`; } else { proxy.name = baseName; } return proxy; }); }