♻️ refactor(substore): 提取重构公共工具 common.js 并新增 liangxin 脚本
This commit is contained in:
+27
-13
@@ -4,7 +4,28 @@
|
||||
* 维护说明:仅需修改 featureMap 字典即可,底部逻辑永远无需改动。
|
||||
*/
|
||||
// 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();
|
||||
|
||||
// ================= 唯一配置区 =================
|
||||
// 在这里新增或修改关键词。格式:"节点关键词": "想要注入的标签"
|
||||
// 支持大小写混合,支持包含空格或特殊符号。
|
||||
@@ -38,16 +59,12 @@ function operator(proxies) {
|
||||
const featureKeywords = Object.keys(featureMap);
|
||||
const normalizeFeatureKeyword = keyword => keyword.toUpperCase().replace(/\s+/g, '');
|
||||
const normalizeHiddenFeatureKeyword = keyword => keyword.trim().replace(/\s+\d+(?:\.\d+)?x$/i, '');
|
||||
const regexSpecialChars = new Set(['.', '*', '+', '?', '^', '$', '{', '}', '(', ')', '|', '[', ']', '\\']);
|
||||
const escapeRegexKeyword = keyword => [...keyword]
|
||||
.map(char => regexSpecialChars.has(char) ? `\\${char}` : char)
|
||||
.join('');
|
||||
for (const featureKeyword of featureKeywords) {
|
||||
normalizedFeatureMap[normalizeFeatureKeyword(featureKeyword)] = featureMap[featureKeyword];
|
||||
}
|
||||
|
||||
const buildKeywordPattern = keyword => {
|
||||
const keywordPattern = keyword.trim().split(/\s+/).filter(Boolean).map(escapeRegexKeyword).join('\\s*');
|
||||
const keywordPattern = keyword.trim().split(/\s+/).filter(Boolean).map(utils.escapeRegex).join('\\s*');
|
||||
const trimmedKeyword = keyword.trim();
|
||||
if (/^[A-Za-z0-9]/.test(trimmedKeyword) && /[A-Za-z0-9]$/.test(trimmedKeyword)) {
|
||||
return `\\b${keywordPattern}\\b`;
|
||||
@@ -68,18 +85,15 @@ function operator(proxies) {
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.trim();
|
||||
|
||||
const normalizeRegionSequence = proxyName => proxyName
|
||||
.replace(/(^|\s)([A-Z]{2})[-\s]?(\d{1,2})(?=$|[\s-])/g, (match, prefix, regionCode, sequenceNumber) => {
|
||||
return `${prefix}${regionCode} ${sequenceNumber.padStart(2, '0')}`;
|
||||
})
|
||||
.replace(/(\b[A-Z]{2} \d{2})-(?=\d+(?:\.\d+)?x\b)/g, '$1 ');
|
||||
|
||||
const abbreviateDedicatedLineProvider = proxyName => proxyName.replace(
|
||||
/\b([A-Za-z])[A-Za-z]*\s*专线/g,
|
||||
(match, providerInitial) => `${providerInitial.toUpperCase()}专线`
|
||||
);
|
||||
|
||||
const normalizeDisplayName = proxyName => abbreviateDedicatedLineProvider(normalizeRegionSequence(proxyName));
|
||||
const normalizeDisplayName = proxyName => abbreviateDedicatedLineProvider(
|
||||
utils.normalizeRegionSequence(proxyName.replace(/\b(\d+(?:\.\d+)?)x\b/gi, '$1X'))
|
||||
.replace(/\b(\d+(?:\.\d+)?)X\b/g, '$1x')
|
||||
);
|
||||
|
||||
// 2. 动态构建复合正则表达式 (核心引擎)
|
||||
// 按字符串长度降序排序,彻底解决 "短路匹配" (Short-Circuit) 问题
|
||||
|
||||
Reference in New Issue
Block a user