-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeV1.js
216 lines (200 loc) · 7.02 KB
/
writeV1.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
(async (args) => {
function select(elem, selection) {
if(selection === "") {
return;
}
elem.dispatchEvent(new MouseEvent("mousedown", { button: 0, bubbles: true }));
elem = document.querySelector("#menu- ul > li[data-value='" + selection + "']");
if(elem !== null) {
elem.click();
}
}
function selectCountry(inputElem, country) {
if(country === "") {
return;
}
const button = inputElem.nextElementSibling.children[1];
button.click();
const menu = document.querySelector(".MuiAutocomplete-popper ul");
const countryItem = document.evaluate("li[text()='" + country + "']", menu).iterateNext();
if(countryItem !== null) {
countryItem.click();
}
}
function input(elem, text) {
if(text === "") {
return;
}
elem.focus();
document.execCommand("insertText", false, text);
elem.dispatchEvent(new Event("focusout", { bubbles: true }));
}
function selectRadio(elem) {
elem.focus();
elem.click();
elem.dispatchEvent(new Event("focusout", { bubbles: true }));
}
function clickForwardButton() {
const buttons = document.querySelectorAll("#\\32 627047 button");
for(const button of buttons) {
const text = button.textContent;
if("Forward" === text || "Eteenpäin" === text) {
button.click();
return;
}
}
console.error("No forward button found");
}
async function awaitElem(elemId) {
for(let attemtps = 0; attemtps < 1000; ++attemtps) {
const elem = document.getElementById(elemId);
if(elem == null) {
await new Promise(r => setTimeout(r, 10));
} else {
return elem;
}
}
throw new Exception(`Element ${elemId} didn't appear. Giving up!`);
}
// Note: Section 1 is the introduction text and contains no data.
async function writeSection2() {
let argsOff = 0;
let elem = await awaitElem("root_6");
console.log("Writing section 2");
const inputs = document.querySelectorAll("#\\32 627047 input");
const notificationType = args[argsOff++];
if(notificationType !== "") {
select(elem, notificationType);
}
input(inputs[1], args[argsOff++]);
input(inputs[2], args[argsOff++]);
input(inputs[3], args[argsOff++]);
input(inputs[4], args[argsOff++]);
input(inputs[5], args[argsOff++]);
selectCountry(inputs[6], args[argsOff++]);
input(inputs[7], args[argsOff++]);
input(inputs[8], args[argsOff++]);
input(inputs[9], args[argsOff++]);
const duration = args[argsOff++];
if(duration !== "") {
input(inputs[10], duration);
select(document.getElementById("root_9"), duration);
if(duration === "yli10") {
input(document.getElementById("root_10_0"), args[argsOff++]);
input(document.getElementById("root_10_1"), args[argsOff++]);
input(document.getElementById("root_10_2"), args[argsOff++]);
input(document.getElementById("root_10_3"), args[argsOff++]);
}
}
if(argsOff < args.length) {
clickForwardButton();
writeSection3(argsOff);
}
}
async function writeSection3(argsOff) {
await awaitElem("root_12_1");
console.log("Writing section 3");
const inputs = document.querySelectorAll("#\\32 627047 input");
input(inputs[0], args[argsOff++]);
input(inputs[1], args[argsOff++]);
input(inputs[2], args[argsOff++]);
input(inputs[3], args[argsOff++]);
input(inputs[4], args[argsOff++]);
selectCountry(inputs[5], args[argsOff++]);
input(inputs[6], args[argsOff++]);
input(inputs[7], args[argsOff++]);
if(argsOff < args.length) {
clickForwardButton();
writeSection4(argsOff);
}
}
async function writeSection4(argsOff) {
let elem = await awaitElem("root_14");
console.log("Writing section 4");
const sector = args[argsOff++];
if(sector !== "") {
select(elem, sector);
}
const construction = args[argsOff++];
if(construction !== "") {
select(document.getElementById("root_15"), construction);
if(construction === "kylla") {
let inputs = document.getElementById("root_16-title").parentElement.querySelectorAll("input");
input(inputs[0], args[argsOff++]);
input(inputs[1], args[argsOff++]);
input(inputs[2], args[argsOff++]);
input(inputs[3], args[argsOff++]);
input(inputs[4], args[argsOff++]);
selectCountry(inputs[5], args[argsOff++]);
inputs = document.getElementById("root_18-title").parentElement.querySelectorAll("input")
input(inputs[0], args[argsOff++]);
input(inputs[1], args[argsOff++]);
input(inputs[2], args[argsOff++]);
input(inputs[3], args[argsOff++]);
input(inputs[4], args[argsOff++]);
selectCountry(inputs[5], args[argsOff++]);
}
}
if(argsOff < args.length) {
clickForwardButton();
writeSection5(argsOff);
}
}
async function writeSection5(argsOff) {
await awaitElem("root_20_3");
console.log("Writing section 5");
let inputs = document.querySelectorAll("#\\32 627047 input,textarea");
input(document.getElementById("root_23"), args[args.length - 1]); // fill in "Additional information"
const workplaces = args[argsOff];
console.log(`Filling in ${workplaces.length} workplace${workplaces.length == 1 ? "" : "s"}`);
let inputsOffset = 0;
if(workplaces.length >= 1) {
[inputs, inputsOffset] = fillWorkplace(inputs, inputsOffset, workplaces[0]);
}
// console.log(`Offset after main workplace: ${inputsOffset}`);
// console.log(inputs[inputsOffset]);
if(workplaces.length > 1) {
selectRadio(inputs[inputsOffset]); // select "Yes": other workspaces to report
inputsOffset += 2; // the "Yes" and "No" radio buttons are two inputs we need to skip
additionalWorkplaces(inputsOffset, workplaces);
} else {
selectRadio(inputs[inputsOffset + 1]); // select "No": no other workspaces to report
}
}
function additionalWorkplaces(inputsOffset, workplaces) {
const buttons = document.getElementById("root_22__title").parentElement.querySelectorAll("button")
const plusWorkplace = buttons[buttons.length - 1];
for(let i = 2; i < workplaces.length; ++i) {
plusWorkplace.click();
}
let inputs = document.querySelectorAll("#\\32 627047 input,textarea"); // must be done after adding workplaces
for(let i = 1; i < workplaces.length; ++i) {
const workplace = workplaces[i];
[inputs, inputsOffset] = fillWorkplace(inputs, inputsOffset, workplace);
}
}
function fillWorkplace(inputs, offset, workplace) {
selectCountry(inputs[offset++], workplace[0]); // this is really the "Municipality" but mechanism is the same
input(inputs[offset++], workplace[1]); // address
input(inputs[offset++], workplace[2]); // loading and unloading location
++offset; // skip hidden textarea
const workers = workplace[3]; // worker list
const plusWorkerButton = document.evaluate("ancestor::table/../following-sibling::button", inputs[offset])
.iterateNext();
if(workers.length > 70) {
for(let i = 10; i < workers.length/7; ++i) {
plusWorkerButton.click();
}
inputs = document.querySelectorAll("#\\32 627047 input,textarea");
}
for(let i = 0; i < workers.length; ++i) {
input(inputs[offset++], workers[i]);
}
if(workers.length < 70) {
offset += 70 - workers.length;
}
return [inputs, offset]
}
document.querySelector("#\\32 627047 button").click();
await writeSection2();
})