1
|
/**
|
2
|
* SPDX-License-Identifier: Apache-2.0
|
3
|
*
|
4
|
* Copyright © 2021 jahoti <jahoti@tilde.team>
|
5
|
*
|
6
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
* you may not use this file except in compliance with the License.
|
8
|
* You may obtain a copy of the License at
|
9
|
*
|
10
|
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
*
|
12
|
* Unless required by applicable law or agreed to in writing, software
|
13
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
* See the License for the specific language governing permissions and
|
16
|
* limitations under the License.
|
17
|
*/
|
18
|
|
19
|
function submitFormItem() {
|
20
|
var name, val, queryString = '', xhr = new content.XMLHttpRequest();
|
21
|
for (var formItem of this.querySelectorAll('select, input:not([type="radio"]):not([type="checkbox"])' +
|
22
|
':not([type="submit"]):not([type="reset"])')) {
|
23
|
queryString += (queryString && '&') + formItem.name + '=' + encodeURIComponent(formItem.value);
|
24
|
}
|
25
|
|
26
|
xhr.onreadystatechange = function () {
|
27
|
if (this.readyState === 4) {
|
28
|
if (this.status === 200) location.href = JSON.parse(this.responseText).follow_up_url;
|
29
|
else if (this.status === 422) {
|
30
|
var failMessage = [], response = JSON.parse(this.responseText);
|
31
|
for (field in response.errors) for (error of response.errors[field]) {
|
32
|
failMessage.push('Field "' + field + '" ' + error);
|
33
|
}
|
34
|
alert(failMessage.join('\n'));
|
35
|
}
|
36
|
else alert('Submission failed: response code ' + this.status);
|
37
|
}
|
38
|
}
|
39
|
|
40
|
xhr.open('POST', this.action, true); // Manually add the domain, as it's not properly handled in extensions
|
41
|
xhr.setRequestHeader('X-CSRF-Token', csrf);
|
42
|
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
|
43
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
44
|
xhr.send(queryString);
|
45
|
return false;
|
46
|
}
|
47
|
|
48
|
// Apply CSS as necessary
|
49
|
if (notice = document.querySelector('#petition-bar-main > span')) notice.style.display = 'none'; // Hide the totally mistaken (even without this extension) anti-anti-JS warning
|
50
|
document.querySelector('.script-dependent').style.display = 'block';
|
51
|
document.querySelector('.button-wrapper').style.position = 'static'; // Stop the "submit" button obscuring the form
|
52
|
|
53
|
|
54
|
|
55
|
csrf = document.querySelector('meta[name="csrf-token"]').content
|
56
|
for (var button of document.querySelectorAll('button[type="submit"].button.action-form__submit-button')) button.form.onsubmit = submitFormItem;
|