// ==UserScript== // @name 阿里云盘-批量修改文件名-剧集刮削 // @namespace http://tampermonkey.net/ // @version 2.05 // @description 用于阿里云盘批量修改文件名,主要为剧集刮削准备 // @author wdwy // @match https://www.aliyundrive.com/drive/folder/* // @match https://www.aliyundrive.com/drive/* // @match https://www.aliyundrive.com/drive/legacy/folder/* // @match https://www.aliyundrive.com/drive/legacy/ // @match https://www.aliyundrive.com/drive/file/backup/* // @match https://www.aliyundrive.com/drive/file/backup/ // @match https://www.aliyundrive.com/drive/file/* // @match https://www.alipan.com/drive/folder/* // @match https://www.alipan.com/drive/* // @match https://www.alipan.com/drive/legacy/folder/* // @match https://www.alipan.com/drive/legacy/ // @match https://www.alipan.com/drive/file/backup/* // @match https://www.alipan.com/drive/file/backup/ // @match https://www.alipan.com/drive/file/* // @icon https://gw.alicdn.com/imgextra/i3/O1CN01aj9rdD1GS0E8io11t_!!6000000000620-73-tps-16-16.ico // @require https://code.jquery.com/jquery-3.6.0.min.js // @run-at document-body // @license GPLv3 // @downloadURL https://update.greasyfork.icu/scripts/459303/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98-%E6%89%B9%E9%87%8F%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6%E5%90%8D-%E5%89%A7%E9%9B%86%E5%88%AE%E5%89%8A.user.js // @updateURL https://update.greasyfork.icu/scripts/459303/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98-%E6%89%B9%E9%87%8F%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6%E5%90%8D-%E5%89%A7%E9%9B%86%E5%88%AE%E5%89%8A.meta.js // ==/UserScript== (function() { 'use strict'; var $ = $ || window.$; var obj = { files: [], randomFillParam: -1, url: location.href, panel: 1 }; var op1 = `
剧集重命名
当前路径地址:

修改前名字模板:


修改后名字模板:

使用原始后缀


`; var op2 = `
查找替换
当前路径地址:

查找
替换为


