首页 > 解决方案 > Chrome 扩展显示页面操作示例清单 v3

问题描述

我正在尝试使用清单 v3重现此示例。但我的操作始终处于活动状态 - 我将其在所有页面上禁用,而 URL 中没有“g”。

提前致谢!

清单.json

{
   "name":"Example",
   "description":"description",
   "version":"0.1",
   "manifest_version":3,
   "background":{
      "service_worker":"background.js"
   },
   "permissions":[
      "declarativeContent"
   ],
   "action":{
      "default_icon":{
         "16":"/images/get_started16.png",
         "32":"/images/get_started32.png",
         "48":"/images/get_started48.png",
         "128":"/images/get_started128.png"
      },
      "default_title":"press here to open"
   },
   "icons":{
      "16":"/images/get_started16.png",
      "32":"/images/get_started32.png",
      "48":"/images/get_started48.png",
      "128":"/images/get_started128.png"
   }
}

背景.js

chrome.runtime.onInstalled.addListener(() => {
  // 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() ]
      }
    ], function callback(details) {
      console.log("chrome.declarativeContent.onPageChanged.addRules callback");
      console.log(details);
    });
  });
});

标签: google-chrome-extension

解决方案


推荐阅读