-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_submission_data.js
123 lines (98 loc) · 3.45 KB
/
get_submission_data.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
// Since document is a global singleton, you cannot run `DOMElt.getElementById`,
// this makes querying other xhr-requested pages difficult. Thus they can use
// this helper function instead (which travses the DOM tree.. so it's a little
// bit unoptimized)
// NOTE: domElt must be a domElt, i.e., don't use this on `document`, instead,
// do
// getEltById(document.documentElement, id)
let getEltById = function(domElt, id) {
if (domElt.getAttribute("id") == id) {
return domElt;
}
if (domElt.children) {
for (let i = 0 ; i < domElt.children.length ; i++) {
let res = getEltById(domElt.children[i], id);
if (res) {
return res;
}
}
}
return null;
}
let getPrizes = function(divs) {
for (let i = 0 ; i < divs.length ; i++) {
if (divs[i].getAttribute('id') == 'opt_in_prizes') {
return divs[i];
}
}
return null;
}
let getPrizeList = function(text) {
return text.split(":")[1].trim().split(",");
}
// dev post is ultimately paginated... so we have to scrap page by page
let getSubmissionInfoOfPage = function(page) {
let table =
getEltById(page, 'moderate-submissions')
.getElementsByTagName("tbody")[0];
let submissions = [];
for (let i = 1, row ; row = table.rows[i] ; i++) {
let submission = row.cells[2].getElementsByTagName("div")[0];
let title = submission.getElementsByTagName("p")[0].innerText;
let projectLink =
submission
.getElementsByTagName("p")[0]
.getElementsByTagName("a")[0].href;
let request = new XMLHttpRequest();
// we set async to false, just because performance isn't of much
// importance here...
request.open("GET", projectLink, false);
request.send(null);
let projectPage = document.createElement("html");
projectPage.innerHTML = request.responseText;
// this is annoying... turns out you cannot do '.getElementById' on DOM
// elts
let prizesText = getEltById(projectPage, 'opt_in_prizes').innerText;
let prizes = getPrizeList(prizesText);
console.log({"title": title, "prizes": prizes});
submissions.push({"title": title, "prizes": prizes});
}
return submissions;
}
let getAllPageElts = function() {
let paginationArea = document.getElementsByClassName("pagination")[0];
let otherPagesURLs = new Set([]);
Array.prototype.forEach.call(paginationArea.children, function (elt) {
if (elt.getAttribute("href")) {
otherPagesURLs.add(elt.getAttribute("href"));
}
});
let currentHostname = document.domain;
let pagesElts = [document.documentElement];
otherPagesURLs.forEach(function (path) {
let absPath = "https://" + currentHostname + path;
let r = new XMLHttpRequest();
r.open("GET", absPath, false);
r.send(null);
let pg = document.createElement("html");
pg.innerHTML = r.responseText;
pagesElts.push(pg);
});
return pagesElts;
}
let main = function() {
let pages = getAllPageElts();
let submissions = []
pages.forEach(function (pg) {
let pgSubmissions = getSubmissionInfoOfPage(pg);
pgSubmissions.forEach(function(obj) {
if (obj) {
submissions.push(obj)
}
});
});
return submissions;
}
let result = main();
console.log(result);
result