// ==UserScript==
// @name 天翼云盘助手
// @description 天翼云盘跳过客户端直接下载文件,支持2021年6月份改版后的天翼云盘。
// @version 2.0.2
// @namespace TyCloud_Tools
// @icon https://z3.ax1x.com/2021/05/31/2m1BTA.png
// @author 邓小明
// @include *//cloud.189.cn/*
// @require https://cdn.bootcdn.net/ajax/libs/jquery/2.1.4/jquery.min.js
// @run-at document-start
// @grant unsafeWindow
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
$(document).ready(function () {
init();
});
})();
function init() {
console.log('初始化网盘助手' + unsafeWindow.location.pathname);
var append_html = `
`;
$('body').append(append_html);
$('.show_file_list').hide();
$('.show_file_list_btn').on('click', function () {
$('.show_file_list_btn').fadeOut();
$('.show_file_list').fadeIn();
});
$('.show_file_list .td-dialog__close').on('click', function () {
$('.show_file_list_btn').fadeIn();
$('.show_file_list').fadeOut();
$('#file_direct_link').empty();
});
}
var share_id = '';
var request_proxied = unsafeWindow.XMLHttpRequest.prototype.open;
unsafeWindow.XMLHttpRequest.prototype.open = function () {
this.addEventListener('load', function () {
if (this.status == 200 && this.response) {
var url = this.responseURL;
if (url.indexOf('getUserInfoForPortal.action') > -1) {
//console.log('用户信息', this.response);
} else if (url.indexOf('file/listFiles.action') > -1) {
//console.log('文件列表', this.response);
if (this.response.res_code == 0) {
show_file_list(this.response.fileListAO);
}
} else if (url.indexOf('share/listShareDir.action') > -1) {
//console.log('分享列表', json);
if (this.response.res_code == 0) {
show_file_list(this.response.fileListAO);
}
} else if (url.indexOf('share/getShareInfoByCode.action') > -1) {
if(this.response.shareId){
share_id = this.response.shareId;
}
} else if (url.indexOf('getFileDownloadUrl.action') > -1) {
//console.log('文件下载', this.response);
} else { }
}
});
return request_proxied.apply(this, [].slice.call(arguments));
};
function show_file_list(data) {
$('.folder_list').empty();
if (data.count > 0 || data.fileListSize > 0) {
$('.show_file_list_btn').fadeIn();
} else {
$('.show_file_list_btn').fadeOut();
}
$.each(data.folderList, function (index, item) {
$('.show_file_list .folder_list').append('' + item.name + '');
});
$.each(data.fileList, function (index, item) {
$('.show_file_list .folder_list').append('' + item.name + ' ' + conver(item.size) + '');
});
$('.show_folder').off('click').on('click', function (e) {
e.preventDefault();
unsafeWindow.location.href = '/web/main/file/folder/' + $(this).data('id');
});
$('.show_file').off('click').on('click', function (e) {
e.preventDefault();
$('#file_direct_link').empty();
var that = $(this);
$.ajax({
url: '/api/open/file/getFileDownloadUrl.action',
data: {
fileId: $(this).data('id'),
dt: 1,
shareId: share_id
},
dataType: 'xml',
success: function (data) {
var fileName = that.find('span').text();
var fileDownloadUrl = $(data).find('fileDownloadUrl').text() + '&fileName=' + encodeURIComponent(fileName);
$('#file_direct_link').html('直链网址:' + fileDownloadUrl + '');
}
});
});
}
function conver(limit) {
var size = '';
if (limit < 0.1 * 1024) {
size = limit.toFixed(2) + 'B';
} else if (limit < 0.1 * 1024 * 1024) {
size = (limit / 1024).toFixed(2) + 'KB';
} else if (limit < 0.1 * 1024 * 1024 * 1024) {
size = (limit / (1024 * 1024)).toFixed(2) + 'MB';
} else {
size = (limit / (1024 * 1024 * 1024)).toFixed(2) + 'GB';
}
var sizestr = size + '';
var len = sizestr.indexOf('\.');
var dec = sizestr.substr(len + 1, 2);
if (dec == '00') {
return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
}
return sizestr;
}