-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_accept.js
216 lines (187 loc) · 6.44 KB
/
auto_accept.js
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
class AutoAccept {
constructor(interval = 10000) {
this.interval = interval;
this.monitorInterval = null;
this.isRunning = false;
this.lastStatus = null;
this.hasAlertedComplete = false;
this.log('AutoAccept initialized');
this.start();
}
log(message, type = 'info') {
const timestamp = new Date().toISOString();
const prefix = '[AutoAccept]';
const fullMessage = `${prefix} ${timestamp} - ${message}`;
try {
window.postMessage({ type: 'log', message: fullMessage }, '*');
console.debug(fullMessage);
if (type === 'error') {
console.trace(fullMessage);
} else {
console.debug(fullMessage);
}
} catch (e) {
console.log(fullMessage);
}
}
start() {
if (this.isRunning) {
this.log('Auto accept is already running');
return false;
}
this.log('Starting auto accept...');
this.checkAndAccept();
this.monitorInterval = setInterval(() => {
this.checkAndAccept();
}, this.interval);
this.isRunning = true;
this.log(`Auto accept started, checking every ${this.interval/1000} seconds`);
return true;
}
stop() {
if (!this.isRunning) {
this.log('Auto accept is not running');
return false;
}
clearInterval(this.monitorInterval);
this.monitorInterval = null;
this.isRunning = false;
this.log('Auto accept stopped');
return true;
}
textIncludes(text, target) {
return text.toLowerCase().includes(target.toLowerCase());
}
getCurrentStatus(content) {
if (this.textIncludes(content, 'generating')) {
return 'generating';
}
if (this.textIncludes(content, 'completed')) {
return 'completed';
}
return 'unknown';
}
checkStatus(element) {
try {
if (!element) return;
const content = element.textContent || '';
const currentStatus = this.getCurrentStatus(content);
if (this.lastStatus !== currentStatus && currentStatus !== 'completed') {
this.hasAlertedComplete = false;
this.log(`Status changed from ${this.lastStatus} to ${currentStatus}, resetting complete alert`);
}
this.lastStatus = currentStatus;
switch (currentStatus) {
case 'generating':
this.log('Current status: generating');
break;
case 'completed':
if (!this.hasAlertedComplete) {
this.log('Current status: completed', 'info');
alert('Task completed');
this.hasAlertedComplete = true;
}
break;
case 'unknown':
this.log('Current status: unknown');
break;
}
return currentStatus;
} catch (error) {
this.log(`Error checking status: ${error.message}`, 'error');
return 'error';
}
}
triggerClick(element) {
const rect = element.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const mousedownEvent = new MouseEvent('mousedown', {
bubbles: true,
cancelable: true,
view: window,
button: 0,
buttons: 1,
clientX: centerX,
clientY: centerY,
screenX: centerX,
screenY: centerY,
detail: 1,
isTrusted: true
});
element.dispatchEvent(mousedownEvent);
this.log(`Triggered accept event: ${element.textContent.trim()}`);
}
checkAndAccept() {
try {
const inputBox = document.querySelector('div.full-input-box');
if (!inputBox) {
this.log('Input box not found');
return;
}
let previousSibling = inputBox.previousElementSibling;
if (!previousSibling) {
this.log('Previous sibling not found');
return;
}
const status = this.checkStatus(previousSibling);
if (status === 'completed') return;
const buttons = previousSibling.querySelectorAll('div.cursor-button-primary');
if (!buttons || buttons.length === 0) {
this.log('No buttons found');
return;
}
buttons.forEach(button => {
const buttonText = button.textContent.trim();
if (this.textIncludes(buttonText, 'Run command') ||
this.textIncludes(buttonText, 'Accept')) {
window.targetButton = button;
this.log(`Found button: ${buttonText}`);
this.triggerClick(button);
}
});
} catch (error) {
this.log(`Error executing: ${error.message}`, 'error');
}
}
getStatus() {
const status = {
isRunning: this.isRunning,
interval: this.interval,
intervalSeconds: this.interval / 1000,
lastStatus: this.lastStatus,
hasAlertedComplete: this.hasAlertedComplete
};
this.log(`Current status: ${JSON.stringify(status)}`);
return status;
}
setInterval(newInterval) {
if (typeof newInterval !== 'number' || newInterval < 1000) {
this.log('Interval must be a number greater than or equal to 1000 milliseconds', 'error');
return false;
}
this.interval = newInterval;
if (this.isRunning) {
this.stop();
this.start();
}
this.log(`Check interval updated to ${newInterval/1000} seconds`);
return true;
}
}
// Create global instance for console use
window.autoAccept = new AutoAccept();
window.autoAccept.start(5000);
/*
Usage:
1. Restart auto accept (if needed):
autoAccept.start()
2. Stop auto accept:
autoAccept.stop()
3. Check status:
autoAccept.getStatus()
4. Change check interval (in milliseconds):
autoAccept.setInterval(5000) // Set to 5 seconds
5. Manually reset status:
autoAccept.resetStatus()
*/