Chrome 扩展程序命令切换

发布于 2025-01-10 21:55:57 字数 1656 浏览 0 评论 0原文

我正在编写一个简单的 Chrome 扩展程序,用于拦截媒体键事件并与网站交互。

我已将其设置为可以通过单击扩展来关闭和打开它。打开时,它会监视选项卡并在按下媒体键时向其分派事件。当它关闭时,它仍然会拦截媒体按键,但不会对其执行任何操作。

我的问题是,当扩展程序“关闭”时,我想停止拦截媒体按键。如果我的扩展程序未安装,我希望 Chrome 能够像平常一样使用媒体键。我认为有一种方法可以停止拦截按键,或者将它们转发到 Chrome,但我不知道如何实现。

目前,我正在使用清单通过以下代码拦截按键:

{
    .... a bunch of stuff ...

    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "commands": {
        "playPause": {
            "suggested_key": {
                "default": "MediaPlayPause"
            },
            "global": true,
            "description": "Toggle play/pause"
        },
        "playNext": {
            "suggested_key": {
                "default": "MediaNextTrack"
            },
            "global": true,
            "description": "Play next track"
        },
        "playPrev": {
            "suggested_key": {
                "default": "MediaPrevTrack"
            },
            "global": true,
            "description": "Play previous track"
        }
    }
}

然后我用以下代码捕获按键:

function commandFired(command) {
    chrome.storage.local.get('activeTabId', result => {
        if (result.activeTabId == -1) {
            //send to chrome normally here
            return;
        }

        switch (command) {
            case "playNext":
                //Handling next
                break;
            case "playPrev":
                //Handling prev
                break;
            case "playPause":
                //Handling play
                break;
        }
    });
}

chrome.commands.onCommand.addListener(commandFired);

I'm writing a simple Chrome extension that intercepts media key events and interacts with a website.

I have it set up so that the extension can be toggled off and on by clicking it. While on, it monitors a tab and dispatches events to it when the media keys are pressed. When it's off, it still intercepts the media key presses, but doesn't do anything with them.

My problem is that I want to stop intercepting the media key presses when the extension is "off". I would like Chrome to utilize the media keys the way it normally would if my extension wasn't installed. I assume there is a way to stop intercepting the key presses, or to forward them to Chrome, but I can't figure out how.

Currently I'm using the manifest to intercept the key presses with this code:

{
    .... a bunch of stuff ...

    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },
    "commands": {
        "playPause": {
            "suggested_key": {
                "default": "MediaPlayPause"
            },
            "global": true,
            "description": "Toggle play/pause"
        },
        "playNext": {
            "suggested_key": {
                "default": "MediaNextTrack"
            },
            "global": true,
            "description": "Play next track"
        },
        "playPrev": {
            "suggested_key": {
                "default": "MediaPrevTrack"
            },
            "global": true,
            "description": "Play previous track"
        }
    }
}

Then I'm catching the key presses with this:

function commandFired(command) {
    chrome.storage.local.get('activeTabId', result => {
        if (result.activeTabId == -1) {
            //send to chrome normally here
            return;
        }

        switch (command) {
            case "playNext":
                //Handling next
                break;
            case "playPrev":
                //Handling prev
                break;
            case "playPause":
                //Handling play
                break;
        }
    });
}

chrome.commands.onCommand.addListener(commandFired);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ懵逼小萝莉 2025-01-17 21:55:57

Chrome 没有以编程方式禁用热键的 API,因此您必须取消注册整个 Service Worker,并在弹出窗口中提供一个按钮来重新注册它。

manifest.json:

  "action": {"default_popup": "popup.html"},

popup.html:

<label><input type=checkbox id=power>Enabled</label>
<script src=popup.js></script>

popup.js:

Object.assign(document.getElementById('power'), {
  checked: !!navigator.serviceWorker.controller,
  async onchange() {
    if (this.checked) {
      const bg = chrome.runtime.getManifest().background;
      await navigator.serviceWorker.register(bg.service_worker, {type: bg.type});
    } else {
      (await navigator.serviceWorker.getRegistration())?.unregister();
    }
  },
});

如果您想继续侦听其他 chrome 事件,请在取消注册主文件后注册另一个 bg2.js 文件。主bg.js可以导入bg2.js来删除重复代码,更多信息

Chrome doesn't have an API to disable hotkeys programmatically so you'll have to unregister the entire service worker and offer a button in the popup to re-register it.

manifest.json:

  "action": {"default_popup": "popup.html"},

popup.html:

<label><input type=checkbox id=power>Enabled</label>
<script src=popup.js></script>

popup.js:

Object.assign(document.getElementById('power'), {
  checked: !!navigator.serviceWorker.controller,
  async onchange() {
    if (this.checked) {
      const bg = chrome.runtime.getManifest().background;
      await navigator.serviceWorker.register(bg.service_worker, {type: bg.type});
    } else {
      (await navigator.serviceWorker.getRegistration())?.unregister();
    }
  },
});

In case you want to keep listening to other chrome events, register a different bg2.js file after unregistering the main one. The main bg.js can import bg2.js to deduplicate the code, more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文