Chrome 扩展程序 - 针对特定网页的网页操作



我想使用 pageAction API 向全能框添加一个自定义图标

https://developer.chrome.com/extensions/pageAction

如何仅针对特定网址/匹配模式执行自定义网页操作?是否可以针对特定 URL 注册事件?

例如,如果用户访问 foobar.com 执行自定义页面操作。对于其他页面,我不想做任何事情。

我认为PageStateMatcher就是你想要的。文档在这里。

  new chrome.declarativeContent.PageStateMatcher({
    pageUrl: { hostEquals: 'www.google.com', schemes: ['https'] },
    css: ["input[type='password']"]
  })

这是按网址执行的版式示例网页操作示例。检查PageStateMatcher部分。

// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
  // Replace all rules ...
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // With a new rule ...
    chrome.declarativeContent.onPageChanged.addRules([
      {
        // That fires when a page's URL contains a 'g' ...
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlContains: 'g' },
          })
        ],
        // And shows the extension's page action.
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

最新更新