// ==UserScript==
// @name 柒灵全能视频下载器
// @namespace https://weibo.com/guoxuebiji/profile?is_all=1
// @version 1.7.3
// @description (目前仅支持)哔哩哔哩(bilibili)视频批量下载
// @author 东风
// @date 2020-4-25
// @modified 2020-10-26
// @match http*://*.bilibili.com/video/*
// @match http*://*.bilibili.com/bangumi/play/*
// @match http*://*.bilibili.com/*/favlist*
// @icon https://space.bilibili.com/favicon.ico
// @license BSD 3-Clause License
// @grant unsafeWindow
// @grant GM_setClipboard
// @grant GM_info
// @grant GM_download
// @grant GM_registerMenuCommand
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @grant GM_openInTab
// @grant GM.openInTab
// @grant GM_getValue
// @grant GM.getValue
// @grant GM_setValue
// @grant GM.setValue
// ==/UserScript==
(function () {
'use strict';
//==========utils=====================================================================
//加载css文件
function addCSS(href) {
var link = document.createElement('link');
link.type = 'text/css';
link.rel = 'stylesheet';
link.href = href;
document.getElementsByTagName("head")[0].appendChild(link);
}
//加载js文件
function addJS(src, cb) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
script.onload = typeof cb === "function" ? cb : function () {};
}
//由于@require方式引入jquery时layer使用异常,故引用cdn中jquery v1.10.1;加载完成后引用又拍云中layer v3.1.1
addJS("https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js", function () {
$.noConflict();
// addJS("https://static.saintic.com/cdn/layer/3.1.1/layer.js");
addJS("https://www.layuicdn.com/layui-v2.5.6/layui.js");
addCSS("https://www.layuicdn.com/layui-v2.5.5/css/layui.css");
});
function GetFileName(url) {
var Business=url.split("/");
return Business[Business.length-1];
}
// String.prototype.TextFilter=function(){
// var pattern=new RegExp("[`~%!@#^=''?~!@#¥……&——‘”“'?*()(),,。.、]"); //[]内输入你要过滤的字符
// var rs="";
// for(var i=0;i<this.length;i++){
// rs+=this.substr(i,1).replace(pattern,'');
// }
// return rs;
// }
// 去掉空格
function Trim(str)
{
// str = str.TextFilter()
var result = str.replace(/\s/g,"");
result = result.replace(/\//g,"");
result = result.replace(/\\/g,"");
return result;
}
// 去掉标题后缀
function FormatTitle(str)
{
var title = Trim(document.title)
var n = title.lastIndexOf(str);
if (n >= 0) {
title = title.substring(0,n);
}
return title;
}
function ShowTips(str) {
layui.use('layer', function(){
var layer = layui.layer;
layer.msg(str, {
icon: 1
});
});
}
//==========以下是与下载器通讯=====================================================================
var host = '127.0.0.1'
,port = '5678';
function wsmessage(evt)
{
console.log(evt);
var received_msg = evt.data;
console.log("收到服务器的信息",received_msg);
// ws.send(JSON.stringify(["https://www.bilibili.com/video/BV19E411D78Q?p=6","https://www.bilibili.com/video/BV19E411D78Q?p=3"]));
// console.log(JSON.parse(received_msg));
// 发送成功{id: 2333333, jsonrpc: "2.0", result: "a6ff40d33524229a"}
// 开始下载{jsonrpc: "2.0", method: "aria2.onDownloadStart", params: [[gid: "a6ff40d33524229a"]]}
// 下载完成{jsonrpc: "2.0", method: "aria2.onDownloadComplete", params: [[gid: "a6ff40d33524229a"]]}
// 下载出错{jsonrpc: "2.0", method: "aria2.onDownloadError", params: [[gid: "a6ff40d33524229a"]]}
ShowTips("任务发送成功");
};
function wsclose()
{
console.log("连接关闭");
// layui.use('layer', function(){
// var layer = layui.layer;
// layer.msg("连接关闭", {
// icon: 1
// });
// });
};
function Download(url, out, dir)
{
// var json = MakeSendData(url, out, dir)
var ws = new WebSocket("ws://" + host + ":" + port + "/jsonrpc");
function wsopen()
{
console.log("连接下载服务器");
// var json = MakeSendData("http://aria2c.com/usage.html", "test.html", "./dow")
// ws.send(JSON.stringify(["https://www.bilibili.com/video/BV19E411D78Q?p=5","https://www.bilibili.com/video/BV19E411D78Q?p=3"]));
ws.send(JSON.stringify(url));
setTimeout(function() {
ws.close()
}, 10000)
};
setTimeout(function() {
if (ws.readyState===1) {
//连接成功什么事情都不用处理
}else{
ShowTips("连接下载器失败,请确认开启下载器");
}
}, 1000)
ws.onopen = wsopen;
ws.onmessage = wsmessage;
ws.onclose = wsclose;
};
//===============================================================================
// 拷贝我的收藏视频网址
function CopyFavlistUrls() {
var title = "我的收藏"
var x = document.getElementsByClassName("fav-video-list clearfix content")[0].children;
console.log(title)
var list = [];
console.log(x)
for (var i = 0; i < x.length; i++) {
console.log(i,x[i].attributes["class"].nodeValue)
if (x[i].attributes["class"].nodeValue != "small-item disabled") {
list[i] = x[i];
}
}
console.log(list)
// zoomfile
if (list) {
GM_setClipboard( list.map(function (pin) {
return "https:" + pin.children[0].attributes["href"].nodeValue + "\r\n";
}).join(""));
return list.map(function (pin) {
var item = {"url": "https:" + pin.children[0].attributes["href"].nodeValue};
item.folder = "我的收藏"
item.islist = false;
return item;
})
}else
{
ShowTips("找不到视频");
}
}
// 拷贝播放列表视频网址
function CopyVedioUrls() {
var title = Trim(document.querySelector('meta[property="og:title"]').getAttribute('content'));//FormatTitle("_哔哩哔哩(゜-゜)つロ干杯~-bilibili")
var list_box = document.getElementsByClassName("list-box")
if (list_box.length == 0) {
return [{"url": window.location.href,"islist": false}]
}
var x = document.getElementsByClassName("list-box")[0].children;
console.log(title)
var list = [];
console.log(x)
for (var i = 0; i < x.length; i++) {
list[i] = x[i];
}
console.log(list)
// zoomfile
if (list) {
GM_setClipboard( list.map(function (pin) {
return "https://www.bilibili.com" + pin.children[0].attributes["href"].nodeValue + "\r\n";
}).join(""));
return list.map(function (pin) {
var item = {"url": "https://www.bilibili.com" + pin.children[0].attributes["href"].nodeValue}
item.folder = Trim(title)
item.islist = false;
return item;
})
}else
{
ShowTips("找不到视频");
}
}
// 拷贝番剧播放列表视频网址
function CopyBangumiUrls() {
jQuery.ajax({
url: window.location.href,
async: false,
success: function (res) {
// console.log(res);
var info = res
var n = info.lastIndexOf("<script>window.__INITIAL_STATE__");
if (n >= 0) {
info = info.substring(n+"<script>window.__INITIAL_STATE__=".length);
var n2 = info.indexOf(";(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());</script>");
// console.log("1111",n, n2,info)
info = info.substring(0,n2);
}
var bili_state = JSON.parse(info)
if (bili_state) {
console.log(bili_state)
var epList = bili_state.epList
var title = bili_state.mediaInfo.title
var ssType = bili_state.ssType
// console.log(bili_state.epList)
// console.log("epList =", epList, "mediaInfo = ", mediaInfo)
// for (var i = 0; i < epList.length; i++) {
// console.log(i, epList[i])
// }
// if (ssType==1) { //番剧
var urls = epList.map(function (ep) {
var item = {"url": "https://www.bilibili.com/bangumi/play/ep"+ ep.id}
item.folder = Trim(title)
item.islist = false;
return item;
})
Download(urls)
// }else
// {// 电影
// DownloadSelf(title, false)
// }
}else
{
var title = document.querySelector('meta[property="og:title"]').getAttribute('content');
DownloadSelf(title, true)
}
},
omplete: function (data) {
if (data.status === 200) {
}
else {
ShowTips("系统错误:暂时无法连接服务器")
var title = document.querySelector('meta[property="og:title"]').getAttribute('content');
DownloadSelf(title, true)
}
}
});
}
// function CopyBangumiUrlsEx() {
// var title = document.querySelector('meta[property="og:title"]').getAttribute('content');//FormatTitle("_番剧_bilibili_哔哩哔哩")
// console.log(title)
// //进度
// var progress = document.getElementsByClassName("ep-list-progress");
// // console.log(progress[0])
// var arrprogress = progress[0].innerText.split('/')
// var index = parseInt(arrprogress[0])-1;
// var total = parseInt(arrprogress[1]);
// console.log("当前第"+index+"集")
// var arrUrl = window.location.pathname.split('/');
// // console.log("arrUrl = ", arrUrl);
// var preVideoID = arrUrl[3].substring(0,2)
// var videoID = parseInt(arrUrl[3].substring(2))-index
// console.log(preVideoID , "videoID = ", videoID)
// var list = [];
// // console.log(x)
// for (var i = 0; i < total; i++) {
// list[i] = videoID + i;
// }
// console.log(list)
// // zoomfile
// if (list) {
// GM_setClipboard( list.map(function (pin) {
// return "https://www.bilibili.com/bangumi/play/"+ preVideoID + pin + "\r\n";
// }).join(""));
// var item = {"url": window.location.href}
// item.folder = title;
// item.islist = true;
// return [item]
// }else
// {
// ShowTips("找不到视频");
// }
// }
function DownloadSelf(title, islist) {
var item = {"url": window.location.href}
item.folder = Trim(title); // 不能有空格,有时候下载失败是因为有空格
item.islist = islist;
Download([item])
}
function ParseUrl() {
console.log("onclick-----------------")
var arrUrl = window.location.pathname.split('/');
console.log("arrUrl = ", arrUrl);
if(!arrUrl[1]) return;
if (arrUrl[1] == "video")
return CopyVedioUrls();
else if (arrUrl[1] == "bangumi")
return CopyBangumiUrls();
else if (arrUrl[2] == "favlist")
return CopyFavlistUrls();
else
{
var item = {"url": window.location.href,"islist": false}
// item.folder = title;
return [item]
}
}
/*
主入口,分出不同模块:用户、画板,监听并刷新URL
*/
window.onload =function() {
function GMaddStyleString(css) {
var myStyle = document.createElement('style');
myStyle.textContent = css;
var doc = document.head || document.documentElement;
doc.appendChild(myStyle);
}
// var int=self.setInterval(function (){
// console.log("Interval---------------");
// var user_text = "拷贝下载链接";
// var tmpHtml = '<button type="button" class="CopyPicUrls" style=" height:30px;width:100px;color:white;border-radius: 4px;background-color: #008CBA; font-size: 16px; text-align:center" id="CopyPicUrls" >拷贝下载链接</button>'
// var btn = document.getElementsByClassName("CopyPicUrls")
// // console.log(btn,"-======")
// if (btn.length > 0) {
// btn.innerHTML = tmpHtml
// return
// }
// var gelement = document.getElementsByClassName("nav-search-box")
// // console.log(gelement,"----------", gelement.length, gelement[0]);
// var pab = document.getElementsByClassName("nav-search-box")[0];
// // console.log(pab,"----------222");
// // console.log(pab);
// pab.insertAdjacentHTML('afterEnd', tmpHtml);
// //监听用户点击下载事件
// document.getElementById("CopyPicUrls").onclick = function () {
// ParseUrl();
// };
var btn = document.getElementsByClassName("btnDownload")
if (btn.length > 0) {
// btn.innerHTML = tmpHtml
return
}
GMaddStyleString(`#download_movie_box {cursor:pointer; position:fixed; top:` + 60 + `px; left:` + 0 + `px; width:0px; background-color:#2E9AFE; z-index:2147483647; font-size:20px; text-align:left;}
#download_movie_box .item_text {width:28px; padding:4px 0px; text-align:center;}
#download_movie_box .item_text img {width:35px; height:35px; display:inline-block; vertical-align:middle;}
`);
// var $ = $ || window.$;
var ImgBase64Data = "data:image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jAyMC0wOS0zMFQwMToyMDo0NiswMDowMJJ4Q5MAAAAgdEVYdHNvZnR3YXJlAGh0dHBzOi8vaW1hZ2VtYWdpY2sub3JnvM8dnQAAABh0RVh0VGh1bWI6OkRvY3VtZW50OjpQYWdlcwAxp/+7LwAAABh0RVh0VGh1bWI6OkltYWdlOjpIZWlnaHQANTY2x6EBXwAAABd0RVh0VGh1bWI6OkltYWdlOjpXaWR0aAA1ODe91EwaAAAAGXRFWHRUaHVtYjo6TWltZXR5cGUAaW1hZ2UvcG5nP7JWTgAAABd0RVh0VGh1bWI6Ok1UaW1lADE2MDE0Mjg4NDYDgXzHAAAAEnRFWHRUaHVtYjo6U2l6ZQA0MDU0NELIVd5gAAAAWnRFWHRUaHVtYjo6VVJJAGZpbGU6Ly8vZGF0YS93d3dyb290L3d3dy5lYXN5aWNvbi5uZXQvY2RuLWltZy5lYXN5aWNvbi5jbi9maWxlcy8xMjkvMTI5MTI2Mi5wbmc00KRyAAAAAElFTkSuQmCC"
var html =
`<div id='download_movie_box' class="btnDownload">
<div class='item_text'>
<img src='`+ ImgBase64Data +`' title='下载视频' id="downloadVideos"/>
</div>
</div>`;
document.body.insertAdjacentHTML('afterEnd', html);
document.getElementById("downloadVideos").onclick = function () {
var urls = ParseUrl();
console.log("urls = ",urls)
if (urls) {
Download(urls)
}
};
// },3000)
}
})();