自动取消下一步(YouTube 和 Bilibili)

Automatically cancel recommended video actions after YouTube and Bilibili videos have finished playing

安裝腳本?
作者推薦腳本

您可能也會喜歡 自动全屏

安裝腳本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         自动取消下一步(YouTube 和 Bilibili)
// @name:en      Auto Cancel Up Next (YouTube & Bilibili)
// @namespace    http://tampermonkey.net/
// @version      2024-11-24_1.1.5
// @description:zh-CN  自动取消 YouTube 和 Bilibili 视频播放完成后的推荐视频操作
// @description  Automatically cancel recommended video actions after YouTube and Bilibili videos have finished playing
// @author       屑屑
// @match        *://www.youtube.com/watch*
// @match        *://www.bilibili.com/video/*
// @icon         https://s2.loli.net/2024/04/28/WEkjH9iy51z63Of.jpg
// @grant        none
// @license      MIT
// ==/UserScript==
(function () {
    'use strict';

    function setupAutoCancel() {
        const isYouTube = location.host.includes('youtube.com');
        const isBilibili = location.host.includes('bilibili.com');

        if (isYouTube) {
            const video = document.querySelector('video');
            if (video) {
                video.addEventListener('ended', () => {
                    const button = document.querySelector('.ytp-autonav-endscreen-upnext-cancel-button');
                    if (button) {
                        button.click();
                        console.log('YouTube: 取消按钮已点击');
                        document.querySelector("#movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-right-controls > button.ytp-fullscreen-button.ytp-button").click();
                    } else {
                        console.log('YouTube: 未找到取消按钮');
                    }
                });
            }
        } else if (isBilibili) {
            const observer = new MutationObserver(() => {
                const button = document.querySelector(
                    '#bilibili-player > div > div > div.bpx-player-primary-area > div.bpx-player-video-area > div.bpx-player-ending-wrap > div.bpx-player-ending-panel > div > div > div.bpx-player-ending-related > a:nth-child(1) > div.bpx-player-ending-related-item-cover > div.bpx-player-ending-related-item-cancel'
                );
                if (button) {
                    button.click();
                    console.log('Bilibili: 取消按钮已点击');
                    document.querySelector("div.bpx-player-ctrl-btn.bpx-player-ctrl-web").click();
                    observer.disconnect(); // 成功点击后停止观察
                }
            });

            observer.observe(document.body, { childList: true, subtree: true });
        } else {
            console.log('当前网站不支持自动取消功能');
        }
    }

    // 启动脚本
    setupAutoCancel();
})();