-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
popup.js
333 lines (278 loc) · 8.92 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* List of Agency Office Codes and their corresponding Department names
*/
var agencyIds = {
"1": "Environmental Protection Agency",
"2": "Energy Department",
"3": "National Oceanic and Atmospheric Administration",
"4": "Occupational Safety and Health Administration",
"5": "National Aeronautics and Space Administration: Science Mission Directorate",
"6": "National Aeronautics and Space Administration: Sciences and Exploration Directorate",
"7": "Agriculture Department",
"8": "Interior Department"
};
function debounce(fn, duration) {
var timeout = null;
return function () {
var scope = this;
var args = arguments;
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(function () {
timeout = null;
fn.apply(scope, args);
}, duration);
};
}
var statusHideTimeout = null;
function showStatus(type, text) {
if (statusHideTimeout) {
clearTimeout(statusHideTimeout);
statusHideTimeout = null;
}
$('#status')
.attr('class', 'status -' + type)
.text(text)
.show();
}
function hideStatus(delay) {
if (statusHideTimeout) {
clearTimeout(statusHideTimeout);
statusHideTimeout = null;
}
if (delay) {
statusHideTimeout = setTimeout(hideStatus, delay);
} else {
$('#status').hide();
}
}
function showUrlWarning(text) {
$('#url-warning').text(text).show();
}
function hideUrlWarning() {
$('#url-warning').hide();
}
function checkUrl() {
var options = {
url: $('#url').val(),
limit: -1,
output: 'json',
fl: 'timestamp'
};
$.getJSON('http://web.archive.org/cdx/search/cdx', options)
.then(function (response) {
if (response.length) {
var headers = response[0];
var rows = response.slice(1);
return rows.map(function (row) {
var match = {};
headers.forEach(function (header, idx) {
var value = row[idx];
switch (header) {
case 'timestamp':
var tokens = /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/.exec(value);
if (tokens) {
match[header] = new Date(
parseInt(tokens[1], 10), // year
parseInt(tokens[2], 10) - 1, // month
parseInt(tokens[3], 10), // day
parseInt(tokens[4], 10), // hours
parseInt(tokens[5], 10), // minutes
parseInt(tokens[6], 10) // seconds
);
} else {
console.error('Invalid timestamp value', value);
}
break;
default:
match[header] = value;
}
});
return match;
});
} else {
return response;
}
})
.then(function (matches) {
hideUrlWarning();
if (matches.length) {
var match = matches[0];
var now = new Date();
var staleDays = 30;
var staleDuration = staleDays * 24 * 60 * 60 * 1000; // days in milliseconds
if (now - match.timestamp < staleDuration) {
showUrlWarning('This URL has already been crawled by Internet Archive in the last ' + staleDays + ' days');
}
}
})
.catch(function (error) {
hideUrlWarning();
console.error('Error looking up URL in CDX', error);
});
}
function checkAgency() {
/* Disable the Department/Agency dropdown bar once the agency code has been entered.
* Changing the code will re-enter the correct department/agency name.
*/
var agencyId = $('#agencyId').val();
var agencyName = agencyIds[agencyId];
if (agencyName) {
$("#agencyName").val(agencyName).prop('disabled', true);
} else {
$("#agencyName").prop('disabled', false);
}
}
function onPageDetailsReceived(pageDetails) {
$('#title').val(pageDetails.title);
$('#url').val(pageDetails.url);
checkUrl();
}
function getCookieValue(url, name) {
return new Promise(function (resolve, reject) {
chrome.cookies.get({
url: url,
name: name
}, function (cookie) {
if (cookie) {
resolve(cookie.value);
} else {
reject(new Error('Could not read cookie: ' + name));
}
});
});
}
function submitToGoogleForms(fields) {
console.log('Submitting to Google Forms');
var url = 'https://docs.google.com/forms/d/1udxf9C7XeO7rm-SoucjDIv0c8XzGb7VVutsCI8r4s-Y/formResponse';
var fieldConversion = {
title: 180917746,
url: 1767515686,
nominatorName: 1227505863,
nominatorEmail: 1975141568,
eventName: 1518336548,
agencyName: 1285343614,
agencyId: 536064408,
subAgencyId: 1706245913,
organizationId: 1076101938,
suborgId: 1768657731,
subprimerId: 615621344,
hasFtp: 365628902,
hasVisualization: 2057247667,
hasManyFiles: 1702958174,
hasDatabase: 15716096,
comments: 2034225983
};
var query = { ifq: '' };
for (var fieldName in fieldConversion) {
var fieldValue = fields[fieldName];
var queryParamName = 'entry.' + fieldConversion[fieldName];
query[queryParamName] = typeof fieldValue === 'boolean'
? fieldValue ? 'Yes' : ''
: fieldValue;
}
return $.get(url, query)
.then(function () {
console.log('Submitted to Google Forms');
});
}
function submitToUntGwda(fields) {
console.log('Submitting to UNT GWDA');
var url = 'http://digital2.library.unt.edu/nomination/GWDA/add/';
var fieldConversion = {
url: 'url_value',
nominatorName: 'nominator_name',
nominatorEmail: 'nominator_email',
eventName: 'nominator_institution'
};
return $.get(url) // Load form page to get CSRF cookie
.then(function () {
console.log('Loaded form page');
return getCookieValue(url, 'csrftoken');
})
.then(function (csrfToken) {
console.log('Got CSRF token');
var body = { csrfmiddlewaretoken: csrfToken };
for (var fieldName in fieldConversion) {
var fieldValue = fields[fieldName];
var bodyParamName = fieldConversion[fieldName];
body[bodyParamName] = fieldValue;
}
return $.post(url, body)
.then(function () {
console.log('Submitted to UNT GWDA');
});
});
}
// POST the data to the server using XMLHttpRequest
function handleSubmit(e) {
e.preventDefault();
var fields = {
url: $('#url').val(),
title: $('#title').val(),
nominatorName: $('#nominatorName').val(),
nominatorEmail: $('#nominatorEmail').val(),
eventName: $('#eventName').val(),
agencyName: $('#agencyName').val(),
agencyId: $('#agencyId').val(),
subAgencyId: $('#subAgencyId').val(),
organizationId: $('#organizationId').val(),
suborgId: $('#suborgId').val(),
subprimerId: $('#subprimerId').val(),
hasFtp: !!$('#hasFtp:checked').val(),
hasVisualization: !!$('#hasVisualization:checked').val(),
hasManyFiles: !!$('#hasManyFiles:checked').val(),
hasDatabase: !!$('#hasDatabase:checked').val(),
comments: $('#comments').val()
};
localStorage.nominatorName = fields.nominatorName;
localStorage.nominatorEmail = fields.nominatorEmail;
localStorage.eventName = fields.eventName;
localStorage.agencyName = fields.agencyName;
localStorage.agencyId = fields.agencyId;
localStorage.subAgencyId = fields.subAgencyId;
localStorage.organizationId = fields.organizationId;
localStorage.suborgId = fields.suborgId;
localStorage.subprimerId = fields.subprimerId;
showStatus('pending', 'Submitting');
$('#submit').prop('disabled', true);
$.when(submitToGoogleForms(fields), submitToUntGwda(fields))
.then(function () {
$('#submit').prop('disabled', false);
showStatus('success', 'URL submitted. Thanks!');
hideStatus(3000);
})
.catch(function (error) {
$('#submit').prop('disabled', false);
showStatus('failure', 'Could not submit URL');
});
}
/* When the popup loads: Autopopulate the name, event name and email if it has been submitted before,
* i.e. if localStorage has these fields already saved.
*/
$(function () {
$('#nominatorName').val(localStorage.nominatorName);
$('#nominatorEmail').val(localStorage.nominatorEmail);
$('#eventName').val(localStorage.eventName);
$('#agencyName').val(localStorage.agencyName);
$('#agencyId').val(localStorage.agencyId);
$('#subAgencyId').val(localStorage.subAgencyId);
$('#organizationId').val(localStorage.organizationId);
$('#suborgId').val(localStorage.suborgId);
$('#subprimerId').val(localStorage.subprimerId);
checkAgency();
$('#agencyId').change(checkAgency);
$('#url').on('keyup paste change', debounce(checkUrl, 500));
// Focus first field for a11y
$('#title').focus();
hideUrlWarning();
hideStatus();
$('#form').submit(handleSubmit);
// Get the event page
chrome.runtime.getBackgroundPage(function (eventPage) {
// Call the getPageInfo function in the event page, passing in
// our onPageDetailsReceived function as the callback. This injects
// content.js into the current tab's HTML
eventPage.getPageDetails(onPageDetailsReceived);
});
});