加载中...

脚本

// ==UserScript==
// @name         shuiche_m3u8dl
// @version      0.2
// @author       Momo707577045
// @match        *://*.aura.cn/*
// @include      *
// @grant        none
// @run-at document-start
// ==/UserScript==

// 学习
// https://developer.mozilla.org/zh-CN/docs/Web/API/MediaSource/endOfStream  MediaSource对象

(function () {
    'use strict'

    if (document.getElementById('shuiche_m3u8dl')) {
        return
    }

    let _sourceBufferList = [] // 缓存视频、音频数据
    let fileName = '未命名'
    let isDl = false
    let dlNum = localStorage.getItem('shuicheDlNum') ? localStorage.getItem('shuicheDlNum') : 1
    let speed = localStorage.getItem('shuicheSpeed') ? localStorage.getItem('shuicheSpeed') : 2

    function answerTimer () {
        setTimeout(function () {
            console.log('shuiche 定时器执行了')
            if (document.querySelector('input[type="radio"]')) {
                document.querySelector('input[type="radio"]').setAttribute('checked', 'checked')
                setTimeout(function () {
                    document.querySelector('button[class="confirm_btn"]').click()
                    setTimeout(function () {
                        document.querySelector('button[class="confirm_btn"]').click()
                    }, 1000)
                }, 1000)
            }
            answerTimer()
        }, 60000)
    }
    // 添加操作的 dom
    function _appendDom () {
        if (document.getElementById('shuiche_m3u8dl')) {
            return
        }
        let style = document.createElement('style')
        style.innerHTML = `.shuiche-m3u8dl-warp {padding:10px; position: fixed;top: 0px;left: 0px;z-index: 999999999;background: rgb(204,204,204);}
        .shuiche-m3u8dl-item{line-height: 30px;text-align: center; cursor: pointer;margin-top: 10px;border: 1px solid #ffffff}`
        document.getElementsByTagName('head').item(0).appendChild(style)

        let divWarp = document.createElement('div')
        divWarp.className = 'shuiche-m3u8dl-warp'
        divWarp.id = 'shuiche_m3u8dl'

        divWarp.innerHTML = `<div class="shuiche-m3u8dl-item" id="shuiche_m3u8dl-source_number">已经捕捉到 0 个片段</div>
    <div class="shuiche-m3u8dl-item" id="shuiche_m3u8dl-download">下载已捕捉音视频</div>
    <input class="shuiche-m3u8dl-item" id="shuicheSpeedInput" value="${speed}">
    <div class="shuiche-m3u8dl-item" id="shuiche_m3u8dl-tenRate">设定倍速</div>`
        document.getElementsByTagName('body').item(0).appendChild(divWarp)

        document.getElementById('shuiche_m3u8dl-download').addEventListener('click', _download)
        document.getElementById('shuiche_m3u8dl-tenRate').addEventListener('click', setSpeed)
    }

    // 倍速播放
    function _RatePlay () {
        let $domList = document.getElementsByTagName('video')
        for (let i = 0, length = $domList.length; i < length; i++) {
            const $dom = $domList[i]
            $dom.playbackRate = speed
        }
    }
    // 设定倍速
    function setSpeed () {
        let inputVal = document.getElementById('shuicheSpeedInput').value
        if (inputVal === '') {
            alart('未填写速度,请填写合适播放速度')
            return
        }
        let spd = parseInt(inputVal)
        if (spd <= 0) {
            alart('请填写合适播放速度')
            return
        }
        speed = spd
        localStorage.setItem('shuicheSpeed', spd)
        _RatePlay()
    }

    // 下载资源
    function _download () {
        _sourceBufferList.forEach((target) => {
            const mime = target.mime.split(';')[0]
            const type = mime.split('/')[1]
            const fileBlob = new Blob(target.bufferList, { type: mime }) // 创建一个Blob对象,并设置文件的 MIME 类型
            const a = document.createElement('a')
            if (mime.indexOf('audio') >= 0) {
                a.download = `${dlNum}_${fileName}.mp3`
            } else {
                a.download = `${dlNum}_${fileName}.${type}`
            }
            a.href = URL.createObjectURL(fileBlob)
            a.style.display = 'none'
            document.body.appendChild(a)
            a.click()
            a.remove()
        })
        _sourceBufferList = [] // 这里新增的
    }

    // 监听资源全部录取成功
    let _endOfStream = window.MediaSource.prototype.endOfStream
    window.MediaSource.prototype.endOfStream = function () {
        if (!isDl) {
            _download()
            localStorage.setItem('shuicheDlNum', (parseInt(dlNum) + 1))
            isDl = true
        }

        _endOfStream.call(this)
    }

    // 录取资源
    let _addSourceBuffer = window.MediaSource.prototype.addSourceBuffer
    window.MediaSource.prototype.addSourceBuffer = function (mime) {
        _appendDom()
        _RatePlay()
        answerTimer()
        fileName = document.querySelector('div[style="color: red"]').innerHTML
        let sourceBuffer = _addSourceBuffer.call(this, mime)
        let _append = sourceBuffer.appendBuffer
        let bufferList = []
        _sourceBufferList.push({
            mime,
            bufferList
        })
        sourceBuffer.appendBuffer = function (buffer) {
            document.getElementById('shuiche_m3u8dl-source_number').innerHTML = `已捕获 ${_sourceBufferList[0].bufferList.length} 个片段`
            bufferList.push(buffer)
            _append.call(this, buffer)
        }
        return sourceBuffer
    }
})()

posted @ 2021-11-08 16:39  水车  阅读(71)  评论(0编辑  收藏  举报