// ==UserScript==
// @name 115批量文件迅雷下载(暂不支持文件夹类型下载)
// @namespace sscozak
// @include http://115.com/*
// @version 1.3.1
// @run-at document-end
// @grant none
// @description 迅雷党的福音来啦~\(≧▽≦)/~,免VIP,免115浏览器,对了还能在线播放
// ==/UserScript==
// ======================================= XunLei下载 =============================================
Core.FileAPI.Thunder = {
// 存放URL列表及相关维护操作
thunder_urls : [],
thunder_table : null,
thunderClear : function() {
var len = this.thunder_urls.length;
this.thunder_urls.splice(0,len);
this.thunder_table = null;
},
// 更新窗口URL列表
thunderUpdateTable : function(r) {
if (this.thunder_table) {
var row = this.thunder_table.insertRow();
row.style["white-space"]="nowrap"; // 内容水平展开,不自动换行
var cell = row.insertCell(); cell.align = "left"; cell.innerHTML = r.file_name;
var cell = row.insertCell(); cell.align = "left"; cell.innerHTML = r.file_url;
}
},
thunderPushURL : function(url) {
this.thunder_urls.push(url);
},
// 启动下载
thunderLaunch : function() {
var len = this.thunder_urls.length;
if (0 >= len) {
alert("URL列表为空。。");
return;
}
var urls = this.thunder_urls;
var pid = 66666; // 迅雷合作ID(随意)
BatchTasker.BeginBatch(4,pid); //开始批量添加
for (var i = 0; i < len; i++) {
BatchTasker.AddTask(ThunderEncode(urls[i].file_url), urls[i].file_name); //添加下载任务
}
BatchTasker.EndBatch(pid);
},
// 批量复制链接
thunderCopyToClipboard : function() {
var len = this.thunder_urls.length;
if (0 >= len) {
alert("URL列表为空。。");
return;
}
var urls = this.thunder_urls;
var urls_content = '';
for (var i = 0; i < urls.length; i++) {
urls_content += urls[i].file_url+'\r\n';
}
var url_clipboard = document.getElementById('ID_url_clip');
url_clipboard.innerHTML = urls_content;
// 选取并复制URLs
$(url_clipboard).focus();
$(url_clipboard).select();
document.execCommand('copy', false, "");
},
// 显示下载窗口
thunderShowPage : function() {
var stylePatcher = function(obj, attr_nm, attr_vl) {
for (var i = 0; i < attr_nm.length; i++) {
obj.style[attr_nm[i]] = attr_vl[i];
}
};
// 子窗口
var url_wdw = document.createElement('div'); url_wdw.id = "ID_url_wdw";
url_wdw.className = 'dialog-box dialog-mini easy-download window-current';
stylePatcher(url_wdw,
['z-index', 'width', 'position', 'top', 'left'],
['1000000002', '530px', 'fixed', '56px', '418px']);
document.body.appendChild(url_wdw);
// 构建列表页面
var html = [];
html.push('<div class="dialog-box dialog-mini easy-download window-current" style="z-index: 1000000002; '
+'width: 530px; position: fixed; top: 56px; left: 418px;">');
html.push('<div class="dialog-header" rel="title_box" ws_property="1"><h3 rel="base_title">迅雷批量下载</h3></div>');
html.push('<div class="dialog-handle"><a href="javascript:;" class="close" id="ID_thunder_close">关闭</a></div>');
html.push('<div rel="base_content"><div class="dialog-frame" style="height:240px; overflow:scroll;">'
+'<table id="ID_thunderTable" style="height: auto" border="1" cellspacing="20"></table>');
html.push('</div><div><a href="javascript:;" class="button btn-green" style="width:50%; padding:unset" onclick="Core.FileAPI.Thunder.thunderLaunch()">'
+'<i class="icon ico-normal"></i><em>开始下载</em></a>'
+'<a href="javascript:;" class="button btn-blue" style="width:50%; padding:unset" onclick="Core.FileAPI.Thunder.thunderCopyToClipboard()">'
+'<i class="icon ico-normal"></i><em>复制链接</em></a></div></div></div>');
html.push('<textarea id="ID_url_clip" style="width:0;height:0;border-style:none;"></textarea>');
url_wdw.innerHTML = (function(){
var res = '';
for (var i = 0; i < html.length; i++) {
res += html[i];
}
return res;
})();
// 获取列表Table
this.thunder_table = document.getElementById('ID_thunderTable');
// 背景蒙版
var bk_mask = document.createElement('div'); bk_mask.id = "ID_bk_mask";
stylePatcher(bk_mask,
['z-index', 'background', 'height', 'left', 'position', 'top', 'width', 'opacity'],
['1000000001', 'rgb(0, 0, 0)', '100%', '0px', 'fixed', '0px', '100%', '0.4']);
document.body.appendChild(bk_mask);
var bk_mask_inner = document.createElement('div');
stylePatcher(bk_mask_inner,
['height', 'width'],
['100%', '100%']);
bk_mask.appendChild(bk_mask_inner);
// 设置关闭窗口按钮事件
var btn_close = document.getElementById('ID_thunder_close');
btn_close.onclick = Function('(function(){document.body.removeChild(document.getElementById("ID_bk_mask"));'
+'document.body.removeChild(document.getElementById("ID_url_wdw"))})()');
},
};
// ======================================= 在线点播 =============================================
Core.FileAPI.Player = {
VideoList : [],
VideoTypes : ['mp4','avi','rmvb','3gp','wmv','mp3','flv','mpeg'],
GetFileType : function(file_name) {
return file_name.split('.').pop();
},
CheckVideoType : function(type_name) {
var video_types = this.VideoTypes;
for (var idx = 0; idx < video_types.length; ++idx) {
if (type_name === video_types[idx]) {
return true;
}
}
return false;
},
AddVideoButtonForItem : function() {
const DM = window.frames["wangpan"].document;
var items = DM.querySelectorAll("li[rel='item']");
for (var idx = 0; idx < items.length; ++idx) {
if (this.CheckVideoType(this.GetFileType(items[idx].attributes.title.value))) {
var opdv = items[idx].querySelector("div.file-opr")||items[idx].querySelector("span.file-name");
var aplay = '<a href="javascript:;" menu="play_one" onclick="window.parent.Core.FileAPI.Player.PlayOne(this)">'
+'<i class=""></i>'
+'<span>播放</span>'
+'</a>';
opdv.innerHTML += aplay;
}
}
},
AddVideoButtonForBar : function() {
const DM = window.frames["wangpan"].document;
// 获取当前被选中的文件
var items = DM.querySelectorAll('[rel=\'list\'] [rel=\'item\'].' + "selected");
// 筛选其中可以播放的文件
this.VideoList.splice(0, this.VideoList.length); // 清空上次的信息
for (var idx = 0; idx < items.length; ++idx) {
if (this.CheckVideoType(this.GetFileType(items[idx].attributes.title.value))) {
this.VideoList.push(items[idx]);
}
}
if (0 === this.VideoList.length) {
return;
}
var bar = DM.querySelector("ul");
var aplay = '<li menu="play_all" onclick="window.parent.Core.FileAPI.Player.PlayAll()">'
+'<span>播放全部</span>'
+'</li>';
bar.innerHTML += aplay;
},
// 播放单个文件
PlayOne : function(item_btn_elem) {
var item_info_elem = item_btn_elem.parentElement.parentElement;
var pick_code = item_info_elem.attributes.pick_code.value;
Core.FileAPI.RequestFileURL(pick_code, ((r)=>{
Core.FileAPI.Player.PlayVideo([{title:r.file_name,url:r.file_url}]);
}));
},
// 批量播放
PlayAll : function() {
if (0 === this.VideoList.length) {
return;
}
const video_count = this.VideoList.length;
var remain = video_count;
var play_list = new Array(video_count);
var unplayed = true;
var timer_key = 0;
var delay_time = 5000;
// 设置超时行为
var time_to_play = function() {
if (!unplayed) {
return;
}
unplayed = false;
var ready_list = [];
for (var idx = 0; idx < video_count; ++idx) {
if (play_list[idx]) {
ready_list.push(play_list[idx]);
}
}
Core.FileAPI.Player.PlayVideo(ready_list);
};
// 异步请求播放文件URL
for (var idx = 0; idx < video_count; ++idx) {
const idc = idx; // 保存当前文件任务的ID,必须使用Const类型
Core.FileAPI.RequestFileURL(this.VideoList[idx].attributes.pick_code.value,(
(r)=>{
play_list[idc] = {title:r.file_name,url:r.file_url};
// 检查是否已获取全部url
window.clearTimeout(timer_key); // 取消当前延时
if (!unplayed) {
return;
} else if (--remain === 0) {
unplayed = false;
Core.FileAPI.Player.PlayVideo(play_list);
} else {
// 重设延时器
timer_key = window.setTimeout(time_to_play,delay_time);
}
}));
}
// 设置超时
timer_key = window.setTimeout(time_to_play,delay_time);
},
// 生成ASX文件调用本地PotPlayer
PlayVideo : function(video_infos) {
if (0 === video_infos.length) {
return;
}
console.log("Ready to play: "+video_infos.length);
console.log(video_infos);
var hrefs = '<ASX Version="3.0">';
for (var i = 0; i < video_infos.length; ++i) {
hrefs += '<Entry>';
if (video_infos[i].title) {
hrefs += '<Title>' + video_infos[i].title + '</Title>';
}
hrefs += '<Ref href ="' + video_infos[i].url + '" />';
hrefs += '</Entry>';
}
hrefs += '</ASX>';
var asx_content = [hrefs];
var asx_blob = new Blob(asx_content,{'type':'video/x-ms-asf-plugin'}); // 由关联ASX文件的程序调用
var url = URL.createObjectURL(asx_blob); // 提供blob对象的url地址
location.href = url; // 等效于将url直接输入于地址栏,播放视频
},
};
// ====================================== 工具函数 ==============================================
// 异步请求文件URL
Core.FileAPI.RequestFileURL = function(pick_code, callback) {
var _ = function () {
UA$.ajax({
url: 'files/download?pickcode=' + pick_code,
type: 'GET',
dataType: 'json',
cache: false,
success: function (r) {
callback&&callback(r);
}
})
};
_();
return;
}
// ===================================== 注入 ===============================================
// 替换原函数,直接根据pickcode请求资源URL
Core.FileAPI.Download = function (pick_code, win) {
this.RequestFileURL(pick_code, ((r)=>{
//alert("URL: "+r.file_url);
Core.FileAPI.Thunder.thunderPushURL(r);
Core.FileAPI.Thunder.thunderUpdateTable(r);
}));
};
// 替换原函数,对所有选中的文件(不含文件夹)进行URL获取
Core.FileAPI.DownloadSomeFile = function (list) {
if (!list.length) {
Core.MinMessage.Show({
text: '请选择文件',
type: 'war',
timeout: 2000
});
return;
}
var check_type = function(list) {
for (var i = 0; i < list.length; i++) {
if (list[i].attr('file_type') != '1') {
return false;
}
}
return true;
}
if (check_type(list)) {
Core.FileAPI.Thunder.thunderClear();
Core.FileAPI.Thunder.thunderShowPage();
for (var i = 0; i < list.length; i++) {
Core.FileAPI.Download(list[i].attr('pick_code'));
}
} else {
if ('DownFile' in Core) {
Core.DownFile.Go(list);
return false;
}
}
};
// ======================================= 初始化 =============================================
// 加载迅雷JS库
(function(){
var getScriptSync = function(url) {
$.ajax({
url: url,
async: false,
dataType: "script"
});
};
getScriptSync('http://pstatic.xunlei.com/js/webThunderDetect.js');
getScriptSync('http://pstatic.xunlei.com/js/base64.js');
getScriptSync('http://pstatic.xunlei.com/js/thunderBatch.js');
})();
// 监听iframe载入
(function(){
var sub_wind = document.querySelector("iframe[rel='wangpan']");
sub_wind.onload = (function(){
// 设置观察器
var DM = window.frames["wangpan"].document;
DM.item_bar_observer = new MutationObserver((function(e){Core.FileAPI.Player.AddVideoButtonForBar();}));
DM.item_bar_observer.observe(DM.querySelector('#js_operate_box'),{'childList':true});
DM.item_list_observer = new MutationObserver((function(e){Core.FileAPI.Player.AddVideoButtonForItem();}));
DM.item_list_observer.observe(DM.querySelector('#js_data_list'),{'childList':true});
});
})();