-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete-reddit.js
149 lines (132 loc) · 3.48 KB
/
delete-reddit.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
/**
* Usage:
* 1. Open up your reddit user profile and switch over to the comments tab to exclude your posts.
* 2. Then paste this code into your browser console and hit enter.
*
* It will simulate all the user clicks you would do if you were deleting your comments manually.
*/
(function () {
/**
* Gets the options button of the first comment on the page
*/
function getOptionsButton() {
return document.querySelector('[aria-label="more options"]');
}
/**
* Gets the delete button within the comment options menu
*/
function getDeleteButton() {
return document.querySelector('div[role~="menu"][style~="left:"] > button:last-child');
}
/**
* Gets the confirm delete button of the modal that appears when deleting a comment
*/
function getConfirmDeleteButton() {
return document.querySelector('div[aria-modal="true"] button:nth-child(2)');
}
/**
* This will click the ""..."" options button next to the current comment
*/
function getOptions() {
return new Promise((resolve, reject) => {
var optionsButton = getOptionsButton();
if(optionsButton === null) {
reject("No options button");
} else {
optionsButton.click();
window.setTimeout(() => {
resolve(optionsButton);
}, 100);
}
});
}
/**
* This will click the delete option from the comment options menu.
*/
function deletePost() {
return new Promise((resolve, reject) => {
var deleteButton = getDeleteButton();
if(deleteButton === null) {
reject("No delete button");
} else {
deleteButton.click();
window.setTimeout(() => {
resolve(deleteButton);
}, 600);
}
});
}
/**
* This will click the confirm delete button in the modal that appears when you try to delete a comment.
*/
function confirmDelete() {
return new Promise((resolve, reject) => {
var confirmButton = getConfirmDeleteButton();
if(confirmButton === null) {
reject("No confirm delete button");
} else {
confirmButton.click();
window.setTimeout(() => {
resolve(confirmButton);
}, 900);
}
});
}
/**
* Gets the state of the page in order to know what action to take next
*/
function getState() {
if(getConfirmDeleteButton() !== null) {
return "modal";
}
if (getDeleteButton() !== null) {
return "options";
}
if (getOptionsButton() !== null) {
return "initial";
}
return "done";
}
/**
* Sometimes the delays between each action is off and you get stuck trying to open
* the comment options when really the modal is open waiting for you to confirm deletion.
* This method takes a step back to see what state you are in and tries to get you back on track.
*/
function getUnstuck() {
window.setTimeout(() => {
var state = getState();
if(state === "done") {
console.log("Done.");
return;
}
console.log("Getting unstuck from state:", state);
if (state === "modal") {
confirmDelete().then(doDelete);
return;
}
if (state === "options") {
deletePost().then(confirmDelete).then(doDelete);
return;
}
if (state === "initial") {
doDelete();
}
}, 500);
}
/**
* This is the main loop that controls the comment deletion process.
*/
function doDelete() {
getOptions()
.then(deletePost)
.then(confirmDelete)
.then(() => {
// Everything went ok, repeat the process
setTimeout(doDelete, 500);
}).catch(() => {
// something went wrong. Try to detect current state and get unstuck
getUnstuck();
});
}
doDelete();
}());