// ==UserScript==
// @name 天翼云盘-下载不求人
// @namespace http://tampermonkey.net/
// @version 0.8.1
// @description 让下载成为一件愉快的事情
// @author You
// @match https://cloud.189.cn/web/*
// @connect 189.cn
// @icon https://cloud.189.cn/web/logo.ico
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @require https://cdn.staticfile.org/blueimp-md5/2.19.0/js/md5.min.js
// @grant GM_xmlhttpRequest
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
var $ = $ || window.$;
var obj = {};
obj.showTipSuccess = function (text, time) {
obj.showNotify({
type: "success",
text: text
});
};
obj.showTipError = function (text, time) {
obj.showNotify({
type: "error",
text: text
});
};
obj.showTipLoading = function (text, time) {
obj.showNotify({
type: "loading",
text: text
});
};
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() {
var $Vue = (document.querySelector(".content") || document.querySelector(".p-web")).__vue__;
$Vue.$toast.hide();
$Vue.$loading.hide();
};
obj.getFinalUrl = function (url) {
return new Promise(function (resolve) {
const xhr = GM_xmlhttpRequest({
url: url,
method: "get",
onreadystatechange: function(response) {
if (response.readyState === 4 || response.finalUrl !== url) {
xhr.abort();
if (!xhr.mark) {
xhr.mark = true;
resolve(response.finalUrl);
}
}
},
onerror: function (error) {
resolve("");
}
});
});
};
obj.getAccessToken = function () {
var accessToken = localStorage.getItem("accessToken");
if (accessToken) return Promise.resolve(accessToken);
return obj.getFinalUrl("https://api.cloud.189.cn/open/oauth2/ssoH5.action").then(function (location) {
if (location) {
var accessToken = (/accessToken=(.+)/.exec(location) || [])[1];
accessToken && localStorage.setItem("accessToken", accessToken);
return accessToken;
}
else {
return "";
}
});
};
obj.getSignature = function (e) {
for (var t = 1; t < arguments.length; t++) {
var n = arguments[t];
for (var r in n) {
Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r])
}
}
var i = []
for (var s in e){
i.push(s + "=" + e[s]);
}
i.sort(function(e, t) {
return e > t ? 1 : e < t ? -1 : 0
})
e = i.join("&");
return window.md5(e);
};
obj.getFileDownloadUrl = function (fileId, shareId) {
var accessToken = localStorage.getItem("accessToken").replace(/[\"\\]/g, "")
, timestamp = Date.now()
, data = Object.assign({
AccessToken: accessToken,
Timestamp: timestamp,
fileId: fileId
}, shareId ? {dt: 1, shareId: shareId} : {})
, signature = obj.getSignature(data);
return new Promise(function (resolve) {
$.ajax({
url: "https://api.cloud.189.cn/open/file/getFileDownloadUrl.action?fileId=" + fileId + (shareId ? "&dt=1&shareId=" + shareId : ""),
headers: {
Accept: "application/json;charset=UTF-8",
AccessToken: accessToken,
Signature: signature,
"Sign-Type": 1,
Timestamp: timestamp
},
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("网络错误,刷新重试");
localStorage.removeItem("accessToken");
resolve("");
}
});
});
};
obj.getDownloadUrl = function (fileId, shareId) {
if (localStorage.getItem("accessToken")) {
return obj.getFileDownloadUrl(fileId, shareId);
}
else {
return Promise.resolve("");
}
};
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__;
if (Object.keys($Vue.fileDetail).length) {
return [$Vue.fileDetail];
}
else {
return [];
}
}
else {
return [];
}
};
obj.showBox = function (body) {
var html = '显示链接
' + (++index) + ':' + (item.fileName ? item.fileName : item.fileId) + ' || 请进入文件夹下载
'; html += ''; retCount++; } else { if (item.downloadUrl) { html += '' + (++index) + ':' + (item.fileName ? item.fileName : item.fileId) + '
'; html += ''; retCount++; } else { obj.getDownloadUrl(item.fileId, item.shareId).then(function (downloadUrl) { item.downloadUrl = downloadUrl; html += '' + (++index) + ':' + (item.fileName ? item.fileName : item.fileId) + '
'; html += ''; retCount++; }); } } }); var waitId = setInterval(function(){ if (retCount == fileList.length){ html += '