// ==UserScript==
// @name 天翼云盘-下载不求人
// @namespace http://tampermonkey.net/
// @version 0.5.1
// @description 让下载成为一件愉快的事情
// @author You
// @match https://cloud.189.cn/web/*
// @icon https://cloud.189.cn/web/logo.ico
// @require https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js
// @run-at document-start
// @grant none
// ==/UserScript==
(function() {
'use strict';
var $ = $ || window.$;
var obj = {
file_page: {
folderId: -11,
sessionKey: "",
shareId: "",
}
};
obj.showTipSuccess = function (text, time) {
obj.showNotify({
type: "success",
text: text,
time: time || 3000
});
};
obj.showTipError = function (text, time) {
obj.showNotify({
type: "error",
text: text,
time: time || 3000
});
};
obj.showTipLoading = function (text, time) {
obj.showNotify({
type: "loading",
text: text,
time: time || 3000
});
};
obj.showNotify = function (opts) {
var $Vue = (document.querySelector(".content") || document.querySelector(".p-web")).__vue__;
if (opts.type == "loading") {
$Vue.$loading.show(opts);
}
else {
$Vue.$toast.show(opts);
}
};
obj.hideNotify = function() {
if (window.IsPC) {
var $Vue = (document.querySelector(".content") || document.querySelector(".p-web")).__vue__;
$Vue.$toast.hide();
$Vue.$loading.hide();
}
else if (window.app) {
window.app.$toast.hide();
window.app.$loading.hide();
}
};
obj.getFileSize = function(e, t, n, i) {
if (!(e && e.toString().search(/B|K|M|G|T/) > -1)) {
var o, a, r, s = parseFloat(e), l = Math.abs(s);
void 0 === t && (t = 2);
void 0 === n && (n = !0);
l < 1024 ? (t = 0, o = s, a = "B") :
l < 921600 ? (o = s / 1024, a = "K") :
l < 943718400 ? (o = s / 1048576, a = "M") :
l < 966367641600 || 0 === t && l < 1099511627776 ? (o = s / 1073741824, a = "G") :
(o = s / 1099511627776, a = "T");
o = (Math.round(o * Math.pow(10, t)) / parseFloat(Math.pow(10, t))).toFixed(t);
r = i && t > 0 ? o !== Math.floor(o) ? o : parseInt(Math.floor(o), 10) : o;
n && (r += a);
return r;
}
else {
return e;
}
};
obj.getDownloadUrl = function (fileId, shareId) {
return new Promise(function (resolve) {
$.ajax({
url: "https://cloud.189.cn/api/open/file/getFileDownloadUrl.action" + "?noCache".concat(Math.random(), "&fileId=").concat(fileId, "&dt=").concat(1, "&shareId=").concat(shareId || ""),
headers: {
accept: "application/json;charset=UTF-8"
},
async: true,
success: function (t) {
if (0 === t.res_code) {
resolve(t.fileDownloadUrl);
}
else if ("InfoSecurityErrorCode" === t.res_code) {
obj.showTipError("文件内容违规,下载失败");
resolve("");
}
else {
obj.showTipError("下载失败,网络错误,刷新重试");
resolve("");
}
},
error: function () {
obj.showTipError("网络错误,刷新重试");
resolve("");
}
});
});
};
obj.buildPackageUrl = function (folderId, shareId) {
var sessionKey = obj.file_page.sessionKey || sessionStorage.getItem("sessionKey");
if (sessionKey) {
return "https://cloud.189.cn/downloadMultiFiles.action?sessionKey=" + sessionKey + "&fileIdS=" + folderId + "&downloadType=" + (shareId ? 3 : 1) + (shareId ? "&shareId=" + shareId : "");
}
else {
return ""
}
};
obj.getSelectedFileList = function () {
var $Vue;
if (document.querySelector(".c-file-list")) {
$Vue = document.querySelector(".c-file-list").__vue__;
if ($Vue.selectLength > 0) {
return $Vue.selectedList;
}
else {
return $Vue.fileList;
}
}
else if (document.querySelector(".info-detail")) {
$Vue = document.querySelector(".info-detail").__vue__;
return [$Vue.fileDetail] || [];
}
else {
return [];
}
};
obj.showBox = function (body) {
var template = '
文件下载×
';
if ($(".c-share").length == 0) {
$("body").append('
');
}
$(".c-share").append(template);
$(".c-share .share-detail").append(body);
$(".c-share").show();
$(".c-share .share-content-head-close").off("click").on("click", function () {
$(".c-share").hide();
$(".c-share .share-content").remove();
});
};
obj.showDownload = function () {
var login_user = window._ux21cn.cookie.get("COOKIE_LOGIN_USER");
if (!login_user || login_user.length == 16) {
obj.showTipError("无法显示链接,请登录后重试");
return;
}
var fileList = obj.getSelectedFileList();
if (fileList.length == 0) {
obj.showTipError("getSelectedFileList 获取选中文件出错");
return;
}
obj.showTipLoading("正在获取链接...");
var html = '
';
var rowStyle = "margin:10px 0px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;";
var shareId = obj.file_page.shareId;
if (fileList.length > 1 && obj.file_page.folderId > 0) {
var downloadUrl = obj.buildPackageUrl(obj.file_page.folderId, shareId);
html += '
压缩包
';
html += '
' + downloadUrl + '
';
html += '
';
}
var retCount = 0;
fileList.forEach(function (item, index) {
if (item.isFolder) {
item.downloadUrl = item.downloadUrl || obj.buildPackageUrl(item.fileId, shareId);
html += '
' + (++index) + ':' + (item.name ? item.name : item.fileId) + ' || 文件夹
';
html += '
' + item.downloadUrl + '
';
retCount++;
}
else {
if (item.downloadUrl) {
html += '
' + (++index) + ':' + (item.fileName ? item.fileName : item.fileId) + ' || ' + obj.getFileSize(item.fileSize) + '
';
html += '
' + item.downloadUrl + '
';
retCount++;
}
else {
obj.getDownloadUrl(item.fileId, shareId).then(function (downloadUrl) {
item.downloadUrl = downloadUrl;
html += '
' + (++index) + ':' + (item.fileName ? item.fileName : item.fileId) + ' || ' + obj.getFileSize(item.fileSize) + '
';
html += '
' + item.downloadUrl + '
';
retCount++;
});
}
}
});
var waitId = setInterval(function(){
if (retCount == fileList.length){
html += '
';
html += '
提示:如链接获取失败请尝试刷新一次本页面
';
obj.showBox(html);
obj.hideNotify();
clearInterval(waitId);
}
}, 500);
};
obj.initDownloadPage = function () {
if ($(".btn-show-link").length) {
return;
}
if ($(".file-operate").length) {
$(".file-operate").append('
显示链接');
$(".btn").css({"margin-left": "5px", "margin-right": "5px"});
$(".tips-save-box").css("display", "none");
$(".btn-show-link").on("click", obj.showDownload);
}
else if ($(".FileHead_file-head-left_3AuQ6").length) {
$(".FileHead_file-head-left_3AuQ6").append('
显示链接
');
$(".btn-show-link").on("click", obj.showDownload);
}
};
obj.initPageFileInfo = function () {
var open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener("load", function() {
if (! (this.readyState == 4 && this.status == 200)) {
return;
}
var responseURL = this.responseURL;
var response = this.response;
if (responseURL.indexOf("/getUserBriefInfo.action") > 0) {
if (response.sessionKey) {
obj.file_page.sessionKey = response.sessionKey;
}
}
if (response instanceof Object && response.res_code == 0) {
if (responseURL.indexOf("/checkAccessCode.action") > 0) {
if (response.shareId) {
obj.file_page.shareId = response.shareId;
}
}
else if (responseURL.indexOf("/listShareDir.action") > 0 || responseURL.indexOf("/listFiles.action") > 0) {
obj.file_page.folderId = (/shareDirFileId=(\d+)/.exec(responseURL) || /folderId=(\d+)/.exec(responseURL) || [])[1] || -11;
if (response.fileListAO) {
obj.initDownloadPage();
obj.showTipSuccess("文件加载完成 共:" + (response.fileListAO.count || (response.fileListAO.fileList || []).length) + "项");
}
}
}
}, false);
open.apply(this, arguments);
};
}();
// Your code here...
})();