油管剧场小窗模式

Youtube在剧场模式下,如果是小窗口(宽度小于876像素),视频会出现迷之黑边,去掉它们并保证视频占据整个窗口。并让顶部工具栏只在鼠标悬停时显示。(推荐配合WebAPP模式、窗口置顶工具一同使用)

当前为 2023-12-31 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         油管剧场小窗模式
// @version      1.0.2
// @description  Youtube在剧场模式下,如果是小窗口(宽度小于876像素),视频会出现迷之黑边,去掉它们并保证视频占据整个窗口。并让顶部工具栏只在鼠标悬停时显示。(推荐配合WebAPP模式、窗口置顶工具一同使用)
// @author       CWBeta
// @include      *youtube.com*
// @icon         https://www.google.com/s2/favicons?domain=youtube.com
// @namespace    https://greasyfork.org/users/670174
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    function CheckBlackFrame(){
        if (window.innerWidth >= 876){
            return;
        }
        var videoWindow = document.querySelector(".video-stream.html5-main-video");
        var videoWrapper = document.querySelector("#full-bleed-container");
        var topValue = videoWindow.style.top;
        var height = parseInt(videoWindow.style.height.substr(0,videoWindow.style.height.length-2));
        var width = parseInt(videoWindow.style.width.substr(0,videoWindow.style.width.length-2));
        if (videoWindow.getAttribute("cw-width") !=window.innerWidth
            || videoWindow.getAttribute("cw-height") != window.innerHeight)
        {
            console.log("窗口变化,重置黑边!")
            videoWindow.style["top"] = "0px";
            videoWrapper.style["height"] = window.innerHeight + "px";
            videoWindow.style["top"] = (window.innerHeight - height) / 2 + "px";
            //videoWrapper.style["height"] = height;
            videoWrapper.style["min-height"] = "0px";
            videoWrapper.style["max-height"] = "99999px";
        }
        videoWindow.setAttribute("cw-width",window.innerWidth);
        videoWindow.setAttribute("cw-height",window.innerHeight);
    }

    function CheckPageLoad(){
        var shouldRefresh = false;

        // 网址变了则刷新(因为Youtube会动态加载而不会刷新,所以需要主动调用刷新)
        if(cwData["href"] != location.href){
            console.log("网页地址跳转:" + cwData["href"] + " → " + location.href);
            shouldRefresh = true;
        }

        // 如果切换了播放模式(普通、剧场、全屏),则主动调用刷新以避免在非剧场模式启用样式变化
        var layout = GetLayout();
        if (cwData["layout"] != layout)
        {
            console.log("播放样式切换:" + cwData["layout"] + " → " + layout);
            shouldRefresh = true;
        }
        cwData["layout"] = layout;

        if(shouldRefresh){
            Refresh();
        }
    }

    function GetLayout(){
        var element = document.querySelector('ytd-watch-flexy');
        var layout = "";
        if(element.hasAttribute("default-layout")){
            layout = "default";
        }
        else if(element.hasAttribute("fullscreen")){
            layout = "fullscreen";
        }
        else{
            layout = "theater";
        }
        return layout;
    }

    function AddStyle(){
        var oldStyle = document.getElementById("cw-style");
        if(oldStyle != undefined){
            oldStyle.parentNode.removeChild(oldStyle);
        }

        var style = document.createElement("style");
        style.id = "cw-style";
        style.type = "text/css";
        var cssString = "html::-webkit-scrollbar { width: 0;} "

        // 当网页为视频播放页,而且是剧场模式时,启用样式表
        if (location.href.indexOf("/watch?") != -1
            && cwData["layout"]=="theater"){
            cssString += "html{--ytd-toolbar-height:0px} #masthead-container.ytd-app{opacity:0; transition:1s;} #masthead-container.ytd-app:hover{opacity:1}"
        }
        try
        {
            style.appendChild(document.createTextNode(cssString));
        }
        catch(ex)
        {
            style.styleSheet.cssText = cssString;//针对IE
        }
        var head = document.getElementsByTagName("head")[0];
        head.appendChild(style);
    }

    function Update(){
        CheckBlackFrame();
        CheckPageLoad();
    }

    function OnLoad(){
        if (cwData["init"] == undefined){
            Init();
            cwData["init"] = true;
        }
    }

    function Init(){
        console.log("[油管剧场小窗模式] 运行中!")
        Refresh();
    }

    function Refresh(){
        console.log("刷新状态!")
        AddStyle();
        if(cwData["updateInterval"] != undefined){
            clearInterval(cwData["updateInterval"]);
        }
        cwData["updateInterval"] = setInterval(Update,500);
        cwData["href"] = location.href;
        cwData["layout"] = GetLayout();
    }

    var cwData = {};

    window.addEventListener("load", function() {
        OnLoad();
    });

})();