-
Notifications
You must be signed in to change notification settings - Fork 0
/
supporterlistermajigger.html
229 lines (207 loc) · 8.97 KB
/
supporterlistermajigger.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Supporter Listermajigger</h1>
<h2>by <a href="https://github.com/Zro617">Zro617</a></h2>
<p>Automatic page crawling of a suggestion topic to find who supports and who doesn't.</p>
<p>Enter a topic's ID or URL:</p>
<input type="textarea" id="topic" value="" style="width:200px;display:block"></input><br>
<button id="get" onclick="get()" style="display:block">Find muh supporters</button>
<button id="copy" onclick="copy()" style="display:none">Get copyable text pls</button>
<p id="msg" style="display:none">ohai dere mr. inspect element</p>
<div id="results">
<p id="list1" style="display:none"></p>
<ul id="supporters"></ul>
<p id="list2" style="display:none"></p>
<ul id="nonsupporters"></ul>
<p id="list3" style="display:none"></p>
<ul id="undecided"></ul>
</div>
</body>
<script type="text/javascript" id="main">
var result, op;
function init() {
result = { supporters: [], nonsupporters: [], unknown: [] };
op = "";
document.getElementById("list1").innerHMTL = "No supporters found.";
document.getElementById("list2").innerHTML = "No nonsupporters found.";
document.getElementById("list3").innerHTML = "No posts found?";
clear();
}
function clear() {
// clear all unsorted list elements
var lists = document.getElementsByTagName("ul");
for (var l in lists) while (lists[l].firstChild) lists[l].removeChild(lists[l].firstChild);
}
function message(text) {
document.getElementById("msg").innerHTML = text;
document.getElementById("msg").style.display = "block";
}
function getTopic() {
var topic = document.getElementById("topic").value;
try {
topic = /topic\/(\d+)/.exec(topic)[1];
} catch (e) {
// TODO: Catch more invalid urls
topic = Number(topic);
}
return topic;
}
function get() {
init();
var topicId = getTopic();
if (!topicId) {
message("That is not a valid topic URL or ID.");
return;
}
message("Sending requests...");
document.getElementById("copy").style.display = "none";
document.getElementById("list1").style.display = "none";
document.getElementById("list2").style.display = "none";
document.getElementById("list3").style.display = "none";
request(topicId, 1);
}
function request(id, page) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://scratch.mit.edu/discuss/topic/" + id + "/?page=" + page, true);
xhr.responseType = "document";
xhr.onload = function () {
if (xhr.status == 200 && xhr.response.getElementsByClassName("blockpost roweven firstpost").length > 0) {
process(xhr.response, page);
request(id,page+1);
} else {
list();
}
};
xhr.send();
}
function process(dom, page) {
// find usernames and text content
var users = dom.getElementsByClassName("black username");
var posts = dom.getElementsByClassName("post_body_html");
if (posts.length == 0) return console.log("No posts found on page " + page);
if (users.length != posts.length) return console.error("# of users found does not match # of posts found.");
if (page == 1) {
op = users[0].innerHTML; // original post
result.supporters.push(op);
}
for (var user, post, text, si, ni, ui, regex, test, i = 0; i < users.length; i++) {
user = users[i].innerHTML;
// ignore all posts by the OP
if (user != op) {
post = posts[i];
// get actual text content of post
// if a post is just blockstuff, retain original post anyways
text = justtextpls(post).trim() || post.innerHTML;
quotes = post.getElementsByTagName("blockquote");
// Check for "agreement" quoting (NOT TESTED YET)
// posts that contain nothing in their body except a quote,
// or that contain a common phrase like "as per this" will
// use the quoted text instead
regex = /^(as per this|(?:i )?agreed?|\^+|\+\d+)$/gi;
while (quotes.length && (text=="" || regex.test(text))) {
post = quotes[0];
text = justtextpls(post).trim();
quotes = post.getElementsByTagName("blockquote");
}
si = result.supporters.indexOf(user),
ni = result.nonsupporters.indexOf(user),
ui = result.unknown.indexOf(user);
// now we have our important text, let's test it
regex = /((?!you(?:'re|r)? )(?:not?|don'?t) )?(support|62807079593)(?!(?!.*[;:,.!]).*\?)/gi;
// do not match "support" if it's inside a question
// p.s. the long number thing is supposed to be support in base 10 :P
test = regex.exec(text);
if (test) {
if (test[1]) {
// first capture group is the "no..." part
if (si > -1) {
result.supporters.splice(si, 1);
console.log("Support to No Support: " + user);
}
if (ui > -1) {
result.unknown.splice(ui, 1);
console.log("Undecided to No Support: " + user);
}
if (ni == -1) {
result.nonsupporters.push(user);
console.log("Non-Supporter | " + user + ". '" + HtmlToString(post) + "'");
}
} else if (test[2]) {
// second capture group is the "support" part, only useful when "no..." wasn't matched
if (ni > -1) {
result.nonsupporters.splice(ni, 1);
console.log("No Support to Support: " + user);
}
if (ui > -1) {
result.unknown.splice(ui, -1);
console.log("Undecided to Support: " + user);
}
if (si == -1) {
result.supporters.push(user);
console.log("Supporter | " + user + ". '" + HtmlToString(post) + "'");
}
}
} else if (si == -1 && ni == -1 && ui == -1) {
// no match and not in lists
result.unknown.push(user);
console.log("Undecided | " + user + ". '" + HtmlToString(post) + "'");
} else {
// no match but already listed
console.log("Ignored | " + user + ". '" + HtmlToString(post) + "'");
}
}
}
}
function list() {
clear();
var u, ul;
if (result.supporters.length) {
ul = "";
for (u = 0; u < result.supporters.length; u++) ul += "<li>" + result.supporters[u] + "</li>";
document.getElementById("list1").innerHTML = "Supporters (" + result.supporters.length + "):";
document.getElementById("supporters").innerHTML = ul;
}
if (result.nonsupporters.length) {
ul = "";
for (u = 0; u < result.nonsupporters.length; u++) ul += "<li>" + result.nonsupporters[u] + "</li>";
document.getElementById("list2").innerHTML = "Non-Supporters (" + result.nonsupporters.length + "):";
document.getElementById("nonsupporters").innerHTML = ul;
}
if (result.unknown.length) {
ul = "";
for (u = 0; u < result.unknown.length; u++) ul += "<li>" + result.unknown[u] + "</li>";
document.getElementById("list3").innerHTML = "Undecided (" + result.unknown.length + "):";
document.getElementById("undecided").innerHTML = ul;
}
document.getElementById("list1").style.display = "block";
document.getElementById("list2").style.display = "block";
document.getElementById("list3").style.display = "block";
document.getElementById("copy").style.display = "block";
message("Done.");
}
function copy() {
var t = "";
t += "[b]Supporters (" + result.supporters.length + "):[/b]\n" + result.supporters.join(", ") + "\n\n";
t += "[b]Non-Supporters (" + result.nonsupporters.length + "):[/b]\n" + result.nonsupporters.join(", ") + "\n\n";
t += "[b]Undecided (" + result.unknown.length + "):[/b]\n" + result.unknown.join(", ");
window.open("data:text/plain;base64," + btoa(t));
}
function justtextpls(element) {
return element.cloneNode(false).innerHTML;
}
function HtmlToString(obj) {
if (typeof obj === 'object' && navigator.userAgent.indexOf("Chrome") > -1) return obj.innerHTML;
else return obj;
}
Array.prototype.clear = function () { this.length = 0; };
Array.prototype.empty = function () { return this.length == 0; };
Array.prototype.pushNew = function (e) { if (this.indexOf(e) == -1) this.push(e); };
// event listeners for jsfiddle
//document.getElementById("get").addEventListener('click', get);
//document.getElementById("copy").addEventListener('click', copy);
</script>
</html>