-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuto_Pop-ups.user.js
More file actions
108 lines (93 loc) · 3.48 KB
/
Copy pathAuto_Pop-ups.user.js
File metadata and controls
108 lines (93 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// ==UserScript==
// @name 自动处理消息类弹窗
// @namespace http://localhost/
// @version 1.0
// @description Automatically process information pop-ups
// @author you and me
// @match https://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 自动处理消息类弹窗
// 获取正确的window对象(兼容不同环境)
const targetWindow = typeof unsafeWindow !== 'undefined' ? unsafeWindow : window;
// 保存原始方法
const originalAlert = targetWindow.alert;
const originalConfirm = targetWindow.confirm;
const originalPrompt = targetWindow.prompt;
// 自动处理alert
targetWindow.alert = function(message) {
console.log('拦截alert:', message);
// 这里可以添加自定义处理逻辑
return undefined; // alert没有返回值
};
// 自动处理confirm
targetWindow.confirm = function(message) {
console.log('拦截confirm:', message);
// 默认返回true,可以根据需要修改
return true;
};
// 自动处理prompt
targetWindow.prompt = function(message, defaultText) {
console.log('拦截prompt:', message, '默认值:', defaultText);
// 返回默认值或自定义值
return defaultText || '';
};
// 使用MutationObserver检测动态创建的弹窗
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
checkForPopup(node);
// 同时检查新添加元素的所有子节点
if (node.querySelectorAll) {
node.querySelectorAll('*').forEach(checkForPopup);
}
}
});
});
});
// 检查是否是弹窗元素
function checkForPopup(element) {
if (element.classList &&
(element.classList.contains('alert') ||
element.classList.contains('modal') ||
element.classList.contains('dialog'))) {
handlePopup(element);
} else if (element.id &&
(element.id.includes('alert') ||
element.id.includes('modal') ||
element.id.includes('dialog'))) {
handlePopup(element);
}
}
// 处理检测到的弹窗
function handlePopup(element) {
console.log('检测到潜在弹窗元素:', element);
// 可以在这里添加移除或隐藏弹窗的逻辑
// 例如:element.remove();
// 或者:element.style.display = 'none';
// 示例:自动点击确认按钮
const confirmBtn = element.querySelector('.confirm-btn, .btn-ok, [onclick*="confirm"]');
if (confirmBtn) {
confirmBtn.click();
}
}
// 开始观察文档变化
observer.observe(document.body, {
childList: true,
subtree: true
});
// 可选:恢复原始方法的功能
function restoreOriginals() {
targetWindow.alert = originalAlert;
targetWindow.confirm = originalConfirm;
targetWindow.prompt = originalPrompt;
observer.disconnect();
}
// 暴露恢复方法到控制台,方便调试
targetWindow.restoreAlertFunctions = restoreOriginals;
console.log('自动弹窗拦截器已启用');
})();