获取视频m3u8片段【弃用】

用于慢速获取当前播放视频m3u8片段并合并下载

// ==UserScript==
// @name         获取视频m3u8片段【弃用】
// @author       JIEMO
// @description   用于慢速获取当前播放视频m3u8片段并合并下载
// @version      0.11
// @namespace https://greasyfork.org/users/158417
// @match        *://www.iqiyi.com/*
// ==/UserScript==

(function () {

  let _sourceBufferList = []
  let $btnDownload = document.createElement('div')
  let $downloadNum = document.createElement('div')
  let $tenRate = document.createElement('div') // 10倍速播放

  // 十倍速播放
  function _tenRatePlay () {
    let $domList = document.getElementsByTagName('video')
    for (let i = 0, length = $domList.length; i < length; i++) {
      const $dom = $domList[i]
      $dom.playbackRate = 10
    }
  }

  // 下载资源
  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')
      a.download = `${document.title}.${type}`
      a.href = URL.createObjectURL(fileBlob)
      a.style.display = 'none'
      document.body.appendChild(a)
      a.click()
      a.remove()
    })
  }

  // 监听资源全部录取成功
  let _endOfStream = window.MediaSource.prototype.endOfStream
  window.MediaSource.prototype.endOfStream = function () {
    alert('资源全部捕获成功,即将下载!')
    _download()
    _endOfStream.call(this)
  }

  // 录取资源
  let _addSourceBuffer = window.MediaSource.prototype.addSourceBuffer
  window.MediaSource.prototype.addSourceBuffer = function (mime) {
    console.log(mime)
    let sourceBuffer = _addSourceBuffer.call(this, mime)
    let _append = sourceBuffer.appendBuffer
    let bufferList = []
    _sourceBufferList.push({
      mime,
      bufferList,
    })
    sourceBuffer.appendBuffer = function (buffer) {
      $downloadNum.innerHTML = `已捕获 ${_sourceBufferList[0].bufferList.length}`
      bufferList.push(buffer)
      _append.call(this, buffer)
    }
    return sourceBuffer
  }

  // 添加操作的 dom
  function _appendDom () {
    const baseStyle = `
position:fixed;
top:210px;
left:5px;
z-index:999999999;
width:100px;
height:30px;
line-height:30px;
text-align:center;
font-size:16px;
font-family:Verdana, Arial, '楷体';
font-weight:500;
color:orange;
user-select:none;
padding:0px;
white-space:nowrap;
border-radius:3px;
border:2px solid red;cursor:pointer;
    `
    $tenRate.innerHTML = '速度*10'
    $downloadNum.innerHTML = '获取进度:'
    $btnDownload.innerHTML = '下载'
    $tenRate.style = baseStyle + `top: 150px;`
    $btnDownload.style = baseStyle + `top: 100px;`
    $downloadNum.style = `
position:fixed;
top:126px;
left:5px;
z-index:999999999;
width:100px;
height:30px;
line-height:30px;
text-align:center;
font-size:16px;
font-family:Verdana, Arial, '楷体';
font-weight:500;
color:orange;
user-select:none;
padding:0px;
white-space:nowrap;

    `
    $btnDownload.addEventListener('click', _download)
    $tenRate.addEventListener('click', _tenRatePlay)
    document.getElementsByTagName('html')[0].insertBefore($downloadNum, document.getElementsByTagName('head')[0]);
    document.getElementsByTagName('html')[0].insertBefore($btnDownload, document.getElementsByTagName('head')[0]);
    document.getElementsByTagName('html')[0].insertBefore($tenRate, document.getElementsByTagName('head')[0]);


  }

  _appendDom()
})()