wildcard is supported

This commit is contained in:
Gabe Yuan
2023-08-18 13:16:17 +08:00
parent 01676bc682
commit b18721a4e5
3 changed files with 41 additions and 4 deletions

View File

@@ -153,8 +153,8 @@ export const I18N = {
en: `URL pattern`, en: `URL pattern`,
}, },
pattern_helper: { pattern_helper: {
zh: `多个URL支持英文逗号“,”分隔`, zh: `1、支持星号(*)通配符。2、多个URL支持英文逗号“,”分隔`,
en: `Multiple URLs can be separated by English commas ","`, en: `1. The asterisk (*) wildcard is supported. 2. Multiple URLs can be separated by English commas ",".`,
}, },
selector_helper: { selector_helper: {
zh: `1、遵循CSS选择器规则。2、留空表示采用全局设置。`, zh: `1、遵循CSS选择器规则。2、留空表示采用全局设置。`,

View File

@@ -9,6 +9,7 @@ import {
BUILTIN_RULES, BUILTIN_RULES,
} from "../config"; } from "../config";
import { browser } from "./browser"; import { browser } from "./browser";
import { isMatch } from "./utils";
/** /**
* 获取节点列表并转为数组 * 获取节点列表并转为数组
@@ -48,7 +49,6 @@ export const setFab = async (obj) => await storage.setObj(STOKEY_FAB, obj);
/** /**
* 根据href匹配规则 * 根据href匹配规则
* TODO: 支持通配符(*)匹配
* @param {*} rules * @param {*} rules
* @param {string} href * @param {string} href
* @returns * @returns
@@ -59,7 +59,7 @@ export const matchRule = (rules, href, { injectRules }) => {
} }
const rule = rules.find((rule) => const rule = rules.find((rule) =>
rule.pattern.split(",").some((p) => href.includes(p.trim())) rule.pattern.split(",").some((p) => isMatch(href, p.trim()))
); );
const globalRule = const globalRule =
rules.find((rule) => rules.find((rule) =>

View File

@@ -51,3 +51,40 @@ export const debounce = (func, delay = 200) => {
}, delay); }, delay);
}; };
}; };
/**
* 字符串通配符(*)匹配
* @param {*} s
* @param {*} p
* @returns
*/
export const isMatch = (s, p) => {
if (s.length === 0 || p.length === 0) {
return false;
}
p = `*${p}*`;
let [sIndex, pIndex] = [0, 0];
let [sRecord, pRecord] = [-1, -1];
while (sIndex < s.length && pRecord < p.length) {
if (p[pIndex] === "*") {
pIndex++;
[sRecord, pRecord] = [sIndex, pIndex];
} else if (s[sIndex] === p[pIndex]) {
sIndex++;
pIndex++;
} else if (sRecord + 1 < s.length) {
sRecord++;
[sIndex, pIndex] = [sRecord, pRecord];
} else {
return false;
}
}
if (p.length === pIndex) {
return true;
}
return p.slice(pIndex).replaceAll("*", "") === "";
};