// ==UserScript== // @name 阿里云盘-电视剧批量重命名工具-剧集刮削 // @namespace http://tampermonkey.net/ // @version 2.05 // @description 用于阿里云盘批量修改文件名,主要为 Filmly 适配阿里云盘上的不规则电视剧命名刮削准备,代码抄袭自 wdwy 发布的另外一个脚本 // @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/503840/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98-%E7%94%B5%E8%A7%86%E5%89%A7%E6%89%B9%E9%87%8F%E9%87%8D%E5%91%BD%E5%90%8D%E5%B7%A5%E5%85%B7-%E5%89%A7%E9%9B%86%E5%88%AE%E5%89%8A.user.js // @updateURL https://update.greasyfork.icu/scripts/503840/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98-%E7%94%B5%E8%A7%86%E5%89%A7%E6%89%B9%E9%87%8F%E9%87%8D%E5%91%BD%E5%90%8D%E5%B7%A5%E5%85%B7-%E5%89%A7%E9%9B%86%E5%88%AE%E5%89%8A.meta.js // ==/UserScript== (function() { 'use strict'; var $ = $ || window.$; var obj = { files: [], url: location.href, }; var op1 = `
文件夹:
原文件 42.mp4 回家的诱惑-第60集-720P(avc1F).mp4
模板 $$.mp4 回家的诱惑-第 $$ 集-720P(avc1F).mp4
结果 回家的诱惑.S01E01.mp4 回家的诱惑.S01E01.mp4
模板:
年份:
剧名:
季数:
预览:
`; obj.reset = function () { obj.files = []; 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) { let { response, responseURL } = this; try { response = JSON.parse(response) } catch (error) { }; if (responseURL.endsWith("/file/get_path")) { obj.initBatchButton(); // 设置路径名 if (response?.items) { obj.path = response.items.reverse().reduce((path, item) => path + item.name + "/", "/"); } if(response?.items){ obj.TVname = response.items[response.items.length-1].name } }else if (responseURL.indexOf("/file/list") > 0) { // 将 video 添加到 obj.files if (response && response.items) { if(obj.url && obj.url == location.href) { obj.files = obj.files.concat(response.items.filter(item=> item.category ==='video' )); }else { obj.reset(); obj.files = response.items.filter(item=> item.category ==='video') } } // 确定 file_extension obj.fileExtensions = [...new Set(obj.files.filter(item=> item.category ==='video').map(file => file.file_extension))]; } } }, false); send.apply(this, arguments); }; }; obj.showModifyPage = function() { //滚动到底,自动获取所有文件 const draggableElement = document.querySelector('.scroller---esn7'); draggableElement.scrollTop = draggableElement.scrollHeight; 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(); } }); $(".input-years").on("input",function(){ obj.updatePreview() }) $("#select-season").on("change",function(){ obj.updatePreview() }) $(".batch-after").on("input",function(){ obj.updatePreview() }) $(".batch-before").on("input",function(){ const bef = $(this).val() const hasDoubleDoller = bef.indexOf("$$") === -1 const modifyButton = $(".batch-modify") if(hasDoubleDoller){ modifyButton.attr('disabled',true) modifyButton.css('background-color', 'gray'); modifyButton.text("模板错误(需要包含 $$ 字符)"); }else{ modifyButton.attr('disabled',false) modifyButton.css('background-color', 'darkorange'); modifyButton.text("修改"); } }) $(".batch-path").text(obj.path); $(".batch-modify").on("click", obj.batchModify); $(".batch-after").val(obj.TVname) obj.updateTemplate() obj.updatePreview() }; obj.updateTemplate = function(){ if(obj.files && obj.files.length > 0){ const tvName = obj.files[0].name if(!$.isNumeric(tvName)){ // 不是简单的数字例如 40.mp4 $(".batch-before").val(tvName) $(".batch-modify").attr('disabled',true); $(".batch-modify").css('background-color', 'gray'); $(".batch-modify").text("模板错误(需要包含 $$ 字符)"); } }else{ console.log('empty file') } } obj.updatePreview = function() { const name = $(".batch-after").val() const years = $(".input-years").val() const season = $("#select-season").val() let text = '' for (const extension of obj.fileExtensions) { text += `${name}.S0${season}E01`; if(years){ text += `.${years}`; } text += `.${extension} ` } $("#preview-text").text(text); } 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.enableButton = function() { $(".batch-modify").attr('disabled',false); }; obj.batchModify = function() { $(".batch-modify").attr('disabled',true); const years = $(".input-years").val() const season = $("#select-season").val() var nameBefore = $(".batch-before").val().split("$$"); var nameAfter = $(".batch-after").val(); var suff = $(".batch-after-suff").val(); if(!obj.files) { alert("未从阿里云盘 /file/list 接口获取到 files !"); obj.enableButton(); return; } if(nameBefore.length !== 2){ alert("$$ 模版有误 "); obj.enableButton(); return; } const left = nameBefore[0] const right = nameBefore[1] //解析token const token = JSON.parse(getToken()); if(!token) { alert("请先登录!"); obj.enableButton(); return; } var tokenStr = token.token_type + " " + token.access_token; $(".ant-modal-batch-modify").remove(); obj.loading(); setTimeout(function() { let count = 0; for(let file of obj.files) { if(file.name.length < left.length + right.length ) continue; if(count > 200) break; const startIndex = file.name.indexOf(left) + left.length; const endIndex = file.name.lastIndexOf(right); // 提取出集数 let episode = file.name.substring(startIndex, endIndex); if(!$.isNumeric(episode)) { console.log('error: 集数不是 number , [',episode, '] name:', file.name) continue }; if(episode.length < 2){ episode =`0${episode}` } let newName = `${nameAfter}.S0${season}E${episode}`; if(years){ newName += `.${years}`; } newName += `.${file.file_extension}` if(newName === file.name){ console.log('相同的文件名, 无需修改, 跳过') continue } obj.ajaxModify(file, newName, tokenStr); count++; } //$(".ant-modal-batch-modify").remove(); $(".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