在page_action上显示弹出窗口

use*_*366 1 google-chrome-extension

我想念没有显示弹出窗口吗?

在扩展文件夹中,我有四个文件。我将该文件夹加载为扩展名。我可以看到该图标,但是单击它时不会显示弹出窗口。为什么?

  1. content_script.js:空(只是添加以便我可以加载扩展名)
  2. icon.png:显示在我加载扩展程序时。
  3. manifest.json
{
    "name": "Popup poc",
    "version": "1.4",
    "description": "Simple popup example",
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content_script.js"]
        }
    ],
    "page_action": {
        "default_name": "Display Map",
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    },
    "manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
  1. popup.html
<!doctype html>
<html>
<head>
    <title>Popup</title>
</head>

<body>
    This is body
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ger*_*abo 5

在您的电脑page_action中用“ browser_action” 替换“ ” manifest.json。这样,您应该可以在所有页面上看到弹出窗口。

可能与以下项重复:Chrome扩展程序弹出窗口不再显示, 但似乎我没有足够的声誉点对此进行标记。


仅针对某些主机URL模式的弹出窗口

(编辑)

如果希望仅在某些网站(例如,仅在Google)上可以使用弹出窗口,则需要declarativeContent在清单中指定权限,并在后台脚本中进行一些设置。

因此,您manifest.json将看起来像这样:

    {
        "name": "Popup poc",
        "version": "1.4",
        "description": "Simple popup example",
        "permissions": ["declarativeContent"],
        "content_scripts": [
            {
                "matches": ["https://www.google.com/*"],
                "js": ["content_script.js"]
            }
        ],
        "background": {
          "scripts": ["background.js"],
          "persistent": false
        },
        "page_action": {
            "default_name": "Display Map",
            "default_icon": "icon.png",
            "default_popup": "popup.html"
        },
        "manifest_version": 2
    }
Run Code Online (Sandbox Code Playgroud)

background.js将如下所示:

    chrome.runtime.onInstalled.addListener(function() {
        chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
          chrome.declarativeContent.onPageChanged.addRules([{
            conditions: [new chrome.declarativeContent.PageStateMatcher({
              pageUrl: {hostEquals: 'www.google.com'},
            })
            ],
                actions: [new chrome.declarativeContent.ShowPageAction()]
          }]);
        });
      });
Run Code Online (Sandbox Code Playgroud)

该代码主要来自“ 入门指南”