dilidili flash to html5 fix

把dilidili的Flash播放器替换为HTML5播放器

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         dilidili flash to html5 fix
// @namespace    http://tampermonkey.net/
// @version      0.2.4
// @description  把dilidili的Flash播放器替换为HTML5播放器
// @author       niphor
// @match        http*://*.dilidili.com/watch*
// @match        http*://*.dilidili.wang/watch*
// @match        http*://*.dilidili.name/watch*
// @run-at       document-body
// @grant        none
// @require      https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// ==/UserScript==

(function () {
    'use strict';

    /*(function(){
        const scripts = document.body.getElementsByTagName("script");
        for (var i=0;i<scripts.length;i++) {
            if (!scripts[i].src && scripts[i].innerHTML.indexOf('xiajiafanju.html')){
                document.body.removeChild(scripts[i]);
                break;
            }
        }
    })();*/
    // hijack jQuery.ready
    (function($){
        const _ready = $.fn.ready;
        const homeStr = 'home.html';
        $.fn.ready = function(fn){
            const fnStr = fn.toString();
            // 下架提示
            if(fnStr.indexOf('xiajiafanju')>=0){
                return
            }
            // 豆瓣跳转
            if(fnStr.indexOf('douban.com')>=0){
                // 跳转实际播放页
                if(fnStr.indexOf(homeStr)&&location.href.slice(-9) !== 'home.html'){
                    location.href = location.href + homeStr;
                }
                return
            }
            _ready.apply(this,Array.prototype.slice.call( arguments));
        }
    })(window.jQuery);

    function loadScriptOrCSS(src, callback){
        const isStyle = src.slice(-4) ==='.css';
        let dom = document.createElement(isStyle?'link':"script");
        if(isStyle){
            dom.rel = "stylesheet";
            dom.href = src;
        } else {
            dom.src = src;
        }
        if(callback){
            dom.onload = callback
        }
        document.head.appendChild(dom);
        return dom;
    }

    // 引用类库
    // 引用类库
    loadScriptOrCSS('https://cdnjs.cloudflare.com/ajax/libs/hls.js/8.0.0-beta.3/hls.min.js');
    loadScriptOrCSS('https://cdnjs.cloudflare.com/ajax/libs/dplayer/1.25.0/DPlayer.min.css');
    loadScriptOrCSS('https://cdnjs.cloudflare.com/ajax/libs/dplayer/1.25.0/DPlayer.min.js');

    // 避免播放器太大
    GM_addStyle('body{display:block!important;} .dplayer {max-width:100%;height:100%;}');

    // 页面加载完毕后运行播放器
    document.addEventListener("DOMContentLoaded", function(){

        // 找到原播放器
        let playerWrap = document.getElementsByClassName("player_main")[0];
        let videoSrc;

        if(!playerWrap){
            return
        }

        videoSrc = playerWrap.children[0].src.split("=")[1];

        // 不是m3u8的就不改
        if (videoSrc.slice(-4) != "m3u8") {
            return
        }

        // 新的video
        let player = document.createElement("div");
        player.setAttribute("id", "dplayer");

        // 替换播放器
        while (playerWrap.firstChild) {
            playerWrap.removeChild(playerWrap.firstChild);
        }
        playerWrap.appendChild(player);

        new window.DPlayer({
            container: document.getElementById('dplayer'),
            autoplay: true,
            video: {
                url: videoSrc,
                type:'hls'
            }
        });
    });
})();