// ==UserScript== // @name 天翼云盘助手 // @description 天翼云盘大文件免客户端直接下载文件,支持2021年6月份改版后的天翼云盘。 // @version 2.0.9 // @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 = `

下载直链

X
    `; $('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 { share_id = ''; } } 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) { $('.show_file_list .folder_list').empty(); if (data.count > 0 || data.fileListSize > 0) { $('.show_file_list_btn').fadeIn(); $.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) + '
  • '); var file_list_item = $('.file-list-ul .c-file-list-item[data-fileid=' + item.id + ']'); /* 点击文件名下载 */ file_list_item.find('.file-item-name-fileName-span').attr('title', '点击下载').off('click').on('click', function (e) { var file_id = $(this).parents('.c-file-list-item').data('fileid'); get_file_download_url(file_id, share_id, 0); }); if(item.size >= 50*1024*1024){ /* 替换打开客户端下载按钮 */ var data_v = 'data-v-' + (/data-v-(\w+)/.exec(file_list_item.html())[1]); file_list_item.find('.file-item-ope-item-download').parent().remove(); file_list_item.find('.file-item-ope .file-item-ope-item').eq(0).after('
    '); file_list_item.find('.file-item-ope-item-download').parent().attr('title', '直链下载').off('click').on('click', function (e) { var file_id = $(this).parents('.c-file-list-item').data('fileid'); get_file_download_url(file_id, share_id, 0); }); } }); $('.show_file_list .show_folder').off('click').on('click', function (e) { $('.file-list-ul .c-file-list-item[data-fileid=' + $(this).data('id') + '] .file-item-name-fileName-span').trigger('click'); }); $('.show_file_list .show_file').off('click').on('click', function (e) { $('#file_direct_link').empty(); get_file_download_url($(this).data('id'), share_id, 1); }); } else { $('.show_file_list_btn').fadeOut(); } } function get_file_download_url(fileid, shareid, show_link) { $.ajax({ url: '/api/open/file/getFileDownloadUrl.action', data: { fileId: fileid, shareId: shareid }, dataType: 'xml', success: function (data) { var fileName = $('.file-list-ul .c-file-list-item[data-fileid=' + fileid + '] .file-item-name-fileName-span').text(); var fileDownloadUrl = $(data).find('fileDownloadUrl').text() + '&fileName=' + encodeURIComponent(fileName); if(show_link == 1){ $('#file_direct_link').html('直链网址:' + fileDownloadUrl + ''); } unsafeWindow.location.href = 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; }