如何使用键盘快捷键打开Chrome扩展程序弹窗?

14

我知道我们不能直接从后台JavaScript打开Chrome扩展程序弹出窗口。但是,是否有一种方法可以在用户按下某些键组合时打开弹出窗口?


Signs point to yes - Jaromanda X
2个回答

15

提供的链接中的示例似乎已经不存在了。您能否编辑答案,展示如何修改manifest.json和任何相关文件,以使扩展在用户按下热键时打开? - Abhijeet Singh
@AbhijeetSingh 这里是更新后的链接: https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/mv2-archive/api/commands - Divyesh Kanzariya

1
如果您正在使用版本为3的manifest_version,那么您的manifest.json应该像下面这样,以便使用键盘快捷方式打开弹出窗口。
实际上,这里发生的事情是将键盘快捷键映射到_execute_action,而这个_execute_action会调用默认打开弹出窗口的action
{
    "manifest_version": 3,
    "name": "Search Hindi Website",
    "version": "1.0",
    "description": "Search a Hindi website using English text",
    "icons": {
        "16": "icon16.png",
        "32": "icon32.png",
        "48": "icon48.png",
        "128": "icon128.png"
    },
    "permissions": ["scripting", "activeTab"],
    "action": {
        "default_popup": "popup.html",
        "default_icon": {
            "16": "icon16.png",
            "32": "icon32.png",
            "48": "icon48.png",
            "128": "icon128.png"
        }
    },
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": ["https://*/*"],
            "css": ["mark.css"]
        }
    ],
    "commands": {
        "_execute_action": {
            "suggested_key": {
                "default": "Alt+Shift+P",
                "mac": "MacCtrl+Command+P"
            }
        }
    }
}

对于 manifest_version 2,其内容类似于以下内容

{
    "manifest_version": 3,
    "name": "Search Hindi Website",
    "version": "1.0",
    "description": "Search a Hindi website using English text",
    "icons": {
        "16": "icon16.png",
        "32": "icon32.png",
        "48": "icon48.png",
        "128": "icon128.png"
    },
    "permissions": ["scripting", "activeTab"],
    "browser_action": {
        "default_popup": "popup.html",
        "default_icon": {
            "16": "icon16.png",
            "32": "icon32.png",
            "48": "icon48.png",
            "128": "icon128.png"
        }
    },
    "background": {
        "service_worker": "background.js"
    },
    "content_scripts": [
        {
            "matches": ["https://*/*"],
            "css": ["mark.css"]
        }
    ],
    "commands": {
         "_execute_browser_action": {
            "suggested_key": {
                "default": "Alt+Shift+E",
                "mac": "MacCtrl+Command+E"
            }
        },
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接