// ==UserScript==
// @name 蓝奏云网盘增强
// @version 1.2.3
// @author X.I.U
// @description 刷新不回根目录、后退返回上一级、右键文件显示菜单、自动显示更多文件、自动打开分享链接、自动复制分享链接、拖入文件自动显示上传框、输入密码后回车确认、调整描述(话说)编辑框初始大小
// @match *://*.lanzous.com/*
// @match *://*.lanzoux.com/*
// @match *://*.lanzoui.com/*
// @match *://pan.lanzou.com/*
// @match *://www.lanzou.com/account.php*
// @match *://www.lanzou.com/u
// @match *://up.woozooo.com/u
// @match *://up.woozooo.com/mydisk.php*
// @match *://pc.woozooo.com/u
// @match *://pc.woozooo.com/mydisk.php*
// @icon https://www.lanzou.com/favicon.ico
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_openInTab
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @grant unsafeWindow
// @noframes
// @license GPL-3.0 License
// @run-at document-end
// @namespace https://github.com/XIU2/UserScript
// ==/UserScript==
(function() {
var menu_ALL = [
['menu_open_fileSha', '自动打开分享链接', '自动打开分享链接', true],
['menu_copy_fileSha', '自动复制分享链接', '自动复制分享链接', true],
['menu_refreshCorrection', '刷新不返回根目录', '刷新不返回根目录', true],
['menu_rightClickMenu', '右键文件显示菜单', '右键文件显示菜单', true],
['menu_folderDescdesMenu', '调整描述(话说)编辑框大小', '调整描述(话说)编辑框大小', true]
], menu_ID = [], lastFolderID;
for (let i=0;i<menu_ALL.length;i++){ // 如果读取到的值为 null 就写入默认值
if (GM_getValue(menu_ALL[i][0]) == null){GM_setValue(menu_ALL[i][0], menu_ALL[i][3])};
}
registerMenuCommand();
// 注册脚本菜单
function registerMenuCommand() {
if (menu_ID.length > menu_ALL.length){ // 如果菜单ID数组多于菜单数组,说明不是首次添加菜单,需要卸载所有脚本菜单
for (let i=0;i<menu_ID.length;i++){
GM_unregisterMenuCommand(menu_ID[i]);
}
}
for (let i=0;i<menu_ALL.length;i++){ // 循环注册脚本菜单
menu_ALL[i][3] = GM_getValue(menu_ALL[i][0]);
if (menu_ALL[i][0] == 'menu_refreshCorrection') {
menu_ID[i] = GM_registerMenuCommand(`[ ${menu_ALL[i][3]?'√':'×'} ] ${menu_ALL[i][1]}`, function(){if(menu_value('menu_refreshCorrection')){UNrefreshCorrection();}else{refreshCorrection();};menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
}else{
menu_ID[i] = GM_registerMenuCommand(`[ ${menu_ALL[i][3]?'√':'×'} ] ${menu_ALL[i][1]}`, function(){menu_switch(`${menu_ALL[i][3]}`,`${menu_ALL[i][0]}`,`${menu_ALL[i][2]}`)});
}
}
menu_ID[menu_ID.length] = GM_registerMenuCommand('反馈 & 建议', function () {window.GM_openInTab('https://github.com/XIU2/UserScript#xiu2userscript', {active: true,insert: true,setParent: true});});
}
// 菜单开关
function menu_switch(menu_status, Name, Tips) {
let RefreshTips = '\n(刷新网页后生效)';
if (Name == 'menu_refreshCorrection')RefreshTips = ''
if (menu_status == 'true'){
GM_setValue(`${Name}`, false);
GM_notification({text: `已关闭 [${Tips}] 功能${RefreshTips}`, timeout: 3500});
}else{
GM_setValue(`${Name}`, true);
GM_notification({text: `已开启 [${Tips}] 功能${RefreshTips}`, timeout: 3500});
}
registerMenuCommand(); // 重新注册脚本菜单
};
// 返回菜单值
function menu_value(menuName) {
for (let menu of menu_ALL) {
if (menu[0] == menuName) {
return menu[3]
}
}
}
if (document.getElementById("infos")) { // 分享链接文件列表页
if (document.getElementById("pwdload")) { // 分享链接输入密码页
enterToPass(); // 输入密码后回车确认
}
setTimeout(fileMoreS, 300); // 自动显示更多文件
} else if (document.querySelector("iframe.ifr2")) { // 分享链接文件下载页(暂时没有这方面的功能,先空着)
//console.log()
} else if (document.getElementById("mainframe") || window.top.location.href.indexOf("mydisk.php?") > -1) { // 后台页
if (window.top.location.href != "https://pc.woozooo.com/mydisk.php") {
window.top.location.href = "https://pc.woozooo.com/mydisk.php"
}
var mainframe;
iframe();
}
// 获取 iframe 框架
function iframe() {
mainframe = document.getElementById("mainframe");
if(mainframe){ // 只有找到 iframe 框架时才会继续运行脚本
mainframe = mainframe.contentWindow;
if(menu_value('menu_refreshCorrection')){
refreshCorrection(); // 刷新不返回根目录(F5)
}
setTimeout(folderDescdes, 200); // 调整话说编辑框初始大小
setTimeout(hideSha, 200); // 隐藏分享链接窗口(这样自动打开/复制分享链接时,不会一闪而过)
fobiddenBack(); // 禁止浏览器返回(并绑定新的返回事件)
EventXMLHttpRequest(); // 监听 XMLHttpRequest 事件并执行 [自动显示更多文件]
dragEnter(); // 拖入文件自动显示上传框
}
}
// 刷新不返回根目录(F5)
function refreshCorrection() {
document.onkeydown = mainframe.onkeydown = function (e) {
e = window.event || e;
let keycode = e.keyCode;
if (keycode == 116) {
e.keyCode = 0;
let folderID = /-?\d+/.exec(mainframe.document.getElementById("filemore").children[0].getAttribute("onclick"))
if(folderID.length > 0){
mainframe.folder(folderID[0]);
e.returnValue = false;
e.cancelBubble = true;
return false;
}
}
}
}
// 恢复刷新机制
function UNrefreshCorrection() {
document.onkeydown = mainframe.onkeydown = function (e) {
e = window.event || e;
let keycode = e.keyCode;
if (keycode == 116) {
return true;
}
}
}
// 右键文件显示菜单
function rightClickMenu() {
if(menu_value('menu_rightClickMenu')){ // 脚本菜单开启时才继续
rightClickMenu_("sub_folder_list", "fols", "folse") // 文件夹
rightClickMenu_("filelist", "fs", "fse") // 文件
}
}
// 右键文件显示菜单,参数:文件/文件夹列表 ID、菜单 ID 前缀
function rightClickMenu_(list_id_name, menu_id_name_prefix, list_id_name_prefix) {
let list_ = mainframe.document.getElementById(list_id_name);
if(list_){ // 文件/文件夹列表
list_.oncontextmenu = function(e){
e.preventDefault(); // 屏蔽浏览器自身右键菜单
let left = e.pageX - 30; // 右键菜单弹出位置
let list_ID = e.target.id;
if(e.target.nodeName == "FONT"){
list_ID = e.target.parentNode.parentNode.id
}else if(e.target.id == ""){
list_ID = e.target.parentNode.id
}
list_ID = /\d+/.exec(list_ID)
if(list_ID.length > 0){
mainframe.document.getElementById(menu_id_name_prefix + list_ID[0]).style.cssText="position: absolute !important; left: " + left + "px;" // 修改右键菜单弹出位置(X)
mainframe.document.getElementById(list_id_name_prefix + list_ID[0]).focus();
mainframe.document.getElementById(list_id_name_prefix + list_ID[0]).click();
}
}
}
}
// 自动显示更多文件(后台页)
function fileMore() {
let filemore = mainframe.document.getElementById("filemore"); // 寻找 [显示更多文件] 按钮
if(filemore && filemore.style.display == "block"){ // 判断按钮是否存在且可见
if(filemore.children[0]){ // 判断按钮元素下第一个元素是否存在
filemore.children[0].click(); // 点击 [显示更多文件] 按钮
}
}
}
// 自动显示更多文件(分享链接列表页)
function fileMoreS() {
let filemore = document.getElementById("filemore"); // 寻找 [显示更多文件] 按钮
if(filemore && filemore.style.display != "none"){ // 判断按钮是否存在且可见
filemore.click(); // 点击 [显示更多文件] 按钮
}
}
// 调整话说编辑框初始大小
function folderDescdes() {
if(menu_value('menu_folderDescdesMenu')) {
let folderdescdes = mainframe.document.getElementById("folder_descdes"); // 寻找话说(描述)编辑框
if(folderdescdes){ // 判断话说(描述)元素是否存在
folderdescdes.style.cssText="margin: 15px 0px; width: 550px; height: 125px;"
}
let folderdescdes2 = mainframe.document.getElementById("fol_credes"); // 寻找话说(描述)编辑框
if(folderdescdes2){ // 判断话说(描述)元素是否存在
folderdescdes2.style.cssText="margin: 15px 0px; width: 550px; height: 125px;"
}
let folderdescdes3 = mainframe.document.getElementById("file_desc"); // 寻找话说(描述)编辑框
if(folderdescdes3){ // 判断话说(描述)元素是否存在
folderdescdes3.style.cssText="margin: 15px 0px; width: 550px; height: 125px;"
}
}
}
// 拖入文件自动显示上传框
function dragEnter() {
mainframe.addEventListener("dragenter", function (e) {
e.preventDefault();
e.stopPropagation();
let f_upb = mainframe.document.querySelector(".f_upb")
if(f_upb.style.top != "-36px") {
f_upb.style.top = "-36px";
mainframe.f_upc();
}
}, false);
}
// 分享链接相关(点击文件时)
function fileSha() {
var f_sha = mainframe.document.getElementById("f_sha"); // 寻找分享链接(下载链接)信息框
if(f_sha && f_sha.style.display == "block"){ // 判断信息框是否存在且可见
fileSha_Open(); // 自动打开分享链接(点击文件时)
fileSha_Copy(); // 自动复制分享链接(点击文件时)
if(menu_value('menu_open_fileSha') || menu_value('menu_copy_fileSha')){
f_sha.style.display = "none"; // 隐藏分享链接(下载链接)信息框
}
}
}
// 自动打开分享链接(点击文件时)
function fileSha_Open() {
if(menu_value('menu_open_fileSha')){ // 脚本菜单开启时才继续
let code = mainframe.document.getElementById("code").getAttribute("title"); // 获取分享链接(下载链接)
if(code != ""){ // 确保分享链接(下载链接)不是空
window.GM_openInTab(code, {active: true,insert: true,setParent: true}) // 打开分享链接(下载链接)
}
}
}
// 自动复制分享链接(点击文件时)
function fileSha_Copy() {
if(menu_value('menu_copy_fileSha')){ // 脚本菜单开启时才继续
let f_sha1 = mainframe.document.getElementById("f_sha1").innerText; // 获取分享链接(下载链接)
if(f_sha1 != ""){ // 确保分享链接(下载链接)不是空
copyToClipboard(f_sha1); // 复制到剪切板
}
}
}
// 复制到剪切板
function copyToClipboard(s){
if(window.clipboardData){
window.clipboardData.setData('text',s);
}else{
(function(s){
document.oncopy=function(e){
e.clipboardData.setData('text',s);
e.preventDefault();
document.oncopy=null;
}
})(s);
document.execCommand('Copy');
}
}
// 隐藏分享链接窗口(这样自动打开/复制分享链接时,不会一闪而过)
function hideSha(){
if(menu_value('menu_open_fileSha') || menu_value('menu_copy_fileSha')){ // [自动复制分享链接] 或 [自动打开分享链接] 任意一个功能开启时才继续
let style_Add = mainframe.document.createElement('style');
style_Add.type = 'text/css';
style_Add.innerHTML = `#f_sha {display: none !important;}`;
mainframe.document.head.appendChild(style_Add);
}
}
// 禁止浏览器返回(并绑定新的返回事件)
function fobiddenBack() {
history.pushState(null, null, document.URL);
window.addEventListener('popstate',backEvent)
}
// 允许浏览器返回
function enableBack() {
history.go(-1);
window.removeEventListener('popstate',backEvent)
}
// 浏览器后退事件函数
function backEvent() {
if(lastFolderID) {
mainframe.folder(lastFolderID);
}
history.pushState(null, null, document.URL);
}
// 获取上个文件夹 ID(用于浏览器后退事件)
function getLastFolderID() {
lastFolderID = null
let f_tpspan = mainframe.document.querySelectorAll("span.f_tpspan");
if(f_tpspan.length > 1) {
lastFolderID = /-?\d+/.exec(f_tpspan[f_tpspan.length - 2].getAttribute("onclick"))[0];
}
}
// 输入密码后回车确认
function enterToPass() {
document.getElementById('pwd').onkeydown = function(e){
if(e.keyCode == 13){
document.getElementById('sub').click();
}
};
}
// 定时执行(旧方法,每隔 100ms 执行一次,比较笨且浪费一丢丢性能,但优点是不会漏掉且反应更快)
//setInterval(fileMore,100);
// 监听 XMLHttpRequest 事件并执行(新方法,只有在产生事件时才会执行 [自动显示更多文件],平时不会执行,更优雅~)
function EventXMLHttpRequest() {
var _send = mainframe.XMLHttpRequest.prototype.send
function sendReplacement(data) {
setTimeout(fileMore, 200); // 自动显示更多文件
setTimeout(fileSha, 200); // 自动打开分享链接(点击文件时)
setTimeout(rightClickMenu, 500); // 右键文件显示菜单
setTimeout(getLastFolderID, 200); // 获取上个文件夹 ID(用于浏览器后退事件)
return _send.apply(this, arguments);
}
mainframe.XMLHttpRequest.prototype.send = sendReplacement;
}
/*(function (open) {
mainframe.XMLHttpRequest.prototype.open = function () {
this.addEventListener("readystatechange", function () {
if(this.responseURL != "") {
console.log(this.responseURL);
}
}, false);
open.apply(this, arguments);
};
})(mainframe.XMLHttpRequest.prototype.open);*/
})();