脚本

// ==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 @   水车  阅读(72)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2015-11-08 JavaScript 的同源策略
点击右上角即可分享
微信分享提示
主题色彩