百度云重命名剔除特殊符号-自用

针对百度云新版重命名时提示【文件名不能包含以下字符:<,>,|,*,?,,/】,自动将特殊文件替换成空格

目前為 2023-02-13 提交的版本,檢視 最新版本

// ==UserScript==
// @name         百度云重命名剔除特殊符号-自用
// @namespace    http://tampermonkey.net/
// @version      0.17
// @description  针对百度云新版重命名时提示【文件名不能包含以下字符:<,>,|,*,?,,/】,自动将特殊文件替换成空格
// @author       zyb
// @match        https://pan.baidu.com/disk/*
// @exclude      https://pan.baidu.com/share/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=baidu.com
// @grant        GM_addStyle
// @license      MIT
// ==/UserScript==

(function() {
	'use strict';

	// Your code here...
	let action = "";
	const head = document.head;
	const style = document.createElement("style");
	// 增加一个蒙版,以便触发新建文件夹的监听事件
	const styleStr =`
        .newFolderBtnBox{
            width:118px;
            height:100%;
            position:absolute;
            top:0;
            left:115px;
            cursor: pointer;
        }
    `
	style.appendChild(document.createTextNode(styleStr));
	head.appendChild(style);

	const newFolderBtnBoxDivDom = document.createElement("div");
	newFolderBtnBoxDivDom.setAttribute("class","newFolderBtnBox");

	const headerToolBar=document.querySelectorAll(".wp-s-agile-tool-bar.is-header-tool")[0];
	headerToolBar.style.position="relative";
	headerToolBar.appendChild(newFolderBtnBoxDivDom);

	setTimeout(()=>{
		newFolderBtnBoxDivDom.addEventListener("click",e => {
			// 定位新建文件夹
			let newFolderDom = document.querySelectorAll("button[title='新建文件夹']")[0];
			if(!newFolderDom){
				console.log(e)
				let shareDom = document.querySelectorAll(".is-header-tool button[title='共享']")[0] || document.querySelectorAll(".is-header-tool button[title='下载']")[0];

				if(e.clientX>455){
					// 下载或者取消共享按钮
					shareDom.parentNode.parentNode.children[2].children[0].click();
				}else{
					shareDom.click();
				}
				return;
			}

			// 因为覆盖了原来的新建文件夹,所以只能手动触发,有新bug
			newFolderDom.click();
			action = "newFolder";
			console.log("action",action);
		})
	},500)

	let ajax = {
		post:function(obj,callback){
			let {url,data}=obj

			//第一步  先城建一个ajax的核心 XMLHttpRequest
			let xhr = new XMLHttpRequest();
			//第二步  使用open 创建请求 第一个参数是请求方式 第二个是请求的地址  第三个是同步或者异步
			xhr.open("post",url,true)
			//如果是post请求  必须要写请求头
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded') //设置请求头
			//第三步  为xhr.onreadystatechange  设置监听事件
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4) {
					if(xhr.status == 200){
						//readyState  0 请求未初始化  刚刚实例化XMLHttpRequest
						//readyState  1 客户端与服务器建立链接  调用open方法
						//readyState  2 请求已经被接收
						//readyState  3 请求正在处理中
						//readyState  4 请求成功
						callback && callback();
					}
				}
			}
			// 第四步 发送请求数据  调用send 发送请求 如果不需要参数就写一个null
			xhr.send(data)
		},
		get:function(obj,callback){
			let {url,data}=obj

			//第一步  先城建一个ajax的核心 XMLHttpRequest
			let xhr = new XMLHttpRequest();
			//第二步  使用open 创建请求 第一个参数是请求方式 第二个是请求的地址  第三个是同步或者异步
			xhr.open("get",url,true);
			//第三步  为xhr.onreadystatechange  设置监听事件
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4) {
					if(xhr.status == 200){
						//readyState  0 请求未初始化  刚刚实例化XMLHttpRequest
						//readyState  1 客户端与服务器建立链接  调用open方法
						//readyState  2 请求已经被接收
						//readyState  3 请求正在处理中
						//readyState  4 请求成功
						callback && callback();
					}
				}
			}
			// 第四步 发送请求数据  调用send 发送请求 如果不需要参数就写一个null
			xhr.send()
		}
	}

	document.addEventListener('paste', (event) => {
		let inputDom = document.querySelectorAll(".wp-s-pan-list__file-name-edit--title-edit-input input")[0];
		let miaochuanInputDom = document.querySelectorAll("#mzf-path-input")[0];
		if(!miaochuanInputDom && !inputDom){
			return;
		}
		// 阻止新文件名覆盖旧文件名,目的是获取就文件名
		event.preventDefault();
		// 获取解析 粘贴的文本
		const clipboard = navigator.clipboard;
		let clipPromise = clipboard.readText();
		clipPromise.then(function(clipText){
			let newname = clipText.replaceAll(/<|>|\||\*|\?|\,|\/|:/g," ");
			console.log(newname);
			// 将新文件名回写到剪切板中
			clipboard.writeText(newname);

			let inputDom = document.querySelectorAll(".wp-s-pan-list__file-name-edit--title-edit-input input")[0];
			let miaochuanInputDom = document.querySelectorAll("#mzf-path-input")[0];

			// 秒传重命名
			if(miaochuanInputDom){
				miaochuanInputDom.value = newname;
				return;
			}
			// 是否存在重命名按钮
			let reNameBox = document.querySelectorAll(".is-header-tool button[title='重命名']")[0];

			// 如果存在重命名按钮,说明用户选择的是重命名模式
			if(reNameBox){
				action = "rename";
				console.log("action",action);
			}

			// 获取hash链接: "#/index?category=all&path=%2Fapps"
			let hashStr = location.hash || "";
			// 解析hash链接获取路径
			let pathStr = decodeURIComponent(hashStr.split('&').filter(function(item){return item.indexOf("path=")>-1})[0]?.split("path=")[1]||'/');
			// 使用unsafeWindow获取最外层window数据
			let bdstoken = unsafeWindow.locals.userInfo.bdstoken;
			// 计算logId
			let dpLogid = getLogID();

			// 新建文件夹模式
			if(action==="newFolder"){
				inputDom && (inputDom.value = newname);

				/*$.ajax(`https://pan.baidu.com/api/create?a=commit&bdstoken=${bdstoken}&clienttype=0&app_id=250528&web=1&dp-logid=${dpLogid}`,{
					type:'post',
					data:{
						path : `${pathStr}/${newname}`,
						isdir: 1,
						block_list: [],
					},
					complete:function () {
						console.log("complete!!");
						location.reload();
					}
				});*/
				ajax.post({
					url:`https://pan.baidu.com/api/create?a=commit&bdstoken=${bdstoken}&clienttype=0&app_id=250528&web=1&dp-logid=${dpLogid}`,
					data:`path=${pathStr}/${newname}&isdir=1&block_list=[]`
				},()=>{
					console.log("complete!!");
					location.reload();
				})


				return;
			}

			// 重命名文件夹模式
			if(action === "rename"){
				// 获取原文件名
				let oldName = inputDom.value || document.querySelectorAll(".nd-detail-filelist__name")[0]?.textContent || "";

				if(newname===oldName || !oldName){
					oldName = prompt("重命名失败,请输入旧文件名");
					console.log("oldName",oldName)
				}
				inputDom && (inputDom.value = newname);

				if(inputDom){
					/*$.ajax(`https://pan.baidu.com/api/filemanager?async=2&onnest=fail&opera=rename&bdstoken=${bdstoken}&clienttype=0&app_id=250528&web=1&dp-logid=${dpLogid}`,{
						type:'post',
						data:{
							filelist : JSON.stringify([{
								path : `${pathStr}/${oldName}`,
								newname : newname
							}])
						},
						complete:function () {
							console.log("complete!!");
							location.reload();
						}
					});*/
					ajax.post({
						url:`https://pan.baidu.com/api/filemanager?async=2&onnest=fail&opera=rename&bdstoken=${bdstoken}&clienttype=0&app_id=250528&web=1&dp-logid=${dpLogid}`,
						data:"filelist="+JSON.stringify([{path : `${pathStr}/${oldName}`,newname : newname}])
					},()=>{
						console.log("complete!!");
						location.reload();
					})
				}
			}


		});

	})

	function getLogID() {

		var n = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/~!@#¥%……&";
		var r = String.fromCharCode;
		var a = function(e) {
			if (e.length < 2) {
				var t = e.charCodeAt(0);
				return 128 > t ? e: 2048 > t ? r(192 | t >>> 6) + r(128 | 63 & t) : r(224 | t >>> 12 & 15) + r(128 | t >>> 6 & 63) + r(128 | 63 & t)
			}
			var t2 = 65536 + 1024 * (e.charCodeAt(0) - 55296) + (e.charCodeAt(1) - 56320);
			return r(240 | t2 >>> 18 & 7) + r(128 | t2 >>> 12 & 63) + r(128 | t2 >>> 6 & 63) + r(128 | 63 & t2)
		};
		var o = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
		var c = function(e) {
			return (e + "" + Math.random()).replace(o, a)
		};
		var i = function(e) {
			var t = [0, 2, 1][e.length % 3],
				r = e.charCodeAt(0) << 16 | (e.length > 1 ? e.charCodeAt(1) : 0) << 8 | (e.length > 2 ? e.charCodeAt(2) : 0),
				a = [n.charAt(r >>> 18), n.charAt(r >>> 12 & 63), t >= 2 ? "=": n.charAt(r >>> 6 & 63), t >= 1 ? "=": n.charAt(63 & r)];
			return a.join("")
		};
		var d = function(e) {
			return e.replace(/[\s\S]{1,3}/g, i)
		};
		var l = function() {
			return d(c((new Date).getTime()))
		};
		var u = function(e, t) {
			return t ? l(String(e)).replace(/[+\/]/g,
											function(e) {
				return "+" == e ? "-": "_"
			}).replace(/=/g, "") : l(String(e))
		};
		var f = function(e) {
			var t, n;
			if(document.cookie.length > 0 && (t = document.cookie.indexOf(e + "="), -1 != t)){
				t = t + e.length + 1;
				n = document.cookie.indexOf(";", t);
				-1 == n && (n = document.cookie.length);
				return decodeURI(document.cookie.substring(t, n))
			}else{
				return "";
			}
			// return document.cookie.length > 0 && (t = document.cookie.indexOf(e + "="), -1 != t) ? (t = t + e.length + 1, n = document.cookie.indexOf(";", t), -1 == n && (n = document.cookie.length), decodeURI(document.cookie.substring(t, n))) : ""
		};
		var h = function(e) {
			var t = new RegExp(e + "=([^#&]*)", "g"),
				n = t.exec(location.hash);
			return n ? decodeURIComponent(n[1]) : ""
		}

		return u(f('BAIDUID'));

	}
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址