-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
143 lines (125 loc) · 4.2 KB
/
popup.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
let sendMessagePromise = function(request, callback) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(request, res => {
callback(res);
resolve();
});
});
};
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
let contentDiv = document.getElementById("content");
let generateBtn = document.getElementById("gencsvs");
let loadingDiv = document.createElement("div");
let loading = document.createElement("p");
loading.innerText = "Loading...";
loadingDiv.appendChild(loading);
let getSpreadsheets = function(results) {
let csvContent = "data:text/csv;charset=utf-8,";
for (let i = 0 ; i < results.length ; i++) {
let escapedTitle = results[i]['title']
.replace(/"/g, "\\\"");
let row = `${i}, "${escapedTitle}"`;
csvContent += row + "\r\n";
}
let encodedURI = encodeURI(csvContent);
let p = document.createElement("p");
let link = document.createElement("a");
link.setAttribute("href", encodedURI);
link.setAttribute("download", "table_numbers.csv");
link.setAttribute("class", ['download-links'])
link.innerText = "table_numbers.csv";
p.appendChild(link);
contentDiv.appendChild(p);
};
let getPrizeCsvs = function(results) {
let prizeParticipants = {};
for (let i = 0 ; i < results.length ; i++) {
let project = results[i]['title'];
let prizes = results[i]['prizes'];
prizes.forEach(function(prize) {
if (!(prize in prizeParticipants)) {
prizeParticipants[prize] = "data:text/csv;charset=utf-8,";
}
prizeParticipants[prize] += `${i}, ${project}\r\n`;
});
}
for (let prize in prizeParticipants) {
if (prize) {
let encodedURI = encodeURI(prizeParticipants[prize]);
let p = document.createElement("p");
let link = document.createElement("a");
link.setAttribute("href", encodedURI);
link.setAttribute("download", prize + ".csv");
link.setAttribute("class", ['download-links'])
link.innerText = prize + ".csv";
p.appendChild(link);
contentDiv.appendChild(p);
}
}
};
let downloadAllCsvs = function(results) {
let downloadable_links = document.getElementsByClassName("download-links");
for (let i = 0 ; i < downloadable_links.length ; i++) {
downloadable_links[i].click();
}
}
let finished = false;
let checkFinished = isFinished => {
if (isFinished) {
finished = isFinished
}
else {
sleep(500).then(() => {
chrome.runtime.sendMessage(
{contentScriptQuery : 'isFinished'},
checkFinished
)});
}
};
let sleepUntilFinished = (callback) => {
if (finished) {
callback();
}
else {
sleep(500).then(() => sleepUntilFinished(callback));
}
}
let genCsv = () => {
chrome.runtime.sendMessage({contentScriptQuery: 'getSubmissions'},
result => {
console.log("HOME STRETCH");
console.log(result);
loadingDiv.remove();
getSpreadsheets(result);
getPrizeCsvs(result);
let btn = document.createElement("button");
btn.onclick = downloadAllCsvs;
btn.innerText = "Download All CSVs";
contentDiv.appendChild(btn);
});
}
generateBtn.onclick = function(element) {
chrome.runtime.sendMessage({contentScriptQuery : 'reset'},
() => {
console.log("RESETED");
contentDiv.appendChild(loadingDiv);
chrome.tabs.query(
{active: true, currentWindow: true},
function(tabs) {
chrome.tabs.executeScript(
tabs[0].id,
{file: "get_submission_data.js"},
r => {
// will run until the thing is finished
chrome.runtime.sendMessage(
{contentScriptQuery : 'isFinished'},
checkFinished);
sleep(500).then(() => sleepUntilFinished(genCsv));
}
)
}
);
});
};