Greasy Fork

视频网站自动网页全屏

支持哔哩哔哩、B站直播、腾讯视频、优酷视频、爱奇艺、芒果TV、搜狐视频、AcFun弹幕网播放页自动网页全屏,B站视频播放完成后自动退出网页全屏

目前为 2024-12-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         视频网站自动网页全屏
// @license      MIT
// @author       Feny
// @version      0.9.7
// @namespace    http://tampermonkey.net/
// @description  支持哔哩哔哩、B站直播、腾讯视频、优酷视频、爱奇艺、芒果TV、搜狐视频、AcFun弹幕网播放页自动网页全屏,B站视频播放完成后自动退出网页全屏
// @match      *://tv.sohu.com/v/*
// @match      *://www.mgtv.com/b/*
// @match      *://www.iqiyi.com/v_*
// @match      *://v.pptv.com/show/*
// @match      *://haokan.baidu.com/v*
// @match      *://v.youku.com/v_show/*
// @match      *://v.qq.com/x/page/*
// @match      *://v.qq.com/x/cover/*
// @match      *://v.qq.com/live/p/newtopic/*
// @match      *://www.acfun.cn/v/*
// @match      *://live.acfun.cn/live/*
// @match      *://www.acfun.cn/bangumi/*
// @match      *://live.bilibili.com/*
// @match      *://www.bilibili.com/list/*
// @match      *://www.bilibili.com/video/*
// @match      *://www.bilibili.com/festival/*
// @match      *://www.bilibili.com/cheese/play/*
// @match      *://www.bilibili.com/bangumi/play/*
// @icon         data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAqdJREFUWEftl91LFFEYxp/3jB9ESZjtSl51F1RUSgRCF/kHlF1IhiFhF65dqEQkBUErdJMStBukGwQre2NZUiCRqUiURkW65mIfqGUFsW6Ii0jY7p4Tc3Rqd5zaGVldAudynve8z28e3jMzh5Dmi1R/V0vQyRRWxgWG6x22SrcnOAhQcQIbwVtXba8y1EANSpS1xzJin5c/Dz+jRDPvGWoErwRw35zuh8ChpcXXFjbwi9k/WADA9viGgovGnxtFs6EmcApMvCdBA3oIIirl4N8NNQngmRYJiwTOE7EHHLERAmXFawQ6AdCQkRbjsZIMUvIFoV0HMSsEDjCgSK8tJqAHAEDAMWLKLOexx8tiVVDEhLLVQAtzRPcwKOUANSWCw1/rsBe6PcFz8dpfAdTFgtF+EmIvBG7pID7mZNl2zkVCFQbahzqHfYerddpNhFpdsnfqauzl8ZoEuO4JXdIKOefynnZlimxXhBbqjTZL/el8pzrAVjTGmKh12Bq1ddJs974abQDXfFMuAhQ6EodwDTHWAf6/BAoK8nD0cDEKtuVhyD+OzvvLXnyWJshyApedJ1F65M9n4tlAAF5fL168fGfJWCu2DDA61GpodLvjCdp8vfjyNWQJJGUAquvMzBzafD0yEc65KZCUAmiOo4FPEqS753VSiFUB0FxbPF244en6J8SqAoTD8zhYcjZ9AP6RCVRWNacHYPD5GJqudmBi8tvaAkxNBeUuuNv5NOkAqgUpm4FIJCrfA+r0z4bnTZmvCKCv+wrsts0JBg8fvZLGY28NfoqToFhOoOJ4CS40lMu2I28mpXFP37DpJ9YXWgZQG+Tm5mBL7qakA2aGakUAZhqbrVkH0BLoB34fzcyml5K6pd/yaicRlQlgV0q6mmwitMOpyfpVKfsFya4w73cz9xQAAAAASUVORK5CYII=
// ==/UserScript==

(function () {
    "use strict";

    // 针对:@match *://live.bilibili.com/*
    // 仅限像 live.bilibili.com/blanc/数字、live.bilibili.com/数字,才放行
    const host = location.host, href = location.href;
    const regExp = /live.bilibili.com\/(blanc\/)?\d+/;
    if (host === "live.bilibili.com" && !regExp.test(href)) return;

    // 重写pushState方法
    const orig = history.pushState;
    history.pushState = function () {
        orig.apply(this, arguments);
        window.dispatchEvent(new Event('pushstate'));
    };

    // 重写replaceState方法
    const original = history.replaceState;
    history.replaceState = function () {
        original.apply(this, arguments);
        window.dispatchEvent(new Event('replaceState'));
    };

    const selector = {
        "v.pptv.com": ".w-expandIn",
        "www.acfun.cn": ".fullscreen-web",
        "live.acfun.cn": ".fullscreen-web",
        "v.youku.com": "#webfullscreen-icon",
        "www.mgtv.com": ".webfullscreenBtn i",
        "www.iqiyi.com": ".iqp-btn-webscreen",
        "v.qq.com": 'div[aria-label="网页全屏"]',
        "tv.sohu.com": 'button[data-title="网页全屏"',
        "www.bilibili.com": 'div[aria-label="网页全屏"]',
        "live.bilibili.com": "#businessContainerElement",
        "haokan.baidu.com": 'div[aria-label="网页全屏"]',
    }

    const _q = (selector) => document.querySelector(selector);
    const webfullscreen = {
        init() {
            let i = 0;
            const time = 30000, delay = 300, intervalID = setInterval(() => {
                const video = this.getVideo();
                const element = this.getElement();
                if (++i * delay >= time) clearInterval(intervalID);
                if (element) this.fullScreen(element) && clearInterval(intervalID);
                if (video && /bilibili.com\/video/.test(href)) this.exitWebFull(video);
            }, delay);
        },
        getVideo: () => _q("video"),
        getElement: () => _q(selector[host]),
        fullScreen(elem) {
            // B站直播
            if (host === "live.bilibili.com") {
                parent.scrollTo({ top: 100 });
                localStorage.setItem("FULLSCREEN-GIFT-PANEL-SHOW", 0);
                document.body.classList.add("hide-asida-area", "hide-aside-area");
                elem.dispatchEvent(new Event("dblclick", { view: parent, bubbles: true }));
                setTimeout(() => _q("#shop-popover-vm")?.remove(), 500);
                let i = 0, intervalID = setInterval(() => {
                    if (++i >= 5) clearInterval(intervalID);
                    const popup = _q(".side-bar-popup-cntr");
                    if (popup) popup.remove() & clearInterval(intervalID);
                }, 500);
                return true;
            }

            const video = this.getVideo();
            if (!video) return false;
            const w = video.offsetWidth;
            if (0 === w) return false;
            if (window.innerWidth === w) return true;
            elem.click ? elem.click() : elem.dispatchEvent(new Event("click"));
            return true;
        },
        exitWebFull(video) {
            // B站视频播放完成后自动退出网页全屏,取消连播
            video.addEventListener('ended', () => {
                if (window.innerWidth === video.offsetWidth) {
                    this.getElement().click();
                    const cancel = _q(".bpx-player-ending-related-item-cancel");
                    if (cancel) cancel.click();
                }
            });
        }
    };

    // 关闭B站视频自动连播
    // localStorage.setItem("recommend_auto_play", "close");

    webfullscreen.init();
    window.addEventListener('pushstate', () => webfullscreen.init());
    window.addEventListener('replaceState', () => webfullscreen.init());
})();