YouTube Watch Later Shortcut

Add and remove current YouTube video to and from the Youtube Watch Later list using a keyboard shortcut.

当前为 2025-05-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Watch Later Shortcut
// @namespace    http://tampermonkey.net/
// @version      1.1.2
// @description  Add and remove current YouTube video to and from the Youtube Watch Later list using a keyboard shortcut.
// @author       kyleczhang
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Function to execute YouTube commands (add or remove from Watch Later)
    function executeYouTubeCommand(action) {
        const videoId = new URL(window.location.href).searchParams.get("v");
        const appElement = document.querySelector("ytd-app");

        // Check if video ID and YouTube app element are found
        if (!videoId || !appElement) {
            return;
        }

        const params = {
            clickTrackingParams: "",
            commandMetadata: { webCommandMetadata: { sendPost: true, apiUrl: "/youtubei/v1/browse/edit_playlist" } },
            playlistEditEndpoint: {
                playlistId: "WL",
                actions: []
            }
        };

        if (action === "add") {
            params.playlistEditEndpoint.actions.push({ addedVideoId: videoId, action: "ACTION_ADD_VIDEO" });
        } else if (action === "remove") {
            params.playlistEditEndpoint.actions.push({ action: "ACTION_REMOVE_VIDEO_BY_VIDEO_ID", removedVideoId: videoId });
        }

        const event = new window.CustomEvent('yt-action', {
            detail: {
                actionName: 'yt-service-request',
                returnValue: [],
                args: [{ data: {} }, params],
                optionalAction: false,
            }
        });

        // Dispatch the event to execute the action
        appElement.dispatchEvent(event);
    }

    // Function to add keyboard shortcuts for adding/removing videos
    function addKeyboardShortcuts() {
        document.addEventListener('keydown', function (event) {
            if (event.code === 'KeyR' && event.altKey && event.shiftKey) {
                executeYouTubeCommand("add");
            } else if (event.code === 'KeyF' && event.altKey && event.shiftKey) {
                executeYouTubeCommand("remove");
            }
        });
    }

    // Initialize the script by adding keyboard shortcuts
    addKeyboardShortcuts();
})();