// ==UserScript==
// @name 天翼云盘助手
// @description 天翼云盘跳过客户端直接下载文件,支持文件夹打包下载。
// @version 1.0.9
// @namespace TyCloud_Tools
// @icon https://z3.ax1x.com/2021/05/31/2m1BTA.png
// @author 邓小明
// @include *//cloud.189.cn/t/*
// @include *//cloud.189.cn/main.*
// @include *//cloud.189.cn/photo.*
// @grant unsafeWindow
// ==/UserScript==
(function() {
'use strict';
addLoadEvent(init);
})();
function init() {
var append_html = `
<style type="text/css">
.btn-show-link{ background-color: #2b89ea; border-color:#0073E3; color: #fff; cursor: pointer; }
#J_FileModal::after { content:""; width:100vw; height:100vh; background-color:rgba(0,0,0,0.65); position:fixed; z-index:1; top:0px; left:0px; backdrop-filter: blur(5px); }
#J_FileModal .modal-dialog{ position:relative; z-index:2; }
#J_FileModal .modal-dialog .modal-header .close{ cursor:pointer; }
#J_FileModal .modal-body{ margin-top:15px; border-radius:3px; padding: 15px 10px; height: 360px; overflow-y:auto; }
#J_FileModal .modal-body h2{ font-size:20px; line-height:2em; font-weight:bold; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; }
#J_FileModal .modal-body ul{ }
#J_FileModal .modal-body li{ padding:0 5px; font-size:14px; line-height:40px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; border-bottom:1px dashed #efefef; }
#J_FileModal .modal-body li:hover{ background-color: #f8f8f8; }
#J_FileModal .modal-body a{ color:#2b89ea; display:inline-block; width:100%; }
#J_FileModal .modal-body a i{ color:#666; font-size:12px; font-style:normal; }
</style>
`;
$('body').append(append_html);
var url = unsafeWindow.location.href;
if (url.indexOf('cloud.189.cn/main') > 0 || url.indexOf('cloud.189.cn/photo') > 0) {
/* 个人云盘 */
if ($('#J_Create').length) {
$('#J_Create').after('<a class="btn btn-show-link">显示链接</a>');
$('.btn-show-link').on('click', mainShowDownload);
} else if ($('.JC_Refresh').length) {
$('.JC_Refresh').after('<a class="btn btn-show-link">显示链接</a>');
$('.btn-show-link').on('click', photoShowDownload);
} else {}
} else if (url.indexOf('cloud.189.cn/t') > 0) {
/* 分享页 */
$('.breadcrumb').append('<li style="margin-top:-5px; margin-left:20px; padding:5px 10px; background-color:#f75a54; color:#fff; border-radius:3px;">勾选文件显示下载链接</li>');
if ('undefined' != typeof _shareId) {
if ($('#J_SaveAs').length) {
$('#J_SaveAs').after('<a class="btn btn-show-link">显示链接</a>');
$('.btn-show-link').on('click', shareShowDownload);
} else if ($('.JC_Refresh').length) {
$('.JC_Refresh').after('<a class="btn btn-show-link">显示链接</a>');
} else {}
} else {
/* 恢复单文件下载按钮 */
for (var t = document.getElementsByTagName('a'), e = 0; e < t.length; e++) {
'btn btn-download download-link disable' === t[e].getAttribute('class') && t[e].setAttribute('class', 'btn btn-download download-link');
}
}
} else {}
}
function getSelectedFileList() {
var mainView = null,
fileList = [];
if (unsafeWindow.fileId) {
fileList = [{
attributes: unsafeWindow
}];
} else if (unsafeWindow._shareId) {
mainView = unsafeWindow.appRouter.mainView;
if (mainView instanceof Object && mainView.fileList) {
fileList = mainView.fileList;
if (fileList.selected().length) {
fileList = fileList.selected();
}
}
//obj.processFileList(fileList, unsafeWindow._shareId);
} else if (unsafeWindow.mainView) {
mainView = unsafeWindow.mainView;
if (mainView.fileListTabObj && mainView.fileListTabObj[mainView.options.fileId]) {
fileList = mainView.fileListTabObj[mainView.options.fileId].fileList.selected();
} else if (mainView.getSelectedModels) {
fileList = mainView.getSelectedModels();
}
}
var selectedFileList = [];
fileList.forEach(function(item) {
if (item.attributes.fileId > 0) {
selectedFileList.push(item);
}
});
return selectedFileList;
}
function getMetadata() {
var url = unsafeWindow.location.href;
var match = url.match(/cloud\.189\.cn\/t\/([0-9a-z]+)/i);
var shortCode = match ? match[1] : null;
var result;
$.ajax({
type: 'get',
url: '//cloud.189.cn/v2/listShareDirByShareIdAndFileId.action',
data: {
shortCode: shortCode,
accessCode: getCookie('shareId_' + _shareId),
verifyCode: _verifyCode,
orderBy: 1,
order: 'ASC',
pageNum: 1,
pageSize: 60
},
dateType: 'json',
async: false,
success: function(data) {
result = data;
},
error: function() {}
});
return result;
}
/* 文件夹打包下载链接 */
function buildFolderDownUrl(fileId) {
var sessionKey = getCookie('validCodeTimestamp');
var result = 'https://cloud.189.cn/downloadMultiFiles.action?sessionKey=' + sessionKey + '&fileIdS=' + fileId + '&downloadType=3&shareId=' + _shareId;
return result;
}
/* 文件下载链接 */
function getFileDownUrl(fileId, subFileId) {
var result = '';
var s_url = unsafeWindow.location.href;
var match = s_url.match(/cloud\.189\.cn\/t\/([0-9a-z]+)/i);
var shortCode = match ? match[1] : null;
$.ajax({
type: 'get',
url: '//cloud.189.cn/v2/getFileDownloadUrl.action',
data: {
shortCode: shortCode,
fileId: subFileId,
accessCode: getCookie('shareId_' + _shareId),
subFileId: fileId,
noCache: 1
},
dateType: 'text',
async: false,
success: function(data) {
result = data;
},
error: function(data) {
console.error(data);
}
});
return result;
}
function shareShowDownload() {
if(getCookie('COOKIE_LOGIN_USER') == ''){
application.showNotify({type:'error', message:'登录后才能显示'});
return false;
}
var sels = getSelectedFileList();
showDownloadDialog();
var html = '';
var tmp = getMetadata();
if (tmp.path) {
html += '<h2>压缩包:</h2><ul>';
html += '<li title="' + tmp.path[0].fileName + '_打包下载.zip"><a href="' + buildFolderDownUrl(tmp.path[0].fileId) + '&fileName=' + tmp.path[0].fileName + '_打包下载.zip">' + tmp.path[0].fileName + '_打包下载.zip</a></li>';
html += '</ul>';
html += '<h2>文件列表:</h2>';
}
html += '<ul>';
sels.forEach(function(item, index) {
var file = item.attributes;
if (file.isFolder) {
var f_downloadUrl = buildFolderDownUrl(file.fileId) + '&fileName=' + (file.fileName ? file.fileName : file.fileId) + '_打包下载.zip';
html += '<li title="' + file.fileName + '"><a href="' + f_downloadUrl + '">' + (file.fileName ? file.fileName : file.fileId) + '_打包下载</a></li>';
} else {
var s_downloadUrl = "https:" + getFileDownUrl(file.fileId, file.parentId) + '&fileName=' + file.fileName;
html += '<li title="' + file.fileName + '"><a href="' + s_downloadUrl + '">' + (file.fileName ? file.fileName : file.fileId) + ' <i>' + file.fileSize + '</i></a></li>';
}
});
html += '</ul>';
$('#J_FileModal .modal-body').html(html);
$('#J_FileModal').show();
}
function mainShowDownload() {
if(unsafeWindow.edrive.userId == ''){
application.showNotify({type:'error', message:'登录后才能显示'});
$('#J_FileModal').hide();
return false;
}
showDownloadDialog();
var file_id = (unsafeWindow.location.hash && unsafeWindow.location.hash != '#home' ? unsafeWindow.location.hash.replace('#home/folder/', '') : '-11');
console.log('file_id', file_id);
var file_list = JSON.parse(unsafeWindow.localStorage.getItem('home-' + file_id));
if(file_list == null){
application.showNotify({type:'error', message:'获取失败'});
$('#J_FileModal').hide();
return false;
}
var html = '<h2 title="' + file_list.path[file_list.path.length-1].fileName + '">' + file_list.path[file_list.path.length-1].fileName + '</h2>';
html += '<ul>';
file_list.data.forEach(function(item, index) {
if (item.isFolder) {
html += '<li title="点击进入文件夹:' + item.fileName + '"><a href="#home/folder/' + item.fileId + '" class="folder_btn">' + item.fileName + '</a></li>';
} else {
html += '<li title="点击下载文件:' + item.fileName + '"><a href="' + item.downloadUrl + '" target="_blank">' + item.fileName + '</a></li>';
}
});
html += '</ul>';
$('#J_FileModal .modal-body').html(html);
$('#J_FileModal .modal-body .folder_btn').off('click').on('click', function(e){
e.preventDefault();
application.showNotify({type:'error', message:'请进入文件夹后再获取'});
setTimeout('window.location.hash = "' + $(this).attr('href') + '"', 1000);
$('#J_FileModal').hide();
});
$('#J_FileModal').show();
}
function photoShowDownload() {
application.showNotify({type:'error', message:'功能待完善'});
}
function showDownloadDialog() {
var template = '<div id="J_FileModal" class="treeBox-modal modal in" style="display:block">';
template += '<div class="modal-dialog"><div class="modal-header"><a class="close">×</a><h3>文件下载</h3></div>';
template += '<h3>点击蓝色的链接或右键另存为即可下载,文件夹打包下载必须点击跳转进行下载</h3>';
template += '<div class="modal-body"></div></div></div>';
if ($('#J_FileModal').length == 0) {
$('body').append(template);
$('#J_FileModal .close').on('click', function(e) {
$('#J_FileModal').hide();
});
}
}
function addLoadEvent(func) {
var old_onload = unsafeWindow.onload;
if (typeof unsafeWindow.onload != 'function') {
unsafeWindow.onload = func;
} else {
old_onload();
func();
}
}
function getCookie(name) {
var strcookie = document.cookie;
var arrcookie = strcookie.split('; ');
for (var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split('=');
if (arr[0] == name) {
return arr[1];
}
}
return '';
}