Project

General

Profile

Download (2.71 KB) Statistics
| Branch: | Revision:

hydrilla-fixes-bundle / src / docs-google-com-fix-forms / google_forms.js @ ecc6c218

1
/**
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * (Incomplete) Fix for Google Forms
5
 *
6
 * Copyright © 2021 jahoti <jahoti@tilde.team>
7
 * Copyright 2022 Wojtek Kosior <koszko@koszko.org>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *    http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 *
21
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
22
 * license. Although I request that you do not make use of this code in a way
23
 * incompliant with the license, I am not going to enforce this in court.
24
 */
25

    
26
var form = document.forms[0];
27

    
28
/* Fix form fields. */
29
for (let div of form.querySelectorAll('div[data-params]')) {
30
    var data = JSON.parse('[' + div.dataset.params.substring(4));
31
    var name = 'entry.' + data[0][4][0][0];
32
    var input = div.querySelector('input, textarea');
33

    
34
    if (!input) {
35
	console.error(`cannot enable input ${name}`, div);
36
	continue;
37
    }
38

    
39
    if (input.name === name + '_sentinel') {
40
	/* Handle radio buttons. */
41
	for (const input_div of div.querySelectorAll('[data-value]')) {
42
	    const new_radio = document.createElement('input');
43
	    new_radio.type = 'radio';
44
	    new_radio.name = name;
45
	    new_radio.value = input_div.getAttribute("data-value");
46
	    input_div.replaceWith(new_radio);
47
	}
48
    } else {
49
	input.removeAttribute('disabled');
50
	input.name = name;
51

    
52
	/* Enlarge textareas and make them stand out from mere input fields. */
53
	if (input.tagName === "TEXTAREA") {
54
	    input.style.height = "8em";
55
	    input.style.overflowY = "scroll";
56
	}
57
    }
58
}
59

    
60
/* Remove placeholders in text input fields and textareas. */
61
document.querySelectorAll('[jsname=LwH6nd]').forEach(n => n.remove());
62

    
63
/* Enable the form sumbission button (if any). */
64
for (const submit_but of document.querySelectorAll('[jsname=M2UYVd]'))
65
    submit_but.addEventListener("click", () => form.submit());
66

    
67
/* Enable the "next page" button (if any). */
68
function goToNext()
69
{
70
    var next = document.createElement('input');
71
    next.type = 'hidden';
72
    next.name = 'continue';
73
    next.value = '1';
74
    form.appendChild(next);
75
    form.submit();
76
}
77

    
78
for (const next_but of document.querySelectorAll('[jsname=OCpkoe]'))
79
    next_but.addEventListener("click", goToNext);
80

    
81
// TODO:
82
// * support "back" with instatiation of previous entries
83
// * find and fix form parts that still don't work (if any)
(1-1/2)