121 lines
4.3 KiB
JavaScript
121 lines
4.3 KiB
JavaScript
// ==UserScript==
|
|
// @name Universal encrypted video downloader
|
|
// @namespace http://tampermonkey.net/
|
|
// @version 0.1
|
|
// @description Bypass the encrypted video download protection via js hook
|
|
// @author Evan
|
|
// @match https://www.bilibili.com/video/*
|
|
// @match https://www.bilibili.com/bangumi/play/*
|
|
// @match https://www.youtube.com/watch*
|
|
// @match https://www.huya.com/*
|
|
// @match https://live.bilibili.com/*
|
|
// @grant none
|
|
// ==/UserScript==
|
|
|
|
evanEnv = new Object();
|
|
evanEnv.mediaSources = [];
|
|
evanEnv.enableAutoForward = true;
|
|
evanEnv.enableAutoDownload = true;
|
|
|
|
(function () {
|
|
evanEnv.videos = document.getElementsByTagName('video');
|
|
evanEnv.downloadRawData = function (bufferList, fileName = 'out', ext = '.mp4') {
|
|
var buffList = new Array();
|
|
var totalLength = 0;
|
|
|
|
for (var i = 0; i < bufferList.length; i++) {
|
|
totalLength += bufferList[i].byteLength;
|
|
if (totalLength > 1073741824 || i == bufferList.length - 1) {
|
|
buffList.push(new Uint8Array(totalLength));
|
|
totalLength = 0;
|
|
}
|
|
}
|
|
|
|
var lastLength = 0;
|
|
var part = 0;
|
|
for (var ix = 0; ix < bufferList.length; ix++) {
|
|
//10485760
|
|
//1073741824
|
|
|
|
buffList[part].set(new Uint8Array(bufferList[ix]), lastLength);
|
|
lastLength += bufferList[ix].byteLength;
|
|
if (lastLength > 1073741824) {
|
|
part++;
|
|
lastLength = 0
|
|
}
|
|
}
|
|
|
|
for (var iz = 0; iz < buffList.length; iz++) {
|
|
var buff = buffList[iz];
|
|
let url = window.URL.createObjectURL(new Blob([buff], { type: "arraybuffer" }))
|
|
const link = document.createElement('a');
|
|
link.style.display = 'none';
|
|
link.href = url;
|
|
link.setAttribute('download', fileName + "_" + iz + ext);
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
}
|
|
|
|
};
|
|
|
|
evanEnv.download = function () {
|
|
for (var i = 0; i < evanEnv.mediaSources.length; i++) {
|
|
var currentItem = evanEnv.mediaSources[i];
|
|
if (currentItem.downloaded == undefined || currentItem.downloaded == false) {
|
|
for (var x = 0; x < currentItem.rawSources.length; x++) {
|
|
evanEnv.downloadRawData(currentItem.rawSources[x], 'mediasource_' + i + '_' + x);
|
|
}
|
|
}
|
|
currentItem.downloaded = true;
|
|
}
|
|
};
|
|
evanEnv.downloadMediaSource = function (mediaSource) {
|
|
for (var x = 0; x < mediaSource.rawSources.length; x++) {
|
|
evanEnv.downloadRawData(mediaSource.rawSources[x], 'mediasource_' + x);
|
|
mediaSource.downloaded = true;
|
|
}
|
|
};
|
|
|
|
var proxy_addSourceBuffer = MediaSource.prototype.addSourceBuffer;
|
|
MediaSource.prototype.addSourceBuffer = function (mimeType) {
|
|
var mediaSourceThis = this;
|
|
|
|
evanEnv.mediaSources.push(this);
|
|
this.lastPos = 0;
|
|
if (evanEnv.enableAutoDownload) {
|
|
this.onsourceended = function () {
|
|
evanEnv.download();
|
|
};
|
|
this.onsourceclose = function () {
|
|
evanEnv.downloadMediaSource(this);
|
|
};
|
|
}
|
|
|
|
|
|
var retSourceBuffer = proxy_addSourceBuffer.apply(this, [].slice.call(arguments));
|
|
if (this.rawSources == undefined) {
|
|
this.rawSources = [];
|
|
}
|
|
var buffList = [];
|
|
this.rawSources.push(buffList);
|
|
retSourceBuffer.rawData = buffList;
|
|
if (evanEnv.enableAutoForward) {
|
|
retSourceBuffer.onupdate = function () {
|
|
if (this.buffered != this.buffered.length > 0) {
|
|
var endPos = this.buffered.end(0);
|
|
if (endPos > 5 && endPos - mediaSourceThis.lastPos > 25) {
|
|
evanEnv.videos[0].currentTime = endPos - 3;
|
|
mediaSourceThis.lastPos = evanEnv.videos[0].currentTime;
|
|
}
|
|
}
|
|
};
|
|
}
|
|
return retSourceBuffer;
|
|
};
|
|
var proxy_appendBuffer = SourceBuffer.prototype.appendBuffer;
|
|
SourceBuffer.prototype.appendBuffer = function (buf) {
|
|
this.rawData.push(buf);
|
|
return proxy_appendBuffer.apply(this, [].slice.call(arguments));
|
|
};
|
|
})(); |