動畫瘋下載器

取得動畫的 m3u8 網址,並可使用 PotPlayer 播放

目前为 2025-04-21 提交的版本。查看 最新版本

// ==UserScript==
// @name        動畫瘋下載器
// @namespace
// @description 取得動畫的 m3u8 網址,並可使用 PotPlayer 播放
// @version     1.7.1
// @author
// @match       https://ani.gamer.com.tw/animeVideo.php?sn=*
// @connect     ani.gamer.com.tw
// @grant       none
// @namespace 
// ==/UserScript==

(function () {
    'use strict';

    const mode = 0; //複製模式(0:複製完整指令|1:複製URL+名稱)
    const timeOut = 600;
    const defualtInfo = '複製下載指令或使用外部播放器';
    let m3u8_url = '';
    let Name = '';

    // 讀取已儲存的路徑,若無則使用預設
    let downloadPath = localStorage.getItem('anig_download_path') || '%USERPROFILE%/Downloads';
    let PSdownloadPath = downloadPath.replace(/%([^%]+)%/g, '$Env:$1');

    function initPlayContainer(container) {

        // 創建使用 MPV 播放的按鈕
        const MPVLink = document.createElement('a');
        MPVLink.classList.add('anig-tb');
        MPVLink.textContent = 'MPV';
        MPVLink.title = '使用 MPV 播放';
        MPVLink.addEventListener('click', function () {
            const MPVUrl = `${m3u8_url} --http-header-fields="origin: https://ani.gamer.com.tw" --force-media-title="${Name}"`; // 構建 MPV 協議的 URL
            navigator.clipboard.writeText(MPVUrl);
            window.open('mpv:', '_self'); // 開啟 PotPlayer
        });
        playContainer.appendChild(MPVLink);

        // 創建使用 PotPlayer 播放的按鈕
        const potplayerLink = document.createElement('a');
        potplayerLink.classList.add('anig-tb');
        potplayerLink.textContent = 'PotPlayer';
        potplayerLink.title = '使用 PotPlayer 播放';
        potplayerLink.addEventListener('click', function () {
            const potplayerUrl = `${m3u8_url} /sub="" /headers="origin: https://ani.gamer.com.tw" /current /title="${Name}"`; // 構建 PotPlayer 協議的 URL
            navigator.clipboard.writeText(potplayerUrl);
            window.open('potplayer:', '_self'); // 開啟 PotPlayer
        });
        playContainer.appendChild(potplayerLink);

        // 創建使用 ffmpeg 下載的按鈕
        const ffmpegLink = document.createElement('a');
        ffmpegLink.classList.add('anig-tb');
        ffmpegLink.textContent = 'ffmpeg';
        ffmpegLink.title = '使用 ffmpeg 下載';
        ffmpegLink.addEventListener('click', function () {
            const ffmpegDlUrl = `& ffmpeg -headers "Origin: https://ani.gamer.com.tw" -i "${m3u8_url}" -c copy "${PSdownloadPath}/${Name}.mkv";`; // 構建 PotPlayer 協議的 URL
            navigator.clipboard.writeText(ffmpegDlUrl);
            window.open('ffmpeg:', '_self'); // 開啟 ffmpeg
        });
        playContainer.appendChild(ffmpegLink);

        // 創建複製 ffmpeg 指令的按鈕
        const CopyffmpegLink = document.createElement('a');
        CopyffmpegLink.classList.add('anig-tb');
        CopyffmpegLink.textContent = 'ffmpeg 🗍';
        CopyffmpegLink.title = '複製ffmpeg指令';
        CopyffmpegLink.addEventListener('click', function () {
            let ffmpegUrl
            if (mode==0){
                ffmpegUrl = `ffmpeg -headers "Origin: https://ani.gamer.com.tw" -i "${m3u8_url}" -c copy "${downloadPath}/${Name}.mkv" && exit`; // 構建 PotPlayer 協議的 URL
            }else{
                ffmpegUrl = `${m3u8_url}@${Name}.mkv"`; // 構建 PotPlayer 協議的 URL
            }
            navigator.clipboard.writeText(ffmpegUrl);
            titleDisplay.textContent = '複製成功!'; // 提示成功
            setTimeout(() => {
                titleDisplay.textContent = defualtInfo; // 恢復提示文字
            }, timeOut);
        });
        playContainer.appendChild(CopyffmpegLink);


        // 輸入框
        const input = document.createElement('input');
        input.type = 'text';
        input.value = downloadPath;
        input.placeholder = '下載路徑';
        input.style.cssText = `
            width: 178px;
            margin-right: 5px;
            padding: 4px;
            background-color: #000;  /* 黑底 */
            color: #fff;             /* 白字 */
            border: None;
        `;

        // 儲存按鈕
        const btn = document.createElement('buttonSave');
        btn.textContent = '儲存';
        btn.classList.add('anig-tb');
        btn.addEventListener('click', () => {
            downloadPath = input.value.trim() || downloadPath;
            localStorage.setItem('anig_download_path', downloadPath);
            PSdownloadPath = downloadPath.replace(/%([^%]+)%/g, '$Env:$1');
            titleDisplay.textContent = '已儲存!'; // 提示成功
            setTimeout(() => {
                titleDisplay.textContent = defualtInfo; // 恢復提示文字
            }, timeOut);
        });

        container.appendChild(input);
        container.appendChild(btn);
    }

    // 注入樣式到頁面中
    function injectStyles(css) {
        const style = document.createElement('style'); // 創建 <style> 元素
        style.textContent = css; // 設置樣式內容
        document.head.appendChild(style); // 將 <style> 添加到 <head>
    }

    // 解析 m3u8 播放列表,並在頁面上生成按鈕供使用者複製鏈接或使用 PotPlayer 播放
    async function parsePlaylist() {
        const req = playlist.src; // 獲取播放列表的 URL
        const response = await fetch(req); // 請求播放列表
        const text = await response.text(); // 獲取回應的文字內容
        const urlPrefix = req.replace(/playlist.+/, ''); // 提取 URL 前綴
        const m3u8List = text.match(/=\d+x\d+\n.+/g); // 匹配所有清晰度的 m3u8 連結

        // 生成動畫名稱,作為文件名使用
        const fullwidthMap = {
            '<': '<','>': '>',':': ':','"': '"','/': '/',
            '\\': '\','|': '|','?': '?','!': '!','*': '*'
        };
        Name = document.title.replace(" 線上看 - 巴哈姆特動畫瘋", "");
        Name = Name.replace(/[\/\\<>:"*|?!]/g, char => {
            return fullwidthMap[char] || '_';
        });
        titleDisplay.textContent = defualtInfo;
        // 遍歷每個 m3u8 連結,生成對應的按鈕
        for (const item of m3u8List) {
            let key = item.match(/=\d+x(\d+)/)[1]; // 提取清晰度(如 720)
            let url = item.match(/.*chunklist.+/)[0]; // 提取 m3u8 文件的相對路徑
           url = urlPrefix + url; // 拼接成完整的 m3u8 URL
            // 創建複製鏈接的按鈕
            const copyLink = document.createElement('a');
            copyLink.classList.add('anig-tb');
            copyLink.textContent = `${key}p`;
            copyLink.title = `切換解析度為${key}p`;
            // 被點擊時
            copyLink.addEventListener('click', function () {
                m3u8_url = url; // 更新URL
                m3u8Container.querySelectorAll('a.anig-tb').forEach(el => {
                    el.textContent = el.textContent.replace(/✅\s*$/, '');
                });
                copyLink.textContent = `${key}p✅`;
                titleDisplay.textContent = `切換解析度為${key}p`; // 提示成功
                setTimeout(() => {
                    titleDisplay.textContent = defualtInfo; // 恢復提示文字
                }, timeOut);
            });
            m3u8Container.appendChild(copyLink);
            // 主動點擊最後一個項目
            const allBtns = m3u8Container.querySelectorAll('a.anig-tb');
            if (allBtns.length > 0) {
                allBtns[allBtns.length - 1].click();
            }
        }
    }

    // 獲取播放列表,並等待廣告結束
    async function getPlaylist() {
        const req = `https://ani.gamer.com.tw/ajax/m3u8.php?sn=${AniVideoSn}&device=${DeviceID}`; // 構建請求 URL
        titleDisplay.textContent = '等待廣告...'; // 提示使用者等待廣告

        let retries = 0; // 重試次數計數器
        const maxRetries = 20; // 最多嘗試次數,防止無限循環

        // 循環請求播放列表,直到獲取到有效的播放地址或達到最大重試次數
        while (retries < maxRetries) {
            const response = await fetch(req); // 發送請求
            playlist = await response.json(); // 解析 JSON 資料

            // 如果獲取到有效的播放地址(不包含廣告)
            if (playlist.src && playlist.src.includes('https')) {
                break; // 跳出循環
            }

            await new Promise(resolve => setTimeout(resolve, 3000)); // 等待 3 秒再重試
            retries++; // 增加重試次數
        }

        // 判斷是否成功獲取播放列表
        if (playlist.src && playlist.src.includes('https')) {
            await parsePlaylist(); // 解析播放列表並生成按鈕
        } else {
            titleDisplay.textContent = '獲取播放列表失敗'; // 提示使用者失敗
        }
    }


    // 獲取設備 ID,這是請求播放列表所需的參數
    async function getDeviceId() {
        const req = 'https://ani.gamer.com.tw/ajax/getdeviceid.php'; // 請求設備 ID 的 URL
        const response = await fetch(req); // 發送請求
        const data = await response.json(); // 解析 JSON 資料
        DeviceID = data.deviceid; // 提取設備 ID
        await getPlaylist(); // 繼續獲取播放列表
    }

    // Main
    // 從 URL 中獲取動畫的編號(AniVideoSn)
    let AniVideoSn = new URLSearchParams(window.location.search).get('sn');
    // 定義全域變數
    let DeviceID; // 設備 ID
    let playlist; // 播放列表資料

    // 定義樣式
    const css =`
    .anig-ct {
        margin:5px;
        margin-left:17px;
    }

    .anig-tb {
        display: inline-block;
        padding: 5px;
        background: #50b2d7;
        color: #FFF;
        margin-right: 5px;
        cursor: pointer;
    }`;

    // 創建顯示提示信息的元素
    const titleDisplay = document.createElement('div');
    titleDisplay.classList.add('anig-tb');
    titleDisplay.textContent = '載入中...'; // 初始提示文字
    const titleContainer = document.createElement('div'); // 創建一個容器來包裹提示信息
    titleContainer.classList.add('anig-ct');
    titleContainer.appendChild(titleDisplay); // 將提示元素添加到容器中
    // 創建容器,用於放置清晰度
    const m3u8Container = document.createElement('div');
    m3u8Container.classList.add('anig-ct');
    // 創建容器,用於放置播放下載按鈕
    const playContainer = document.createElement('div');
    playContainer.classList.add('anig-ct');
    // 將容器添加到頁面中的指定位置
    const Downloader = document.createElement('div');
    Downloader.appendChild(titleContainer);
    Downloader.appendChild(m3u8Container);
    Downloader.appendChild(playContainer);
    const animeName = document.querySelector('.videoname');
    animeName.appendChild(Downloader);

    initPlayContainer(playContainer);

    /**
     * 為頁面中的集數連結添加點擊事件監聽
     * 當使用者點擊不同的集數時,更新 AniVideoSn 並重新獲取播放列表
     */
    document.querySelectorAll('a[data-ani-video-sn]').forEach(link => {
        link.addEventListener('click', function () {
            AniVideoSn = this.getAttribute('data-ani-video-sn'); // 更新 AniVideoSn
            m3u8Container.innerHTML = ''; // 清空按鈕容器
            titleDisplay.textContent = '載入中...'; // 更新提示文字
            getDeviceId(); // 重新獲取設備 ID 並獲取播放列表
        });
    });

    /**
     * 新增檢查機制,監測 URL 和 AniVideoSn 是否發生變化
     * 如果發生變化,則重新獲取播放列表,確保資料的及時更新
     */
    let lastAniVideoSn = AniVideoSn; // 保存上一次的 AniVideoSn
    let lastUrl = window.location.href; // 保存上一次的 URL

    setInterval(() => {
        const currentUrl = window.location.href; // 獲取當前的 URL
        const currentAniVideoSn = new URLSearchParams(window.location.search).get('sn'); // 獲取當前的 AniVideoSn

        // 如果 URL 或 AniVideoSn 發生變化
        if (currentUrl !== lastUrl || currentAniVideoSn !== lastAniVideoSn) {
            lastUrl = currentUrl; // 更新 URL
            lastAniVideoSn = currentAniVideoSn; // 更新 AniVideoSn
            AniVideoSn = currentAniVideoSn; // 更新全域變數

            m3u8Container.innerHTML = ''; // 清空按鈕容器
            titleDisplay.textContent = '載入中...'; // 更新提示文字
            getDeviceId(); // 重新獲取設備 ID 並獲取播放列表
        }
    }, 1000); // 每秒檢查一次

    // 開始執行程式
    getDeviceId(); // 獲取設備 ID 並開始流程
    injectStyles(css); // 注入自定義樣式到頁面

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址