如何将扩展清单中的匹配模式转换为正则表达式?
在我的应用程序清单中,我已在 content_scripts
中声明了应允许运行扩展的有效 URL 的 matches
"content_scripts": [
{
"matches": ["*://*.creoweb.com/data/", "*://*.creoweb.com/cp/*/history/","*://*.creoweb.com/user/*/panel/"],
"js": ["content_scripts/core.js"]
}
]
在我的代码中,我有这样的内容:
const filter = {
url:
[
{urlMatches: "*://*.creoweb.com/data/"},
{urlMatches: "*://*.creoweb.com/cp/*/history/"},
{urlMatches: "*://*.creoweb.com/user/*/panel/"}
]
}
function logOnCompleted(details) {
console.log(`onCompleted: ${details.url}`);
}
browser.webNavigation.onCompleted.addListener(logOnCompleted, filter);
我认为侦听器希望 RegEx 正常工作,事实上我没有使用它获得任何控制台日志输出。
In my application manifest I have declared in the content_scripts
the matches
for valid URLs where the extension should be allowed to run
"content_scripts": [
{
"matches": ["*://*.creoweb.com/data/", "*://*.creoweb.com/cp/*/history/","*://*.creoweb.com/user/*/panel/"],
"js": ["content_scripts/core.js"]
}
]
In my code I have something like this:
const filter = {
url:
[
{urlMatches: "*://*.creoweb.com/data/"},
{urlMatches: "*://*.creoweb.com/cp/*/history/"},
{urlMatches: "*://*.creoweb.com/user/*/panel/"}
]
}
function logOnCompleted(details) {
console.log(`onCompleted: ${details.url}`);
}
browser.webNavigation.onCompleted.addListener(logOnCompleted, filter);
I think that the listener wants RegEx to work, in fact I'm not getting any console log output using this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发布了一个名为
webext-patterns
的模块,它可以从模式和 glob 生成特定于 WebExtension 的正则表达式。其他替代方案(例如glob-to-regex
)可能与浏览器行为不匹配(尤其是斜杠和:/
周围)但请注意,扩展程序用于匹配 URL 的正则表达式是有限的,它们不是完整的正则表达式,所以这可能就是它不起作用的原因。
详细信息:
I published a module named
webext-patterns
that can generate a WebExtension-specific regex from patterns and globs. Other alternatives likeglob-to-regex
may not match the browser behavior (especially around slashes and:/
)Note however that regular expressions used by extensions for matching URLs are limited, they aren't full regexes, so that might be the reason why it doesn't work.
More info: