forked from luqmanoop/twitter-mass-unfollow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
234 lines (198 loc) · 6.12 KB
/
script.js
File metadata and controls
234 lines (198 loc) · 6.12 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
console.log("::twu init::");
const html = document.querySelector("html");
let timerHandle;
let previousScrollHeight = 0;
let inProgress;
const unFollowedUsers = [];
const unfollowedUsernames = [];
let shared = {}; // shared.js module
(async () => {
const sharedSrc = chrome.runtime.getURL("shared.js");
shared = await import(sharedSrc);
})();
const tmuWrapper = document.createElement("div");
tmuWrapper.style.cssText = `
position: fixed;
right: 20px;
bottom: 70px;
z-index: 10;
display: flex;
visibility: hidden;`.trim();
let totalUnFollowed = document.createElement("h1");
totalUnFollowed.textContent = -1230;
totalUnFollowed.style.cssText = `
background: #1da1f2;
color: #fff;
font-weight: bold;
border-radius: 50%;
margin-right: 10px;
font-size: 16px;
width: 50px;
height: 50px;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
align-self: flex-end;
`;
let img = document.createElement("img");
img.style.width = "80px";
img.alt = "running";
img.src =
"https://github.com/codeshifu/assets/blob/main/gifs/eevee.gif?raw=true";
tmuWrapper.appendChild(totalUnFollowed);
tmuWrapper.appendChild(img);
document.body.appendChild(tmuWrapper);
const showContentInDOM = () => {
tmuWrapper.style.visibility = "visible";
};
const getFollowingsContainer = () => {
return document.querySelector("section[role=region] div");
};
const getFollowings = () =>
Array.from(
document.querySelectorAll(
'section[role=region] [data-testid="UserCell"] [role=button]'
)
);
const getUsername = (followingBtn) => {
return followingBtn
.getAttribute("aria-label")
.toLowerCase()
.replace(/.*@/, "");
};
const filterFollowings = async (followings, unfollowNotFollowing) => {
const whitelistedUsers = await shared.storage.get(shared.whiteListedUsersKey);
const toUnfollow = followings.filter((followingBtn) => {
if (!whitelistedUsers) return true;
const username = getUsername(followingBtn);
return !whitelistedUsers.includes(username);
});
return unfollowNotFollowing
? toUnfollow.filter((following) => {
const followsYou =
!!following.parentElement.parentElement.querySelector(
'[data-testid="userFollowIndicator"]'
);
return followsYou ? false : true;
})
: toUnfollow;
};
const confirmUnfollow = () => {
document
.querySelector("[data-testid=confirmationSheetDialog] div[role=button]")
.click();
};
const unfollow = async (followingButtons = [], demo) => {
for (const followingButton of followingButtons) {
const username = getUsername(followingButton);
if (!unFollowedUsers.includes(username)) {
unFollowedUsers.push(username);
unfollowedUsernames.push(username); // Add username to the array
totalUnFollowed.textContent = `-${unFollowedUsers.length}`;
if (demo) {
followingButton.firstElementChild.firstElementChild.firstElementChild.textContent =
"Follow";
} else {
followingButton.click();
confirmUnfollow();
}
await shared.delay(500);
}
}
};
const scrollFollowingList = async ({ unfollowNotFollowing, demo } = {}) => {
if (
previousScrollHeight !== html.scrollHeight &&
inProgress &&
shared.isExtensionPage()
) {
previousScrollHeight = html.scrollHeight;
const followingsContainer = getFollowingsContainer();
const scrollBy = followingsContainer.clientHeight;
const followings = getFollowings();
const accountsToUnfollow = await filterFollowings(
followings,
unfollowNotFollowing
);
await unfollow(accountsToUnfollow, demo);
html.scroll({
top: followingsContainer.offsetHeight + scrollBy,
behavior: "smooth",
});
await shared.delay(3000);
scrollFollowingList({ unfollowNotFollowing, demo });
}
};
const stopUnfollowing = async () => {
if (!inProgress) return;
downloadUnfollowedUsernames(); // Download the file with unfollowed usernames
inProgress = false;
if (timerHandle) clearInterval(timerHandle);
const shouldReload = await shared.storage.get(shared.reloadOnStoppedKey);
img.src =
"https://github.com/codeshifu/assets/blob/main/gifs/mario_wave.gif?raw=true";
await shared.delay(2000);
if (shouldReload) window.location.reload();
tmuWrapper.style.visibility = "hidden";
img.src =
"https://github.com/codeshifu/assets/blob/main/gifs/eevee.gif?raw=true";
};
const downloadUnfollowedUsernames = () => {
const timestamp = new Date().getTime();
const filename = `unfollowed-${timestamp}.txt`;
const content = unfollowedUsernames.join("\n");
const blob = new Blob([content], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = filename;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(url);
};
const startTimer = () => {
shared.storage.get(shared.timerKey).then((autoStop) => {
if (autoStop) {
timerHandle = setTimeout(() => stopUnfollowing(), 1000 * 60); // 60secs
}
});
};
const run = ({ unfollowNotFollowing, demo } = {}) => {
if (inProgress) return;
inProgress = true;
showContentInDOM();
scrollFollowingList({ unfollowNotFollowing, demo });
startTimer();
};
chrome.runtime.onMessage.addListener((message, sender, reply) => {
try {
switch (message.type) {
case shared.UNFOLLOW_ALL:
run();
return;
case shared.UNFOLLOW_NOT_FOLLOWING:
run({ unfollowNotFollowing: true });
return;
case shared.DEMO:
run({ demo: true });
return;
case shared.STOP:
stopUnfollowing();
return;
case shared.CHECK_IN_PROGRESS:
reply({ payload: previousScrollHeight !== 0 && inProgress });
return;
default:
break;
}
} catch (error) {
console.log(error);
}
});
window.addEventListener("beforeunload", () => stopUnfollowing());
document.body.addEventListener("click", () => {
if (!shared.isExtensionPage() && inProgress) stopUnfollowing();
});