精确自动关闭夸克网盘保存资源后的“文件保存成功”弹窗,避免误关“保存到”窗口。
// ==UserScript==
// @name 夸克网盘自动关闭保存成功弹窗
// @namespace http://tampermonkey.net/
// @version 0.3
// @description 精确自动关闭夸克网盘保存资源后的“文件保存成功”弹窗,避免误关“保存到”窗口。
// @author Your Name
// @match *://pan.quark.cn/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 定义一个函数来精确关闭“文件保存成功”弹窗
function closeQuarkSuccessPopup() {
// 查找所有可能的弹窗容器
// 这里使用更通用的类名,如 Ant Design 的 .ant-modal,或夸克可能使用的 .quark-modal
const modals = document.querySelectorAll('.ant-modal, .quark-modal, .el-dialog, .modal-dialog');
for (const modal of modals) {
// 确保弹窗是可见的 (即没有被 display: none 隐藏)
// 并且包含“文件保存成功”这个特定文本
if (modal.offsetParent !== null && modal.innerText.includes('文件保存成功')) {
console.log('Tampermonkey: 找到夸克网盘“文件保存成功”弹窗。');
// 尝试在其内部查找关闭按钮 (通常是带有X的按钮或图标)
let closeButton = modal.querySelector('.ant-modal-close-x') || // 常见的Ant Design关闭按钮
modal.querySelector('.quark-dialog-close') || // 夸克自定义弹窗关闭
modal.querySelector('.close-button') || // 其他可能的关闭按钮类名
modal.querySelector('.close-icon') ||
modal.querySelector('.ant-modal-close'); // 另一个可能的Ant Design关闭按钮
if (closeButton && closeButton.offsetParent !== null) {
console.log('Tampermonkey: 尝试点击其内部的关闭按钮。');
closeButton.click();
return true; // 成功点击关闭,停止查找
} else {
// 如果没有找到特定的关闭按钮,直接隐藏整个弹窗
// 这可能不是最佳用户体验,但作为备用方案确保弹窗消失
console.log('Tampermonkey: 未找到内部关闭按钮,尝试隐藏整个弹窗。');
modal.style.display = 'none';
// 同时尝试隐藏弹窗的遮罩层 (如果有的话)
let overlay = document.querySelector('.ant-modal-mask') || document.querySelector('.quark-modal-mask');
if (overlay && overlay.offsetParent !== null) {
overlay.style.display = 'none';
console.log('Tampermonkey: 隐藏弹窗遮罩层。');
}
return true; // 成功隐藏,停止查找
}
}
}
return false; // 未处理任何弹窗
}
// 使用 MutationObserver 监听 DOM 变化
// 当有新的节点被添加到页面时,检查是否是目标弹窗
const observer = new MutationObserver(function(mutations) {
for (const mutation of mutations) {
if (mutation.addedNodes.length > 0) {
// 如果有新节点添加,就尝试关闭弹窗
if (closeQuarkSuccessPopup()) {
// 如果成功关闭了一个弹窗,可以中断当前轮次的检查
// 如果希望脚本持续监听,即使关闭了一个弹窗也继续,则此处不加 return
break;
}
}
}
});
// 监听整个 body 的子节点变化,包括子树 (subtree: true)
// 这样可以确保即使弹窗是动态加载的,也能被检测到
observer.observe(document.body, { childList: true, subtree: true });
// 页面加载完成后也立即尝试关闭一次,以防弹窗是随页面一起加载的
window.addEventListener('load', closeQuarkSuccessPopup);
})();