-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
249 lines (223 loc) · 5.6 KB
/
Copy pathutils.js
File metadata and controls
249 lines (223 loc) · 5.6 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 工具函数库 - Cross-Tab Auto RPA Pro
/**
* 等待指定时间
* @param {number} ms - 等待的毫秒数
* @returns {Promise}
*/
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* 等待元素出现在 DOM 中
* @param {string} selector - CSS 选择器
* @param {number} timeout - 超时时间(毫秒)
* @returns {Promise<HTMLElement>}
*/
function waitForElement(selector, timeout = 10000) {
return new Promise((resolve, reject) => {
const element = document.querySelector(selector);
if (element) {
resolve(element);
return;
}
const observer = new MutationObserver((mutations, obs) => {
const element = document.querySelector(selector);
if (element) {
obs.disconnect();
resolve(element);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
setTimeout(() => {
observer.disconnect();
reject(new Error(`元素 ${selector} 在 ${timeout}ms 内未找到`));
}, timeout);
});
}
/**
* 检查元素是否可见
* @param {HTMLElement} element - DOM 元素
* @returns {boolean}
*/
function isElementVisible(element) {
if (!element) return false;
const style = window.getComputedStyle(element);
if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {
return false;
}
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
}
/**
* 滚动到元素使其可见
* @param {HTMLElement} element - DOM 元素
* @param {object} options - 滚动选项
*/
function scrollToElement(element, options = {}) {
const defaultOptions = {
behavior: 'smooth',
block: 'center',
inline: 'center'
};
element.scrollIntoView({ ...defaultOptions, ...options });
}
/**
* 触发元素点击事件(模拟真实点击)
* @param {HTMLElement} element - DOM 元素
*/
function triggerClick(element) {
if (!element) return;
// 触发 mousedown 事件
element.dispatchEvent(new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
view: window
}));
// 触发 click 事件
element.click();
// 触发 mouseup 事件
element.dispatchEvent(new MouseEvent('mouseup', {
bubbles: true,
cancelable: true,
view: window
}));
}
/**
* 模拟输入文本到元素
* @param {HTMLElement} element - DOM 元素
* @param {string} text - 要输入的文本
* @param {number} delay - 每个字符间的延迟(毫秒)
*/
async function typeText(element, text, delay = 50) {
if (!element) return;
element.focus();
element.value = '';
for (let i = 0; i < text.length; i++) {
element.value += text[i];
element.dispatchEvent(new Event('input', { bubbles: true }));
await sleep(delay);
}
element.dispatchEvent(new Event('change', { bubbles: true }));
element.dispatchEvent(new Event('blur', { bubbles: true }));
}
/**
* 生成唯一 ID
* @returns {string}
*/
function generateId() {
return `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* 格式化时间戳
* @param {number} timestamp - 时间戳
* @returns {string}
*/
function formatTimestamp(timestamp) {
const date = new Date(timestamp);
return date.toLocaleTimeString('zh-CN', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
}
/**
* 防抖函数
* @param {Function} func - 要防抖的函数
* @param {number} wait - 等待时间(毫秒)
* @returns {Function}
*/
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* 节流函数
* @param {Function} func - 要节流的函数
* @param {number} limit - 时间限制(毫秒)
* @returns {Function}
*/
function throttle(func, limit) {
let inThrottle;
return function executedFunction(...args) {
if (!inThrottle) {
func(...args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
/**
* 深度克隆对象
* @param {object} obj - 要克隆的对象
* @returns {object}
*/
function deepClone(obj) {
if (obj === null || typeof obj !== 'object') return obj;
if (obj instanceof Date) return new Date(obj.getTime());
if (obj instanceof Array) return obj.map(item => deepClone(item));
const clonedObj = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObj[key] = deepClone(obj[key]);
}
}
return clonedObj;
}
/**
* 安全的 JSON 解析
* @param {string} jsonString - JSON 字符串
* @param {*} defaultValue - 解析失败时的默认值
* @returns {*}
*/
function safeJsonParse(jsonString, defaultValue = null) {
try {
return JSON.parse(jsonString);
} catch (error) {
console.error('JSON 解析失败:', error);
return defaultValue;
}
}
/**
* 发送消息到 background script
* @param {object} message - 消息对象
* @returns {Promise}
*/
function sendMessageToBackground(message) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(message, (response) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(response);
}
});
});
}
// 将工具函数暴露到全局作用域
if (typeof window !== 'undefined') {
window.RPAUtils = {
sleep,
waitForElement,
isElementVisible,
scrollToElement,
triggerClick,
typeText,
generateId,
formatTimestamp,
debounce,
throttle,
deepClone,
safeJsonParse,
sendMessageToBackground
};
}