// ==UserScript==
// @name 百度云重命名剔除特殊符号-自用
// @namespace http://tampermonkey.net/
// @version 0.14
// @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
// @require https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js
// @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 =`
button[title='新建文件夹']:before{
content: "";
width: 100%;
height: 100%;
display: inline-block;
position: absolute;
left: 0;
top: 0;
}
`
style.appendChild(document.createTextNode(styleStr));
head.appendChild(style);
setTimeout(()=>{
// 定位新建文件夹
let newFolderDom = document.querySelectorAll("button[title='新建文件夹']")[0];
newFolderDom.addEventListener("click",e => {
action = "newFolder";
console.log("action",action);
})
},500)
document.addEventListener('paste', (event) => {
// 阻止新文件名覆盖旧文件名,目的是获取就文件名
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();
}
});
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();
}
});
}
}
});
})
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'));
}
})();