1 const TAG = '[DeviceHome][pincode]';
2 const serverPort = 9000;
3 const serverURL = window.location.protocol + '//' + window.location.hostname;
7 var dots = document.querySelectorAll('.dot'), numbers = document.querySelectorAll('.number');
10 function preloadKey() {
11 var xhr = new XMLHttpRequest();
12 xhr.onload = function () {
13 if (xhr.status === 200 || xhr.status === 201) {
14 publicKey = xhr.responseText;
18 console.error('[WT] Failed to get public key.');
21 console.error(xhr.responseText);
24 xhr.open('GET', serverURL + ':' + serverPort + '/pincode/publicKey');
29 dots = Array.prototype.slice.call(dots);
30 numbers = Array.prototype.slice.call(numbers);
31 numbers.forEach(function (number, index) {
32 number.addEventListener('click', function () {
33 number.className += ' grow';
34 if (number.innerHTML === '0') {
39 dots[input.length - 1].className += ' active';
40 if (input.length >= 4) {
41 console.log(`${TAG} input: ${input}`);
44 setTimeout(function () {
45 dots.forEach(function (dot, index) {
46 dot.className = 'dot';
50 setTimeout(function () {
51 document.body.className = '';
54 setTimeout(function () {
55 number.className = 'number';
61 async function sendPinCode(data) {
62 var encrypt = new JSEncrypt();
63 encrypt.setPublicKey(publicKey);
64 var xhr = new XMLHttpRequest();
65 xhr.onload = function () {
66 if (xhr.status === 200 || xhr.status === 201) {
67 console.log(`result : ${xhr.responseText}`);
68 chkPinCode(xhr.responseText === 'true' ? true : false);
70 console.error(xhr.responseText);
73 xhr.open('POST', serverURL + ':' + serverPort + '/pincode/pinCodeToServer');
74 xhr.setRequestHeader("Content-Type", "application/json");
75 var data = encrypt.encrypt(data);
76 console.log(`${TAG} data: ${data}`);
77 xhr.send(JSON.stringify({ pincode: data.toString("utf8") }));
80 function generatePinCodeNumber() {
81 var xhr = new XMLHttpRequest();
82 xhr.onload = function () {
83 if (xhr.status === 200 || xhr.status === 201) {
84 console.log(xhr.responseText);
86 console.error(xhr.responseText);
89 xhr.open('GET', serverURL + ':' + serverPort + '/retryPinCode');
93 function chkPinCode(returnVal) {
95 dots.forEach(function (dot, index) {
96 dot.className += ' correct';
98 document.body.className += ' correct';
100 setTimeout(function () {
105 dots.forEach(function (dot, index) {
106 dot.className += ' wrong';
108 document.body.className += ' wrong';
110 if (tryCount === 5) {
112 generatePinCodeNumber();
113 setTimeout(function () {
114 alert('Failed to input 5 times. A new Pincode has been generated, so check the TV notification.');
121 window.location.href = serverURL + ':' + serverPort + '/client/client.html';
124 window.onload = function () {