`; obj.reset = function () { obj.files = []; obj.randomFillParam = -1; obj.url = location.href; }; obj.initBatchButton = function () { if ($(".button--batch").length) { return; } if ($("#root header").length) { var html = ''; html += '
'; $("#root header:eq(0)").append(html); $(".button--batch").on("click", obj.showModifyPage); }else { setTimeout(obj.initBatchButton, 1000); } }; obj.init = function () { var send = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.send = function(data) { this.addEventListener("load", function(event) { if(this.readyState == 4 && this.status == 200) { var response = this.response, responseURL = this.responseURL; try { response = JSON.parse(response) } catch (error) { }; if (responseURL.endsWith("/file/get_path")) { obj.initBatchButton(); //设置路径名 if (response instanceof Object && response.items) { obj.path = "/"; var items = response.items; for(var i=items.length-1; i>=0; i--) { obj.path += items[i].name+"/"; } } }else if (responseURL.indexOf("/file/list") > 0) { //if (document.querySelector(".ant-modal-mask")) { //排除【保存 移动 等行为触发】 //return; //} if (response && response.items) { if(obj.url && obj.url == location.href) { obj.files = obj.files.concat(response.items); }else { obj.reset(); obj.files = response.items; } } } } }, false); send.apply(this, arguments); }; }; obj.showModifyPage = function() { //滚动到底,自动获取所有文件 obj.pageScroll(); var html = `
`; $("body").append(html); $(".ant-modal-batch-modify .icon-wrapper--TbIdu").on("click", function () { $(".ant-modal-batch-modify").remove(); }); $(".ant-modal-batch-modify .ant-modal-wrap").on("click", function (event) { if ($(event.target).closest(".ant-modal-content").length == 0) { $(".ant-modal-batch-modify").remove(); } }); $("#option1").on("click", 1, obj.switchPanel); $("#option2").on("click", 2, obj.switchPanel); obj.prepareOp1(); obj.panel = 1; }; obj.prepareOp1 = function() { $(".batch-path").val(obj.path); $(".batch-clear").on("click", obj.clear); $(".batch-modify").on("click", obj.batchModify); $(".batch-randomFill").on("click", obj.randomFill); $(".batch-origin-suff").on("click", obj.changeOriSuff); $(".batch-before").on("blur", obj.checkBefore); } obj.prepareOp2 = function() { $(".batch-path").val(obj.path); $(".batch-clear-2").on("click", obj.clear2); $(".batch-modify-2").on("click", obj.replace); } obj.loading = function() { var html = `
`; $("body").append(html); } obj.refreshLoading = function(num) { var loading = $(".batch-modify-loading-num"); if(loading) loading.html(num); loading.get(0).innerHTML = loading.get(0).innerHTML; } obj.checkBefore = function() { var batchBefore = $(".batch-before").val(); var pat = /\$[0-9]{1,4}\$/; if(isBlank(batchBefore) || !pat.test(batchBefore)) { $(".batch-origin-suff").prop("checked", false); $(".batch-after-suff").removeAttr("disabled"); $(".batch-after-suff").val(""); alert("请先正确标记集数!"); return false; } return true; } obj.changeOriSuff = function() { var checked = $(".batch-origin-suff").prop("checked"); if(checked) { if(!obj.checkBefore()) return; var batchBefore = $(".batch-before").val(); $(".batch-after-suff").attr("disabled", "disabled"); $(".batch-after-suff").val(batchBefore.substring(batchBefore.lastIndexOf("$")+1, batchBefore.length)); }else { $(".batch-after-suff").removeAttr("disabled"); $(".batch-after-suff").val(""); } } obj.clear = function() { $(".batch-before").val(""); $(".batch-after").val(""); $(".batch-after-suff").val(""); }; obj.clear2 = function() { $(".batch-find").val(""); $(".batch-replace").val(""); }; obj.disableButton = function() { $(".batch-randomFill").attr('disabled',true); $(".batch-clear").attr('disabled',true); $(".batch-modify").attr('disabled',true); }; obj.enableButton = function() { $(".batch-randomFill").attr('disabled',false); $(".batch-clear").attr('disabled',false); $(".batch-modify").attr('disabled',false); }; obj.batchModify = function() { obj.disableButton(); var nameBefore = $(".batch-before").val(); var nameAfter = $(".batch-after").val(); var suff = $(".batch-after-suff").val(); if(isBlank(nameBefore) || isBlank(nameAfter)) { alert("修改名字不能为空!"); obj.enableButton(); return; } if(nameBefore === nameAfter) { alert("修改前后名字不能一样!"); obj.enableButton(); return; } if(!obj.files) { alert("当前路径文件为空!"); obj.enableButton(); return; } //检查集数标记 if(!obj.checkBefore()){ obj.enableButton(); return; } var pos = []; var cnt = 0; for(var i=0; i 200) break; //拼接新名字 var episode = f.name.substring(pos[0], pos[1]-1); if(!$.isNumeric(episode)) continue; var newName = nameAfter+"E"+episode; if(checked) { if(pos[1]-1 < f.name.length) newName += f.name.substring(pos[1]-1, f.name.length); }else { if(!isBlank(suff)) newName += "_"+suff; if(!isBlank(f.file_extension)) newName += "."+f.file_extension; } //console.log(f.name + " -> " + newName); obj.ajaxModify(f, newName, tokenStr); count++; } //$(".ant-modal-batch-modify").remove(); $(".ant-modal-loading").remove(); alert("完成修改【"+count+"】个文件"); setTimeout(function() {window.location.reload()}, 100); }, 0); } obj.replace = function() { let findTxt = $(".batch-find").val(); let replaceTxt = $(".batch-replace").val(); if(isBlank(findTxt)) { alert("查找文本不能为空!"); return; } if(!isBlank(replaceTxt) && replaceTxt === findTxt) { alert("查找和替换文本不能相同!"); return; } //解析token let token = JSON.parse(getToken()); if(!token) { alert("请先登录!"); obj.enableButton(); return; } let tokenStr = token.token_type + " " + token.access_token; $(".ant-modal-batch-modify").remove(); obj.loading(); setTimeout(function() { let count = 0; for(let f of obj.files) { //检查文件是否应该修改 if(count > 200) break; let newName = f.name.replace(findTxt, replaceTxt); if(isBlank(newName)) continue; if(f.name === newName) continue; console.log(f.name + " -> " + newName); obj.ajaxModify(f, newName, tokenStr); count++; } $(".ant-modal-loading").remove(); alert("完成修改【"+count+"】个文件"); setTimeout(function() {window.location.reload()}, 100); }, 0); } function getToken() { let token = localStorage.getItem("token"); if(isBlank(token)) { token = sessionStorage.getItem("token"); if(isBlank(token)) { token = getCookie("token"); } } return token; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for(var i=0; i