簡體   English   中英

Chrome擴展通過命令打開彈出窗口

[英]Chrome Extension open popup by command

我想使用快捷鍵Ctrl + B打開我的擴展的popup.html ,所以我不需要移動鼠標點擊。

我嘗試了谷歌文檔中的commands配置,但似乎不起作用。 有誰知道如何解決這一問題? 以下是我的配置(請注意,我使用的是manifest_version: 3

manifest.json:

{
  "name": "",
  "description": "",
  "version": "0.0.1",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "action": {
    "default_popup": "popup.html"
  },
"commands": {
    "_execute_action": {
      "suggested_key": {
        "default": "Ctrl+B",
        "mac": "Ctrl+B"
      }
    }
  }
}

popup.html

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  Hello world
</body>
</html>

背景.js

// Currently empty

不知道這是否適合您,但如果彈出的 position 對您來說不是問題。 (在清單版本 3 中的本地主機上執行)

manifest.json

 "commands": {
 "my_shortcut": {
  "suggested_key": {
    "default": "Ctrl+B",
    "mac": "Command+B"
  },
  "description": "Opens popup.html",
  "global": true
},
}

背景.js

chrome.commands.onCommand.addListener((command) => {

if (command === 'my_shortcut')
    chrome.system.display.getInfo({ singleUnified: true }, (info) => {
    
      const wDimension = info[0].workArea;
      const { top, left, height, width } = wDimension;
      console.log(top);
      const w = 440;
      const h = 220;
      const l = width / 2 - w / 2 + left;
      const t = height / 2 - h / 2 + top;
      const newWindow = () => {
        console.log('in new window function');
      };
      chrome.windows.create(
        {
          url: 'popup.html',
          type: 'popup',
          width: w,
          height: h,
          left: Math.round(l),
          top: Math.round(t),
        },
        newWindow
      );
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM