我可以使用匹配模式获取浏览器标签
browser.tabs.query({ url: 'https://mozilla.org/*/b/*/' })
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns
在选项卡的更新事件期间,我可以获得选项卡 ID 和 url,但如何测试选项卡是否与我的原始匹配模式匹配?
browser.tabs.onUpdated.addListener(event => {
browser.tabs.get(event).then((tab) => {
if (tab.status === 'complete') {
if (?? tab.url matches 'https://mozilla.org/*/b/*/') {
console.log("Do something")
}
}
})
});
最佳答案
一个简单的自写星号匹配器是:
function match(pattern, url) {
pattern = pattern.split("/");
url = url.split("/");
while(url.length) {
const p = pattern.shift();
if(p !== url.shift() && p !== "*")
return false;
}
return true;
}
console.log(
match("https://example.com/*/test/", "https://example.com/a/test/"),
match("https://example.com/*/test/", "https://example.com/a/b/")
);
关于javascript - 如何针对 URL 字符串测试 URL 匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52226491/