-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarklet.js
144 lines (119 loc) · 5.45 KB
/
bookmarklet.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
(function() {
// Check we're on an ATIP request page, or in test mode
const possiblePages = ['atip-aiprp.tbs-sct.gc.ca', 'atip-aiprp.apps.gc.ca', 'localhost:8000'];
if (!possiblePages.includes(window.location.host)) {
alert('ATIPster error: This does not seem to be an ATIP requests page!')
return;
}
// Set up pageType
let pageType;
if (window.location.host === 'atip-aiprp.tbs-sct.gc.ca') {
pageType = 'tbs';
}
if (window.location.host === 'atip-aiprp.apps.gc.ca') {
pageType = 'ircc';
}
if (window.location.host === 'localhost:8000') {
if (document.title === 'Provide contact information - Access to Information and Personal Information Request Service - Canada.ca') {
pageType = 'tbs';
}
if (document.title === 'Access to Information and Privacy (ATIP) Online Request') {
pageType = 'ircc';
}
}
if (!pageType) return;
// Grab the data
const atipsterData = window.atipsterData;
if (!atipsterData) return;
if (atipsterData.requestor === 'Individual/corporation currently present in Canada') {
if (pageType === 'tbs') atipsterData.requestor = 'Individual present in Canada';
if (pageType === 'ircc') atipsterData.requestor = 'Corporation present in Canada';
}
if (['Foreign National present in Canada', 'Foreign National outside of Canada'].indexOf(atipsterData.requestor) != -1) {
if (pageType === 'tbs') atipsterData.requestor = 'Individual present in Canada';
}
// Field mappings
const tbsFormFields = [
{ key: 'input#LastName', value: atipsterData.lastName },
{ key: 'input#FirstName', value: atipsterData.firstName },
{ key: 'input#BusinessOrgName', value: atipsterData.busName },
{ key: 'input#Address', value: `${atipsterData.streetNumber} ${atipsterData.streetName}` },
{ key: 'input#ApartmentSuiteUnitNumber', value: atipsterData.aptNumber },
{ key: 'input#PostOfficeBox', value: atipsterData.poBox },
{ key: 'select#SelectedCountryId', value: 'Canada' }, // default
{ key: 'select#SelectedProvinceId', value: atipsterData.province },
{ key: 'input#City', value: atipsterData.city },
{ key: 'input#PostalCode', value: atipsterData.postal.replaceAll(' ', '') },
{ key: 'input#PhoneNumberCanUs', value: atipsterData.tel },
{ key: 'input#FaxNumberCanUs', value: atipsterData.fax },
{ key: 'fieldset.chkbxrdio-grp', value: atipsterData.requestor }
];
const irccFormFields = [
{ key: 'select#citizenshipCategory', value: atipsterData.reqStatus },
{ key: 'input#familyName', value: atipsterData.lastName },
{ key: 'input#givenName', value: atipsterData.firstName },
{ key: 'input#address.streetNumber', value: atipsterData.streetNumber },
{ key: 'input#address.streetAddress1', value: atipsterData.streetName },
{ key: 'input#address.streetAddress2', value: atipsterData.busName },
{ key: 'input#address.apartmentNumber', value: atipsterData.aptNumber },
{ key: 'input#address.postOfficeBox', value: atipsterData.poBox },
{ key: 'input#address.city', value: atipsterData.city },
{ key: 'select#address.countryCode', value: 'Canada' }, // default
{ key: 'input#address.provinceOrState', value: atipsterData.province },
{ key: 'input#address.postalZipCode', value: atipsterData.postal },
{ key: 'input#telephone', value: atipsterData.tel },
{ key: 'input#fax', value: atipsterData.fax },
{ key: 'input#email', value: atipsterData.email },
{ key: 'input#confirmEmail', value: atipsterData.email },
{ key: 'select#ownBehalf', value: 'Yes' }, // default
{ key: 'select#descriptionCategory', value: atipsterData.requestor }, // requestor category
{ key: 'select#receiveMethod', value: atipsterData.delivery }
];
// Grab proper form fields
const formFields = pageType === 'ircc' ? irccFormFields : tbsFormFields;
function setFieldset(fieldType, value) {
const fieldOptions = {
'Academia': 1,
'Business (private sector)': 2,
'Media': 3,
'Organization': 4,
'Member of the Public': 0,
'Decline to Identify': 5
};
const checkboxes = document.querySelectorAll(`${fieldType} .radio input`);
checkboxes[fieldOptions[value]].checked = true;
}
function setSelect(element, value) {
let valueToAssign = value;
if (pageType === 'ircc') {
const selectedDept = document.getElementById('department').selectedOptions[0].text;
if (selectedDept === 'Immigration, Refugees and Citizenship Canada' && element.id === 'receiveMethod') {
valueToAssign = 'E-mail';
}
}
const optionsText = Array.from(element.options).map(d => d.text.toUpperCase());
element.value = element.options[optionsText.indexOf(valueToAssign.toUpperCase())].value;
}
// Map over the fields and write to them
formFields.map(entry => {
const { key, value } = entry;
const fieldType = key.replace(/#.+/, ''),
elementId = key.replace(/^.+#/, ''),
element = document.getElementById(elementId);
switch (fieldType) {
case 'input':
element.value = value;
break;
case 'select':
setSelect(element, value);
break;
case 'fieldset.chkbxrdio-grp':
setFieldset(fieldType, value);
break;
}
});
const nextBtnSelector = pageType === 'ircc' ? '#btnContinueBottom' : '#contact-information-form > nav > button',
nextBtn = document.querySelector(nextBtnSelector);
// Scroll to next page button
if (nextBtn.scrollIntoView) nextBtn.scrollIntoView({ behavior: 'smooth' });
})();