-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
200 lines (188 loc) · 8.25 KB
/
popup.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
/**
* This JavaScript file is designed to automate a process of interacting with posts on a webpage.
*
* Global Variables:
* - isProcessRunning: A boolean flag to control the process state.
*
* DOM Elements:
* - startButton: Button element to start the process.
* - stopButton: Button element to stop the process.
*
* Event Listeners:
* - startButton click event: Initiates the process if it's not already running.
* - stopButton click event: Stops the process if it's currently running.
*
* Functions:
* - startAutoProcess: Initiates the automated process of interacting with posts.
* - stopAutoProcess: Stops the automated process.
*
* Internal Function:
* - processNextPost: Handles the interaction with each post sequentially.
*
* Chrome Scripting:
* - chrome.scripting.executeScript: Executes the start or stop functions in the active tab.
*
* Chrome Runtime Messaging:
* - chrome.runtime.onMessage.addListener: Listens for messages to update the process state.
*/
// Global variable to control the process
let isProcessRunning = false;
const startButton = document.getElementById("startShootingArrow");
const stopButton = document.getElementById("stopShootingArrow");
if (startButton && stopButton) {
startButton.addEventListener("click", () => {
if (!isProcessRunning) {
isProcessRunning = true;
startButton.textContent = "Shooting...";
stopButton.textContent = "Stop Shooting";
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: startAutoProcess,
});
},
);
}
});
stopButton.addEventListener("click", () => {
if (isProcessRunning) {
isProcessRunning = false;
stopButton.textContent = "Stopping...";
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: stopAutoProcess,
});
},
);
}
});
}
function startAutoProcess() {
let currentPostIndex = 0;
function processNextPost() {
if (!window.isProcessRunning) {
console.log("Process stopped.");
chrome.runtime.sendMessage({ action: "processStopped" });
return;
}
const posts = document.querySelectorAll(
".x1yztbdb.x1n2onr6.xh8yej3.x1ja2u2z",
);
if (posts.length > currentPostIndex) {
const targetElement = posts[currentPostIndex];
targetElement.scrollIntoView({
behavior: "smooth",
block: "start",
});
setTimeout(() => {
if (!window.isProcessRunning) return;
// Find and click the "Actions for this post" button
const actionsButton = targetElement.querySelector(
'div[aria-label="Actions for this post"]',
);
if (actionsButton) {
actionsButton.click();
setTimeout(() => {
if (!window.isProcessRunning) return;
// Find and click the "Report post to group admins" button
const reportButton = Array.from(
document.querySelectorAll('div[role="menuitem"]'),
).find((el) =>
el.textContent.includes(
"Report post to group admins",
),
);
if (reportButton) {
reportButton.click();
setTimeout(() => {
if (!window.isProcessRunning) return;
// Click the specified element
const specificElement = document.querySelector(
".x1n2onr6:nth-child(10) .x1i10hfl > .x9f619 > .x9f619",
);
if (specificElement) {
specificElement.click();
setTimeout(() => {
if (!window.isProcessRunning) return;
// Find and click the "Done" button
const doneButton =
document.querySelector(
'div[aria-label="Done"]',
);
if (doneButton) {
doneButton.click();
setTimeout(() => {
if (!window.isProcessRunning)
return;
console.log(
"Process completed for post " +
(currentPostIndex + 1),
);
currentPostIndex++;
// Process the next post
processNextPost();
}, 1000);
} else {
console.log(
'The "Done" button was not found.',
);
currentPostIndex++;
// Move to next post even if "Done" button is not found
processNextPost();
}
}, 1000);
} else {
console.log(
"The specified element was not found.",
);
currentPostIndex++;
// Move to next post even if specific element is not found
processNextPost();
}
}, 2000);
} else {
console.log(
'The "Report post to group admins" button was not found.',
);
currentPostIndex++;
// Move to next post even if "Report" button is not found
processNextPost();
}
}, 1000);
} else {
console.log(
'The "Actions for this post" button was not found.',
);
currentPostIndex++;
// Move to next post even if "Actions" button is not found
processNextPost();
}
}, 1000);
} else {
console.log("All visible posts have been processed.");
window.isProcessRunning = false;
chrome.runtime.sendMessage({ action: "processStopped" });
}
}
window.isProcessRunning = true;
// Start the process with the first post
processNextPost();
}
function stopAutoProcess() {
window.isProcessRunning = false;
console.log("Process has been stopped.");
chrome.runtime.sendMessage({ action: "processStopped" });
}
// Listen for messages from the content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "processStopped") {
isProcessRunning = false;
startButton.textContent = "Start Shooting";
stopButton.textContent = "Stop Shooting";
}
});