-
Notifications
You must be signed in to change notification settings - Fork 1
/
inject.js
144 lines (116 loc) · 3.92 KB
/
inject.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
// ==UserScript==
// @name Presence converter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Convert the days from JSON to the presence calculator form
// @author You
// @match https://eservices.cic.gc.ca/rescalc/rescalcwizard.do
// @run-at document-end
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
var destination;
var from;
var to;
var reason;
var submit;
function fetchJSON(fn) {
var jsonUrl = "http://127.0.0.1:8080/travel.json";
fetch(jsonUrl)
.then((res) => res.json())
.then((json) => fn && fn(json))
.catch(() => alert("Couldn't download/parse the JSON file!"));
}
function parseData(json) {
var results = [];
var didEnterCanada = false;
for (var i = 0; i < json.length; ++i) {
var item = json[i];
if (item.Country == "Canada") {
didEnterCanada = true;
continue;
}
if (!didEnterCanada) {
continue;
}
var result = {
country: item.Country,
from: item.From,
to: item.To,
why: "City: " + item.City + ". Reason: " + item.Reason + "."
};
var lastItem = null;
var lastIndex = -1;
var otherPlaces = [];
for (var j = i+1; j < json.length; ++j) {
var nextItem = json[j];
if (nextItem.Country == "Canada") {
break;
}
lastItem = nextItem;
lastIndex = j;
var otherPlace = nextItem.Country + " (" + nextItem.City + ")";
if (otherPlaces.indexOf(otherPlace) < 0) {
otherPlaces.push(otherPlace);
}
}
if (lastItem) {
result.to = lastItem.To;
result.why += " Other stops: " + otherPlaces.join(", ") + ".";
i = lastIndex;
}
results.push(result);
}
return results;
}
function addAbsence(country, f, t, why) {
destination.value = findCountry(country);
from.value = f;
to.value = t;
reason.value = why;
submit.click();
}
function findCountry(country) {
if (country == "USA") {
country = "United States of America";
}
else if (country == "Netherlands") {
country = "Netherlands, The";
}
// [expand this as needed if you are seeing blank countries in the generated list]
var options = destination.children;
for (var i = 0; i < options.length; ++i) {
var option = options[i];
if (option.innerText.trim() == country) {
return option.getAttribute("value");
}
}
}
function main() {
var counter = GM_getValue('counter', 0);
if (counter == 0 && !confirm("Run presence converter?")) {
return;
}
destination = document.getElementById("absenDestination");
from = document.getElementById("absenceFromDate");
to = document.getElementById("absenceToDate");
reason = document.getElementById("absencesReason");
submit = document.querySelector('input[name="add.abs"]');
fetchJSON((json) => {
var absences = parseData(json);
console.log(absences);
if (counter < absences.length) {
GM_setValue('counter', counter + 1);
var a = absences[counter];
addAbsence(a.country, a.from, a.to, a.why);
}
else {
GM_setValue('counter', 0);
alert("Done! " + absences.length + " absences reported!");
}
});
}
main();
})();