首页 > 解决方案 > Chrome 扩展在页面操作中显示弹出窗口

问题描述

我对 chrome 扩展浏览器和页面操作有点困惑。我的目标是有一个特定页面的弹出窗口。

manifest.json

    {
      "name": "Basic extention",
      "version": "1.0",
      "description": "Extention",
      "permissions": [
        "activeTab", 
        "declarativeContent", 
        "storage",
        "tabs",
        "*://www.google.com/*"  ],  
      "background": {
        "scripts": ["background.js"],
        "persistent": false
      },  
      "page_action": {
        "default_popup": "popup.html",
        "default_icon": {
          "16": "images/icon16.png",
          "32": "images/icon32.png",
          "48": "images/icon64.png",
          "128": "images/icon128.png"
        }
      },
      "icons": {
        "16": "images/icon16.png",
        "32": "images/icon32.png",
        "48": "images/icon64.png",
        "128": "images/icon128.png"
      },
      "manifest_version": 2
    }

background.js

'use strict';

chrome.runtime.onInstalled.addListener(function() {

  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [new chrome.declarativeContent.PageStateMatcher({
        pageUrl: {hostEquals: 'https://www.google.com/*'},
      })],
      actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
  });
}); 

popup.html

<!DOCTYPE html>
<html>
  <head>
    <style>
      body{
        background-color: green;
      }
    </style>      
    <title>Test extention</title>
  </head>
  <body>  
    Test
  </body>
</html>

这是问题所在:


我是否需要以某种方式手动触发弹出窗口?我在清单中缺少一些权限吗?我将不胜感激任何帮助或提示。

标签: google-chrome-extension

解决方案


您定义的条件hostEquals不正确。hostEqual它应该分为scheme以下几种:

pageUrl: { hostEquals: 'www.google.com', schemes: ['https'] }

完成此操作后,它应该按定义工作。查看https://developer.chrome.com/extensions/declarativeContent中的规则部分


推荐阅读