// ==UserScript==
// @name 视频嗅探
// @namespace http://tampermonkey.net/
// @version 0.8
// @description 自动嗅探页面上的视频资源,将链接展示出来,并提供复制和下载方法(除部分加密的m3u8不能下载,解密方法目前还没空研究)。
// @author geigei717
// @license MIT
// @match https://*/*
// @match http://*/*
// @icon https://greasyfork.s3.us-east-2.amazonaws.com/fc67t00gsk98w7pbhs97xr94g1hl
// @require https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js
// @require https://unpkg.com/@ffmpeg/ffmpeg@0.10.0/dist/ffmpeg.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/layui/2.8.11/layui.min.js
// @require https://cdn.bootcdn.net/ajax/libs/flv.js/1.6.2/flv.min.js
// @require https://cdn.bootcdn.net/ajax/libs/shaka-player/4.3.5/shaka-player.compiled.min.js
// @require https://cdn.bootcdn.net/ajax/libs/dashjs/4.6.0/dash.all.min.js
// @require https://cdn.bootcdn.net/ajax/libs/hls.js/1.4.0/hls.min.js
// @require https://cdn.bootcdn.net/ajax/libs/dplayer/1.27.1/DPlayer.min.js
// @noframes
// @grant unsafeWindow
// @grant GM_download
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @connect *
// @downloadURL none
// ==/UserScript==
(function() {
$("head").append(' ')
$.getScript("https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js");
var info = ['✅ 已启用', '❌ 已禁用']
mkMenu("窗口自动打开:","auto_n")
mkMenu("是否转码(需满足先决条件):","ffmpeg_n")
GM_registerMenuCommand('支持作者,你的支持就是作者的动力', function() {me()})
FirstOpen()
let URLs = [],videos=[];
unsafeWindow.GM_D = [];
var mn = -1
$("body").append("
" +
"
     视频链接:" + //部分m3u8等流媒体无法下载,需要专用下载器" +
"
" +
"
" +
"
")
.append("")
.append("" +
"" +
"" +
"" +
"
")
$("#Alldownload").click(function (){
$('.MyUrls .isUrl').each(function(){
$(this).find("[class^=SaveUrl]").click()
})
})
$("#Allcopy").click(function (){
var urls ="";
$('.MyUrls .isUrl').each(function(){
urls = urls + $(this).find("[class^=downUrl]").attr('title')+ "\n"
})
GM_setClipboard(urls);
})
$("#Alldel").click(function (){
$('.MyUrls .isUrl').remove()
URLs = []
GM_D.forEach(function(item){
item.forEach(function(i){
i.abort()
})
})
})
$(".MyUrls").append("")
//")
$("#MyUpDown").click(function () {
if (mn=="1"||mn==1){
$("#Allurl").css("display","none")
$("#MyUpDown #downIcon").attr('class','layui-icon layui-icon-add-circle')
}else {
$("#MyUpDown #downIcon").attr('class','layui-icon layui-icon-reduce-circle')
$("#redPoint").css("display","none")
$("#Allurl").css("display","block")
}
mn = -mn;
$("#MyUrls").slideToggle("slow");
})
$("body>[id!=MyUpDown][id!=MyUrls][id!=Allurl]").click(function () {
if($("#MyUrls").css("display")!="none"){
$("#MyUpDown").click()
}
})
functionAll("")
function functionAll(u){
//跳转链接
$(".GoUrl"+u).click(function (){
var url = $(this).prevAll(".downUrl"+u).attr("title")
var link = document.createElement('a');
link.href = url;
link.target="_blank";
link.click();
link.remove();
})
//复制链接
$(".CopyUrl"+u).click(function (){
var url = $(this).prevAll(".downUrl"+u).attr("title")
GM_setClipboard(url);
$(this).text("已复制")
})
//播放链接
$(".playUrl"+u).click(function (){
var url = $(this).prevAll(".downUrl"+u).attr("title")
dplayerUrl(url)
})
//删除此条
$(".rmUrl"+u).click(function (){
var num = $(this).prevAll('.StopSaveUrl'+u).data('num')
if(num != undefined){
GM_D[num].forEach(function(item){
item.abort()
})
}
$(this).parent(".isUrl").remove()
})
//下载链接
$(".SaveUrl"+u).click(function (){
//$(obj).attr("disabled","disabled")
var that = $(this)
var url = $(that).prevAll(".downUrl"+u).val()
if(url==undefined||url.trim()==''){
url = $(that).prevAll(".downUrl"+u).attr('title')
if(url==undefined||url.trim()==''){return;}
}
var name = $(that).prevAll(".downName"+u).val()
if(name==undefined||name.trim()==""){
name = $('title').text()
if(name==undefined||name.trim()==""){
name = url.split("/").pop().split("?")[0]
if(name==undefined||name.trim()==""){
name = "video.mp4"
}
}
}
//if(! /\.[\w]+$/.test(name)&& ! /(\.com$ | \.cn)/.test(name)){ name = name + ".mp4"}
name = name.replaceAll(/\s+/ig," ").trim().replace(/(\.mp4)*$/igm,"")+".mp4"
//console.log(name,url)
$(that).css("display","none").next('.StopSaveUrl'+u).css("display","inline-block").text("解析中");
var request = [];
var blob = [];
var loadSize = [];
var xhrs = 0
var num = -1
GM_xmlhttpRequest({
method: "HEAD",
fetch: true,
url: url,
onerror: function(x) {
$(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
},
onload: function(response) {
if( response.status /100 >=4){
$(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
return;
}
var Length =response.responseHeaders.match(/content-length:\s*[\d]+\s/im)
if( Length==null||Length==undefined||Length=="" ){
if( !/\.m3u8(\?[^'"\s]*)+|(\.m3u8$)/img.test(url) ){
console.log("mp4视频下载中ing(直接下)。")
request.push(GM_download({
url: url,
name: name,
onprogress : function (event) {
if (event.lengthComputable) {
var loaded = parseInt(event.loaded / event.total * 100);
if(loaded==100){
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none");
}else{
$(that).next(".StopSaveUrl"+u).text(loaded+"%");
}
}
},
onload : function () {
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").text("0%");
},
onerror : function () {
$(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
}
}))
num =GM_D.push(request)
$(that).next('.StopSaveUrl'+u).data('num',num-1);
return;
}else{
Length = 0
}
}else{
Length = parseInt(Length[0].match(/\d+/)[0]);
}
var Type =response.responseHeaders.match(/content-type:\s*[\S]+\s/im)[0].split(':')[1].trim().toLowerCase();
if( (Type.split('/')[0] != "video" && !/\.mp4(\?[^'"\s]*)+|(\.mp4$)/img.test(url)) && ( /\.m3u8(\?[^'"\s]*)+|(\.m3u8$)/img.test(url) || Type =="Application/vnd.apple.mpegurl" || Length <= 1024*100) ){
GM_xmlhttpRequest({
method: "GET",
url: url,
onerror: function(x) {
$(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
},
onload: function(response) {
var err = 0
var tsNum=0
var tsS = 0
function downTs(Ts,tsUrl){
request[0] = GM_xmlhttpRequest({
method: "GET",
url: tsUrl,
fetch: false,
responseType: "arraybuffer",
onloadstart: function(){
num = $(that).next(".StopSaveUrl"+u).data("num")
if (num == undefined ||num == -1 || num == ""){
num =GM_D.push(request)
tsS = Ts.length + 1
$(that).next('.StopSaveUrl'+u).data('num',num-1)
}else{
num = parseInt( num );
GM_D[num] = request;
}
},
onload: function(response) {
blob.push( new Blob([response.response]) )
if (Ts.length>0) {
tsNum = parseInt(tsNum) + 1 / tsS * 100;
tsNum = tsNum >100 ? 100 : parseInt(tsNum).toFixed(1);
$(that).next(".StopSaveUrl"+u).text(tsNum+"%");
downTs(Ts,Ts.shift())
}else{
var is = true;
try {
var sab = new SharedArrayBuffer(1);
} catch(err) {
console.log( err.message +"\n 浏览器不支持SharedArrayBuffer")
is = false
}
var link = document.createElement("a");
if(GM_getValue("ffmpeg_n", 1) == 0 && is){
$(that).next(".StopSaveUrl"+u).text("转码中");
const { createFFmpeg, fetchFile } = FFmpeg;
const ffmpeg = createFFmpeg({
//corePath: 'https://unpkg.com/@ffmpeg/core@0.10.0/dist/ffmpeg-core.js',
log: true,
progress: ({ ratio }) => {
tsNum = (ratio * 100.0).toFixed(2)
$(that).next(".StopSaveUrl"+u).text(tsNum+"%").attr("title",'转码中');
},
});
(async () => {
console.log( '正在加载 ffmpeg-core.js');
await ffmpeg.load();
console.log('开始转码');
ffmpeg.FS('writeFile', 'video.ts', await fetchFile(new Blob(blob)) );
await ffmpeg.run('-i', 'video.ts' ,'output.mp4');
console.log('转码完成');
const data = ffmpeg.FS('readFile', 'output.mp4');
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").attr("title","下载中");
link.href = window.URL.createObjectURL(new Blob([data.buffer]));
link.download = name;
link.click();
link.remove();
ffmpeg.exit()
})();
}else{
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").attr("title","下载中");
link.href = window.URL.createObjectURL(new Blob(blob));
link.download = name;
link.click();
link.remove();
}
}
},
onabort: function(){
console.log("abort!");
},
onerror: function(x) {
console.log("error!");
if (err<10){
err = err+1
downTs(Ts,tsUrl)
}else{
err = 0
$(that).text("错误").css("display","inline-block").attr("title","下载出错").next(".StopSaveUrl"+u).css("display","none").text("0%");
}
}
});
}
var Ts = response.responseText.trim().split("\n")
if(Ts[0].trim()=="#EXTM3U"){
//console.log("m3u8")
var status = "",bool ="false";
Ts.forEach(function(item){
if(/#EXT-X-KEY/.test(item.trim())){
status = "key"
return;
}
if(/#EXT-X-TARGETDURATION/.test(item.trim())){
bool = "true"
return;
}
})
if(status == "key"){
$(that).text("错误").css("display","inline-block").attr("title","m3u8加密,暂时无法解决。").next(".StopSaveUrl"+u).css("display","none").text("0%");
console.log("m3u8加密,暂时无法解决。")
return;
}
console.log("m3u8解析下载中ing。")
//console.log(Ts)
var tsUrl
Ts.forEach(function(item,i){
if(!/^#EXT/.test(item.trim())){
if(/^(http:|https:)/.test(item)){
//"完整链接"
tsUrl = item
}else{
//"不完整,需要拼接"
tsUrl = url.split("?")[0].split("/");
tsUrl.pop();
tsUrl = tsUrl.join("/")+"/"+item
}
Ts[i] = tsUrl
//console.log(tsUrl)
}
})
if(bool == "true"){
Ts = Ts.filter(function(item){
return !/^#EXT/.test(item.trim())
});
downTs(Ts,Ts.shift())
}else{
console.log("这下边嵌套了m3u8。")
Ts = Ts.join("\n").split("#EXT-X-STREAM-INF:"); Ts.shift()
var maxP=0,maxUrl='';
Ts.forEach(function(item,i){
tsUrl = item.split("\n",2)
if( /RESOLUTION=\d+\D\d+/igm.test( tsUrl[0] )){
var P=1;
tsUrl[0].match(/RESOLUTION=\d+\D\d+/igm)[0].match(/\d+\D\d+/igm)[0].split(/\D/).forEach(function(item,i){
P = P * parseInt(item)
})
if( maxP < P ){
maxUrl = tsUrl[1] ;
maxP = P
}
}else{
maxUrl = tsUrl[1]
return;
}
})
console.log(maxP,maxUrl)
//name = name.replace(/(\.mp4)*$/igm,"")+"_"+m3u8Url.split("?")[0].split("/").pop().replace(/(\.m3u8)*$/igm,"")+".mp4"
GM_xmlhttpRequest({
method: "GET",
url: maxUrl,
onerror: function(x) {
$(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
},
onload: function(response) {
var Ts1 = response.responseText.trim().split("\n")
if(Ts1[0].trim()=="#EXTM3U"){
//console.log("m3u8")
var status = "";
Ts1.forEach(function(item){
if(/#EXT-X-KEY/.test(item.trim())){
status = "key"
return;
}
})
if(status == "key"){
$(that).text("错误").css("display","inline-block").attr("title","m3u8加密,暂时无法解决。").next(".StopSaveUrl"+u).css("display","none").text("0%");
console.log("m3u8加密,暂时无法解决。")
return;
}
Ts1 = Ts1.filter(function(item){
return !/^#EXT/.test(item.trim())
});
var tsUrl
Ts1.forEach(function(item,i){
if(/^(http:|https:)/.test(item)){
//"完整链接"
tsUrl = item
}else{
//"不完整,需要拼接"
tsUrl = maxUrl.split("?")[0].split("/");
tsUrl.pop();
tsUrl = tsUrl.join("/")+"/"+item
}
Ts1[i] = tsUrl
//console.log(tsUrl)
})
downTs(Ts1,Ts1.shift())
}else{
var link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.response]));
link.download = name;
link.click();
link.remove();
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").text("0%");
}
}
})
}
}else{
var link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.response]));
link.download = name;
link.click();
link.remove();
$(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").text("0%");
}
}})
}else{
//console.log("video")
console.log("mp4视频下载中ing。")
var RangeSize = parseInt((Length/5).toFixed(0))
for(var i=0,z=0;i=Length) {range_end = Length}
//console.log(range_start,range_end)
eval('function onprogress'+z+' (event){'+
'loadSize['+z+'] = event.loaded;'+
'var x =0;y=0;'+
'loadSize.forEach(function(item){'+
' x = x + item'+
'});'+
'var loaded = (parseInt(x / Length * 1000)/10).toFixed(1);'+
'if(loaded==100){'+
' $(that).text("下载").css("display","inline-block").next(".StopSaveUrl'+u+'").css("display","none");'+
'}else{'+
' $(that).next(".StopSaveUrl'+u+'").text(loaded+"%"); '+
//' console.log(loaded+"%")'+
'}'+
'}'+
'request['+z+'] = GM_xmlhttpRequest({'+
'method: "GET",'+
'url: url,'+
'fetch: false,'+
'responseType: "arraybuffer",'+
'headers: { "Range":"bytes="+range_start+"-"+range_end},'+
'onprogress: onprogress'+ z +','+
'onload: function(response) {'+
' blob[' + z + '] = new Blob([response.response]);' +
//' console.log(blob);' +
' var x=0;' +
' loadSize.forEach(function(item){' +
' x = x + item' +
' });' +
' blob.forEach(function(item){'+
' y = y +item.size'+
' });'+
' console.log(y+" "+ x+" "+Length);'+
' if (x == Length && y ==Length) {' +
' var link = document.createElement("a");' +
' link.href = window.URL.createObjectURL(new Blob(blob));' +
' link.download = name;' +
' link.click();' +
' link.remove();' +
' $(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl'+u+'").css("display","none").text("0%");'+
' }' +
'},'+
'onabort: function(){'+
//'$(that).text("继续").css("display","inline-block").next(".StopSaveUrl1").css("display","none").text("0%");'+
'console.log("error!");'+
'},'+
'onerror: function(x) {'+
//'$(that).text("错误").css("display","inline-block").next(".StopSaveUrl'+u+'").css("display","none").text("0%");'+
'console.log("error!更换线路ing");'+
'request.forEach(function(item){'+
' item.abort()'+
'});'+
'request['+z+'] = GM_download({'+
' url: url,'+
' name: name,'+
' onprogress : function (event) {'+
' if (event.lengthComputable) {'+
' var loaded = parseInt(event.loaded / event.total * 100);'+
' if(loaded==100){'+
' $(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl'+u+'").css("display","none");'+
' }else{'+
' $(that).next(".StopSaveUrl'+u+'").text(loaded+"%"); '+
' }'+
' }'+
' },'+
' onload : function () {'+
' $(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl'+u+'").css("display","none").text("0%");'+
' },'+
' onerror : function () {'+
' $(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl'+u+'").css("display","none").text("0%");'+
' }'+
'});'+
'var numi = parseInt( $(that).next(".StopSaveUrl'+u+'").data("num") );'+
'GM_D[numi] = request;'+
'},'+
'});')
}
//console.log(request)
num =GM_D.push(request)
$(that).next('.StopSaveUrl'+u).data('num',num-1)
}
}
});
})
//停止
$(".StopSaveUrl"+u).click(function (){
var num = $(this).data("num")
GM_D[num].forEach(function(item){
item.abort()
})
$(this).data("num","").css("display","none").text("0%").prev(".SaveUrl"+u).text("继续").attr("title","下载中断").css("display","inline-block");
})
}
function getNetworkRequsts(){
return performance.getEntriesByType("resource").filter((entry) => {
return entry.initiatorType === "xmlhttprequest"||entry.initiatorType === "video"||entry.initiatorType === "iframe";
});
}
var observer = new PerformanceObserver(perf_observer);
observer.observe({entryTypes: ["resource"]})
function perf_observer(list,observer){
var z,m,length= 0;
length = $('.MyUrls .isUrl').length
var scripts =getNetworkRequsts()
//console.log(scripts)
scripts.forEach(function (x,i) {
if (x.initiatorType === "xmlhttprequest" || (x.initiatorType === "video" && !/\.jpg/.test(x.name))) {
z = x.name.trim()
if(/m3u8/.test(x.name) && !/\.ts/.test(x.name) || /\.mp4$/.test(z.split('?')[0]) ){
//console.log("m3u8")
//console.log(x)
if(! URLs.includes(z.split("?")[0] ) ){
m = URLs.push(z.split("?")[0])-1
//console.log('type: m3u8 , url: ' + x.name)
$(".urlnone").remove()
$(".MyUrls").append(" "+ (m+1) +"、
"+z+"
访问
复制
下载
0%
播放
  
x
")
// "+z+"
")
functionAll(m)
}
}
}
if (x.initiatorType == "iframe"){
z = x.name.trim()
//console.log("iframe")
//console.log(x)
//return
GM_xmlhttpRequest({
method: "GET",
fetch: false,
url: z,
onload: function(response) {
//alert(response.responseText)
var body =response.responseText.match(/\s]*(\.mp4|\.m3u8)+\?+[^"'<>\s]*)|((http:|https:)?\/{2}[^"'<>\s]*(\.mp4|\.m3u8)+(?![^'"\s]))/img)
//console.log(video)
if(video !=null && video !=undefined && video !=""){
video.forEach(function (str,x){
z = str.trim();
//console.log(z)
if(z !=undefined&& z!= ''){
if(!/^(http:|https:)/.test(z)){
z = location.href.split("://")[0] +':'+ z.trim()
}
if( !URLs.includes(z.trim().split("?")[0]) && URLs.indexOf(z.trim().split("?")[0])==-1){
m =URLs.push(z.trim().split("?")[0])-1
//console.log('type: src , url: ' + z)
$(".urlnone").remove()
$(".MyUrls").append(" "+ (m+1) +"、
"+z+"
访问
复制
下载
0%
播放
  
x
")
// $(".MyUrls").append(""+ (m+1) +"、
"+z+"
")
functionAll(m)
}
}
})
}
}
}
})
}
//console.log(URLs)
})
$("video").each(function () {
//console.log("video")
if(!/^blob:/.test($(this).attr('src')) ){
z = $(this).attr('src')
if(z !=undefined && z.trim()!="" && ! URLs.includes(z.trim().split("?")[0])){
m =URLs.push(z.trim().split("?")[0])-1
//console.log('type: video , url: ' + $(this).attr('src'))
$(".urlnone").remove()
$(".MyUrls").append(" "+ (m+1) +"、
"+z+"
访问
复制
下载
0%
播放
  
x
")
// $(".MyUrls").append(""+ (m+1) +"、
"+z+"
")
functionAll(m)
}
}
})
$("source").each(function () {
//console.log("source")
if($(this).attr('src')!=undefined && $(this).attr('src').trim()!=''){
if(!/^(http:|https:)/.test($(this).attr('src'))){
z = location.href.split("://")[0] +':'+ $(this).attr('src')
}else{
z = $(this).attr('src')
}
if(! URLs.includes(z.trim().split("?")[0])){
m =URLs.push(z.trim().split("?")[0])-1
//console.log('type: src , url: ' + z)
$(".urlnone").remove()
$(".MyUrls").append(" "+ (m+1) +"、
"+z+"
访问
复制
下载
0%
播放
  
x
")
// $(".MyUrls").append(""+ (m+1) +"、
"+z+"
")
functionAll(m)
}
}
})
if($('.MyUrls .isUrl').length > length){
if($("#MyUrls").css("display")=="none"){
$("#redPoint").css("display","block")
if(GM_getValue("auto_n", 1)==0){
$("#MyUpDown").click()
}
}
}
}
function mkMenu(str,keyName){
var key = GM_getValue(keyName, 1)
var id = GM_registerMenuCommand(str+info[key], function() {
GM_setValue(keyName, key==1? 0:1);
rmMenu(id)
mkMenu(str,keyName)
});
}
function rmMenu(id){
GM_unregisterMenuCommand(id);
}
function FirstOpen(){
var one = GM_getValue("first", 1)
if(one==1){
me();
GM_setValue("first",99);
};
}
function me(){
layer.open({
formType: 0,
title: "支持作者,你的支持就是作者的动力!",
move: false, //禁止拖动
shadeClose: true, //点击遮罩关闭
offset: '100px', // 垂直位置
resize: false,
btn: ['不再弹出(需要时可在油猴扩展脚本列表的菜单中找到)'],
area: ['500px', '600px'],
content: ""
,success: function(layero, index){
$('layui-layer-btn .layui-layer-btn0').css({'border-color': '#1e9fff !important','background-color': '#1e9fff !important','color': '#fff !important'})
}
})
}
function dplayerUrl(url){
$("head").append(' ')
layer.open({
formType: 0,
title: url,
move: false, //禁止拖动
shadeClose: true, //点击遮罩关闭
offset: '100px', // 垂直位置
resize: false,
btn: [],
area: ['800px', '600px'],
content: ""
})
var dp = new DPlayer({
element: document.getElementById("dplayerUrl"),
preload: 'auto', //视频预加载
hotkey: true, //键盘热键
volume: 1, //默认音量
mutex: true, //互斥 同时只有一个播放器能播放
loop: false, //循环播放
screenshot: true,
autoplay: true,
video: {
url: url,
type: 'auto',
},
});
}
})();