-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
161 lines (134 loc) · 4.67 KB
/
Copy pathcontent.js
File metadata and controls
161 lines (134 loc) · 4.67 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
console.log("Content script loaded!");
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const getOwnerUsernameFromURL = () => {
const currentUrl = window.location.href;
const match = currentUrl.match(/x\.com\/([^/]+)/);
return match ? match[1] : null;
};
const isOwnTweet = (tweet) => {
const ownerUsername = getOwnerUsernameFromURL();
const usernameElement = tweet.querySelector('a[role="link"] > div > span');
if (usernameElement && ownerUsername) {
const tweetUsername = usernameElement.innerText.replace('@', '').trim();
const ownerUsernameCleaned = ownerUsername.replace('@', '').trim();
return tweetUsername === ownerUsernameCleaned;
}
return false;
};
const processTweet = async (tweet) => {
try {
console.log("Processing a tweet...");
const currentUrl = window.location.href;
if (currentUrl.includes("/likes")) {
const unlikeButton = tweet.querySelector('button[data-testid="unlike"]');
if (unlikeButton) {
console.log("Unlike button found. Clicking...");
unlikeButton.click();
console.log("Tweet unliked.");
await wait(10);
return true;
} else {
console.warn("Unlike button not found for this tweet. Skipping...");
return false;
}
}
if (currentUrl.includes("/with_replies")) {
const unlikeButton = tweet.querySelector('button[data-testid="unlike"]');
if (unlikeButton) {
console.log("Unlike button found. Clicking...");
unlikeButton.click();
console.log("Tweet unliked.");
await wait(1000);
}
return await deleteTweet(tweet);
}
const undoRetweetButton = tweet.querySelector('button[data-testid="unretweet"]');
if (undoRetweetButton) {
console.log("Undo retweet button found. Clicking...");
undoRetweetButton.click();
await wait(1000);
const confirmUndoRetweet = document.querySelector('div[data-testid="unretweetConfirm"]');
if (confirmUndoRetweet) {
console.log("Confirming undo retweet...");
confirmUndoRetweet.click();
console.log("Undo retweet successful.");
await wait(2000);
return true;
} else {
console.warn("Confirmation for undo retweet not found. Skipping...");
return false;
}
}
return await deleteTweet(tweet);
} catch (error) {
console.error("Error processing tweet:", error);
return false;
}
};
const deleteTweet = async (tweet) => {
try {
if (!isOwnTweet(tweet)) {
console.warn("This is not your tweet. Skipping...");
return false;
}
const moreButton = tweet.querySelector('button[data-testid="caret"]');
if (!moreButton) {
console.warn("More button not found for this tweet. Skipping...");
return false;
}
console.log("Clicking the 'More' button...");
moreButton.click();
await wait(1000);
const deleteOption = Array.from(
document.querySelectorAll('div[role="menuitem"]')
).find((item) => item.textContent.includes("Delete"));
if (!deleteOption) {
console.warn("Delete option not found in dropdown. Skipping...");
return false;
}
console.log("Clicking the 'Delete' option...");
deleteOption.click();
await wait(1000);
const confirmButton = document.querySelector('button[data-testid="confirmationSheetConfirm"]');
if (!confirmButton) {
console.warn("Confirmation button not found. Skipping...");
return false;
}
console.log("Clicking the 'Confirm Delete' button...");
confirmButton.click();
console.log("Tweet deleted.");
await wait(3000);
return true;
} catch (error) {
console.error("Error deleting tweet:", error);
return false;
}
};
const deleteTweetsSequentially = async () => {
console.log("Starting tweet deletion process...");
while (true) {
const tweets = document.querySelectorAll('article[data-testid="tweet"]');
if (tweets.length === 0) {
console.log("No tweets found. Scrolling down...");
window.scrollBy(0, 2 * window.innerHeight);
await wait(2000);
const moreTweets = document.querySelectorAll('article[data-testid="tweet"]');
if (moreTweets.length === 0) {
console.log("No more tweets to process. Exiting...");
break;
}
continue;
}
for (const tweet of tweets) {
const success = await processTweet(tweet);
if (!success) {
console.warn("Moving to the next tweet...");
}
}
console.log("Finished current batch. Scrolling down for more tweets...");
window.scrollBy(0,2* window.innerHeight);
await wait(2000);
}
console.log("Finished processing all tweets.");
};
deleteTweetsSequentially();