如何将扩展清单中的匹配模式转换为正则表达式?

发布于 2025-01-14 15:38:02 字数 752 浏览 2 评论 0原文

在我的应用程序清单中,我已在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2025-01-21 15:38:02

我发布了一个名为 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 like glob-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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文