[Youtube Music] Pause video/music with Alt+K on any page

Pause youTube video/music with Alt+K on any page

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name                [Youtube Music] Pause video/music with Alt+K on any page
// @namespace           https://greasyfork.org/users/821661
// @match               https://*/*
// @grant               GM_getValue
// @grant               GM_setValue
// @grant               GM_addValueChangeListener
// @version             1.1
// @author              hdyzen
// @description         Pause youTube video/music with Alt+K on any page
// @license             GPL-3.0-only
// ==/UserScript==

function init() {
    const domain = window.location.hostname;

    if (domain === "music.youtube.com") {
        onYoutubeMusic();
    }

    onEveryPage();
}
init();

function onEveryPage() {
    window.addEventListener("keydown", handlerKey);
}

function onYoutubeMusic() {
    const callback = () => {
        const video = document.querySelector(".video-stream");

        video.paused ? video.play() : video.pause();
    };

    GM_addValueChangeListener("toggleVideo", callback);
}

function handlerKey(ev) {
    const isAltKPressed = ev.altKey && ev.code === "KeyK";

    if (!isAltKPressed) {
        return;
    }

    GM_setValue("toggleVideo", !GM_getValue("toggleVideo"));
}