aboutsummaryrefslogtreecommitdiff
path: root/js/script.js
blob: 523d26b1ef0e6047d3925417b9571443f4f84511 (plain)
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
var codes = new Array();
var urls = new Array();
var hints = new Array();

function createSearchQMs(file) {
	let rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
				let allText = rawFile.responseText;
				myFunc(allText);
            }
        }
    }
    rawFile.send(null);
}

function myFunc(text) {
	let lines = text.split('\n');
	for (let i=0; i < lines.length - 1; i++) { // -1 to avoid extra undefined line
		let parts = lines[i].split('\'');
		codes[i] = parts[1];
		urls[i] = parts[3];
		hints[i] = parts[4].split('#')[1];
		let qms = document.getElementById("qms");
		let newP = document.createElement("p");
			newP.id = "p" + i;
		let newInput = document.createElement("input");
			newInput.id = "code" + i;
			newInput.type = "button";
			newInput.value = codes[i];
			newInput.onclick = function() {createInput(i)};
		newP.appendChild(newInput);
		qms.appendChild(newP);
	}
}

function createInput(id) {
	// Delete all search boxes
	let sbs = document.getElementsByClassName("sb");
	for (let i = 0; i < sbs.length; i++) {
		sbs[i].parentNode.removeChild(sbs[i]);
	}
	// Clean big blue p bg
	let psbs = document.getElementsByClassName("psb");
	for (let i = 0; i < psbs.length; i++) {
		psbs[i].classList.remove("psb");
	}
	// Create corresponding search box
	let par = document.getElementById("p"+id);
		par.className = "psb";
	let sb = document.createElement("input");
		sb.id = "sb" + id;
		sb.className = "sb";
		sb.type = "text";
		sb.placeholder = hints[id];
		sb.onkeydown = function() {
			if (event.keyCode === 13) {
				search(id);
			}
		};
	par.appendChild(sb);
	// Focus the search box
	document.getElementById("sb"+id).focus();
}

function search(id) {
	let sb = document.getElementById("sb"+id);
	url = urls[id].replace("{}", sb.value);
	window.location.assign(url);
}