Skip to content

Commit 59bd8e7

Browse files
committed
Search box
1 parent 862ec31 commit 59bd8e7

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

templates/otp.html.j2

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
<div class="parent">
2+
<div class="search-popup"></div>
23
<div class="timer-container">
34
<div id="timer-line"></div>
45
</div>
56
<div class="otps"></div>
67
</div>
78
<style>
9+
10+
.search-popup {
11+
position: fixed;
12+
top: 10px;
13+
left: 10px;
14+
background-color: #2e2e2e;
15+
border-bottom: 1px solid #d6d6d6;
16+
padding: 5px 10px;
17+
z-index: 1000;
18+
font-size: 18px;
19+
max-width: 90vw;
20+
white-space: nowrap;
21+
overflow: hidden;
22+
text-overflow: ellipsis;
23+
}
24+
825
.otp, .otps, .parent {
926
display: flex
1027
}
@@ -60,7 +77,45 @@
6077

6178
<script>
6279
63-
function spaceOutCode(code) {
80+
let searchQuery = '';
81+
82+
const showSearchPopup = () => {
83+
let popup = document.querySelector('.search-popup');
84+
if (searchQuery === '') {
85+
popup.style.display = 'none'
86+
} else {
87+
popup.style.display = ''
88+
}
89+
popup.textContent = searchQuery;
90+
};
91+
92+
const filterOtps = () => {
93+
const allOtps = document.querySelectorAll('.otp');
94+
const query = searchQuery.toLowerCase();
95+
96+
allOtps.forEach(otp => {
97+
const name = otp.querySelector('.otp_name').textContent.toLowerCase();
98+
otp.style.display = name.includes(query) ? '' : 'none';
99+
});
100+
};
101+
102+
document.addEventListener('keydown', (e) => {
103+
if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
104+
searchQuery += e.key;
105+
showSearchPopup();
106+
filterOtps();
107+
} else if (e.key === 'Backspace') {
108+
searchQuery = searchQuery.slice(0, -1);
109+
showSearchPopup();
110+
filterOtps();
111+
} else if (e.key === 'Escape') {
112+
searchQuery = '';
113+
showSearchPopup();
114+
filterOtps();
115+
}
116+
});
117+
118+
const spaceOutCode = code => {
64119
const len = code.length;
65120
66121
if (len % 2 === 0) {
@@ -70,12 +125,15 @@
70125
const middle = Math.floor(len / 2);
71126
return code.slice(0, middle) + ' ' + code[middle] + ' ' + code.slice(middle + 1);
72127
}
73-
}
128+
};
129+
130+
const updateTimer = () => {
131+
const progress = (30 - (Date.now() / 1000) % 30) / 30 * 100;
132+
document.getElementById('timer-line').style.width = progress + '%';
133+
};
74134
75135
const url_prefix = "{{ url_prefix }}"
76-
const token = "{{ token }}";
77-
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
78-
const socket = new WebSocket(`${proto}://${location.host}${url_prefix}ws?token=${encodeURIComponent(token)}`);
136+
const socket = new WebSocket(`${(location.protocol === 'https:' ? 'wss' : 'ws')}://${location.host}${url_prefix}ws?token=${encodeURIComponent("{{ token }}")}`);
79137
const otpContainer = document.querySelector('.otps');
80138
81139
socket.onmessage = (event) => {
@@ -100,19 +158,16 @@
100158
});
101159
};
102160
});
161+
filterOtps()
103162
};
104163
105164
socket.onclose = () => {
106165
window.location.href = url_prefix;
107166
};
108167
109-
function updateTimer() {
110-
const progress = (30 - (Date.now() / 1000) % 30) / 30 * 100;
111-
document.getElementById('timer-line').style.width = progress + '%';
112-
}
113-
114168
setInterval(updateTimer, 10);
115169
updateTimer();
170+
showSearchPopup();
116171
117172
</script>
118173

0 commit comments

Comments
 (0)