-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
139 lines (118 loc) · 3.35 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="#" />
<style>
.center {
height:90vh;
position: relative;
}
.center a {
font-family: Tahoma, Verdana, Arial, sans-serif;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
color: grey;
}
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid gray;
}
</style>
</head>
<body>
<a href="https://github.com/nikilp/QR-Code-from-Cookie" class="gray">source</a>
<div class="center">
<a>
<h2>Machine ID is:</h2>
<div id="currentMachineId"></div>
<h2>Set Machine ID</h2>
<form id="setCookieForm" onsubmit="return getMachineId();">
<input name="size" type="text" id="machineId" autocomplete="off">
<br><br>
<input type="submit" id="submitBtn" value="Clear">
</form>
</a>
</div>
</body>
<script>
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=./";
}
function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;';
}
document.getElementById("setCookieForm").onsubmit =
function getMachineId() {
var machineId = document.getElementById("machineId").value;
if (machineId === "") {
eraseCookie("machineId");
location.reload();
} else {
setCookie("machineId", machineId, 1000000);
window.location.replace("./qr.html");
}
return false;
}
machineIdCookie = getCookie("machineId");
if (machineIdCookie === null) {
document.getElementById("currentMachineId").innerHTML = "undefined";
document.getElementById("currentMachineId").style.color = "LightSalmon";
} else {
document.getElementById("currentMachineId").innerHTML = machineIdCookie;
}
const input = document.querySelector('#machineId')
input.addEventListener('input', evt => {
const value = input.value
const submitBtn = document.getElementById("submitBtn")
if (!value) {
input.dataset.state = ''
if (machineIdCookie == null) {
submitBtn.value = "Cleared";
submitBtn.disabled = true;
} else {
submitBtn.value= "Clear";
submitBtn.disabled = false;
}
return
}
const trimmed = value.trim()
if (trimmed) {
input.dataset.state = 'valid';
submitBtn.value= "Change";
submitBtn.disabled = false;
} else {
input.dataset.state = 'invalid';
submitBtn.value= "Clear";
submitBtn.disabled = false;
submitBtn.disabled = true;
}
})
if (input.dataset.state === undefined && machineIdCookie == null) {
submitBtn.value = "Cleared";
submitBtn.disabled = true;
}
</script>
</html>