替换 iwara 播放器

修复iwara播放器加载源错误问题

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         替换 iwara 播放器
// @namespace    https://github.com/s0urcelab/userscripts
// @version      1.1
// @description  修复iwara播放器加载源错误问题
// @match        https://www.iwara.tv/video/*
// @require      https://fastly.jsdelivr.net/npm/[email protected]/browser/index.min.js
// @icon         https://www.iwara.tv/logo.png
// @author       s0urce
// @license      MIT
// ==/UserScript==

; (function () {
    'use strict';

    // const css=`
    //   .videoPlayer {
    //     display: none !important;
    //   }
    // `
    // GM_addStyle(css)
    // Save the original fetch function
    const originalFetch = window.fetch;
    // Create a new fetch function that wraps the original one
    function newFetch(...args) {
        const [url] = args
        return originalFetch(...args).then(response => {
            if (url.includes('files.iwara.tv/file')) {
                response.clone().json().then(json => {
                    const { src: { view: videoSrc } } = json.find(v => v.name === 'Source')
                    console.warn(`当前播放资源url:${videoSrc}`)
                    window.videoSrc = videoSrc
                })
            }
            return response;
        });
    }
    // Replace the original fetch function with our new one
    window.fetch = newFetch;



    const targetNode = document.getElementById('app');
    const observerOptions = {
        childList: true,
        subtree: true,
    };
    function callback(mutationList, observer) {
        mutationList.forEach((mutation) => {
            switch (mutation.type) {
                case 'childList':
                    for (let node of mutation.addedNodes) {
                        if (node.className === 'videoPlayer') {
                            node.style.display = 'none'
                            document.querySelector(".page-video__player").style.maxHeight = 'none'
                            // append container for xgplayer
                            const playCon = document.createElement("div")
                            playCon.id = 'xgplayer'
                            node.parentNode.appendChild(playCon)
                            window.xg = new window.Player({
                                id: 'xgplayer',
                                url: window.videoSrc,
                                volume: 1,
                                fluid: true,
                                videoInit: true,
                            })

                            observer.disconnect()
                        }
                    }
                    break;
            }
        });
    }
    const observer = new MutationObserver(callback);
    observer.observe(targetNode, observerOptions);

    // function checkAndReplace() {
    //     const originContainer = document.querySelector(".videoPlayer");
    //     if (originContainer) {
    //         originContainer.style.display = 'none'
    //         document.querySelector(".page-video__player").style.maxHeight = 'none'
    //         // append container for xgplayer
    //         const playCon = document.createElement("div")
    //         playCon.id = 'xgplayer'
    //         originContainer.parentNode.appendChild(playCon)
    //         window.xg = new window.Player({
    //             id: 'xgplayer',
    //             url: window.videoSrc,
    //             volume: 1,
    //             fluid: true,
    //             videoInit: true,
    //         })
    //     } else {
    //         // If the element doesn't exist yet, try again in 500ms
    //         setTimeout(checkAndReplace, 500);
    //     }
    // }
    // Start checking for the element
    // checkAndReplace();


})();