抖音PC端直播增强

增加一个抖音直播追流按钮,可以获取到最新的直播画面。

// ==UserScript==
// @name         抖音PC端直播增强
// @namespace    http://dyliveplug.ddwhm.com/
// @version      1.5
// @description  增加一个抖音直播追流按钮,可以获取到最新的直播画面。
// @author       Jesen
// @match        https://live.douyin.com/*
// @icon         https://p-pc-weboff.byteimg.com/tos-cn-i-9r5gewecjs/favicon.png
// @grant        GM_addStyle
// @grant        GM_addElement
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        unsafeWindow
// @run-at       document-idle
// @license      GPLv2
// ==/UserScript==

(function() {
    'use strict';
    window.switchLive = false;
    // 旋转具体函数
    function rotate(e){
        e.style.transformOrigin = "center";
        e.classList.add("rrr");
    }
    function unRotate(e){
        e.style.transformOrigin = "";
        e.classList.remove("rrr");
    }
    // 旋转按钮点击事件
    function onRotateBtnclick(){
        let a = document.getElementsByTagName("video")[0];
        if(window.switchLive){
            unRotate(a);
            window.switchLive = false;
        }else{
            rotate(a);
            window.switchLive = true;
        }
    }
    // 追流按钮事件
    function onZhuiliuBtnclick(){
        let liveVideoNode = document.getElementsByTagName("video")[0];
        let buffered = liveVideoNode.buffered;
        if (buffered.length == 0) {
            // 暂停中
            return;
        }
        liveVideoNode.currentTime = buffered.end(0);
    }

    window.onloadFunc = function() {
        // 在页面中展示横屏、追流、网页全屏按钮。
        //const bodyElement = document.getElementsByTagName("body")[0];
        const xgrs = document.getElementsByTagName("xg-right-grid");
        //let ne = document.createElement("b");
        //ne.innerHTML= "<b style='color:red;'>横屏</b>";
        //ne.innerHTML= '<b style="margin-right:8px;" alt="横屏观看直播"><svg class="icon" height="18" width="18" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="display:block; margin-right:8px; fill: yellow;"><path d="M319.804952 169.984c198.241524-114.44419 451.730286-46.518857 566.198858 151.722667 114.44419 198.217143 46.518857 451.730286-151.722667 566.174476-8.606476 4.973714-17.310476 9.606095-26.087619 13.897143L671.451429 838.217143c8.850286-4.144762 17.603048-8.728381 26.233904-13.702095 163.254857-94.256762 219.184762-303.006476 124.952381-466.261334-94.256762-163.254857-303.030857-219.184762-466.285714-124.952381C193.121524 327.582476 137.191619 536.380952 231.448381 699.611429a339.748571 339.748571 0 0 0 146.139429 136.240761l-38.326858-103.424 68.583619-25.429333 58.514286 157.817905 0.731429 0.146286-0.585143 0.316952 20.626286 55.588571-217.916953 50.956191-16.65219-71.241143 63.463619-14.823619a412.598857 412.598857 0 0 1-147.919238-149.577143C53.638095 537.941333 121.563429 284.452571 319.804952 169.984z m189.756953 172.007619c4.315429 0 6.485333 2.194286 6.485333 6.485333v284.330667c0 4.33981-2.169905 6.485333-6.485333 6.485333h-195.584c-4.315429 0-6.485333-2.145524-6.485334-6.485333v-48.128c0-4.315429 2.169905-6.485333 6.485334-6.485333h129.024v-49.834667h-129.365334c-4.315429 0-6.485333-2.145524-6.485333-6.485333v-173.397334c0-4.315429 2.169905-6.485333 6.485333-6.485333H509.561905z m248.149333 0.682667c4.315429 0 6.485333 2.194286 6.485333 6.485333v281.941333c0 4.33981-2.169905 6.485333-6.485333 6.485334h-195.925333c-4.315429 0-6.485333-2.145524-6.485334-6.485334V349.159619c0-4.315429 2.169905-6.485333 6.485334-6.485333h195.925333z m-66.218667 61.44h-63.146666v172.032h63.146666v-172.032z m-248.490666-0.682667h-63.146667v63.829333h63.146667v-63.829333z"></path></svg></b>';
        let zl = document.createElement("b");
        //zl.innerHTML= "<b style='color:red;'>追流</b>";
        zl.innerHTML= '<b style="margin-right:8px;" alt="追流,顾名思义,强行收看最新直播"><svg class="icon" height="18" width="18" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="display:block; margin-right:8px; fill: yellow;"><path d="M151.3472 107.7248L197.632 61.44l404.2752 404.2752 18.2272 18.2272 28.0576 28.0576-450.56 450.56-46.2848-46.2848L555.6224 512zM807.194624 962.67264v-901.12h65.536v901.12z"></path></svg></b>';
        //bodyElement.insertBefore(ne,xgrs[xgrs.length-1-2]);
        xgrs[0].insertBefore(zl,xgrs[0].children[2]);
        //ne.firstChild.onclick = onRotateBtnclick;
        console.log("插件:加抖音直播按钮成功")
        zl.firstChild.onclick = onZhuiliuBtnclick;
        // 横屏样式
        var style = document.createElement('style');
        style.innerHTML = '.rrr { transform: rotate(270deg) !important; }';
        document.head.appendChild(style);
    };
    // 监测播放暂停按钮加载成功再加入横屏按钮,以免出现按钮没有成功展示的问题。
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            mutation.addedNodes.forEach(function(node) {
                // console.log(node);
                if (node.classList && node.classList.contains('xgplayer-fullscreen')) {
                    window.onloadFunc();
                    observer.disconnect();
                }
            });
        });
    });

    observer.observe(document, { childList: true, subtree: true });

})();