【问题标题】:Using Match Patterns In Backround.js Chrome Extension在 Background.js Chrome 扩展中使用匹配模式
【发布时间】:2021-12-24 21:30:24
【问题描述】:

我正在开发一个 chrome 扩展项目,但无法使匹配模式正常工作,因此它涵盖了用户经过域的所有地方。举个例子,如果用户去https://www.google.com/imghp?hl=en 让代码在那里再次执行。这可以在 background.js 文件中执行吗?

  chrome.webNavigation.onCompleted.addListener(function() {
      alert("This is my favorite website!");
  }, {url: [{urlMatches : '*://www.google.com/*'}]});

我已尝试使用变量来表示“://www.google.com/”,例如:

var site = "*://www.google.com/*"
will not let me pass this into either url: 'site'
or {url: [{urlMatches : 'site'}

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension google-chrome-devtools


    【解决方案1】:

    正如您在documentation 中看到的,urlMatches 不使用匹配模式,它使用正则表达式RE2 syntax

    对于*://www.google.com/*,您可以简单地切换到hostEquals

    chrome.webNavigation.onCompleted.addListener(info => {
      console.log(info);
    }, {
      url: [{hostEquals: 'www.google.com'}],
    });
    

    请注意,在 JavaScript 中,'site' 是与变量 site 无关的文字字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-18
      • 2013-02-18
      • 2023-01-15
      • 1970-01-01
      • 2019-09-08
      • 1970-01-01
      • 2023-03-11
      相关资源
      最近更新 更多