-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
140 lines (121 loc) · 5.15 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
140
<html>
<head>
<title>Search Turkish Subtitles</title>
<style>
html { font-family: 'helvetica'; color: #555 }
b { font-weight: normal }
a { text-decoration: none; color: inherit; }
a:hover { text-decoration: underline }
#input-container { text-align: center; margin: 30px; }
input { padding: 8px 10px; font-size: 16px; border-radius: 5px; outline: none; border: 1px solid #5463FF; color: #5463FF}
button { font-size: 16px; padding: 8px 10px; border: 1px solid #5463FF; border-radius: 5px; background-color: white; cursor: pointer; color: #5463FF;}
button:hover { color: white; background-color: #5463FF; }
#next-button { width: 300px; display: none; margin: 30px auto 100px; }
#result-count { font-size: 12px; color: #bbb; margin-top: 10px;}
#results { margin: 50px 0px; display: flex; flex-wrap: wrap; justify-content: center;}
.match { color: #E04DB0; font-weight: bold;}
.highlight { width: 480px; margin: 50px 30px;}
.title { font-weight: bold; text-align: center; margin-bottom: 20px; font-size: 14px; display: block; color: #332FD0;}
.text { font-size: 16px; margin: 10px 20px; border-radius: 5px; padding: 10px 15px; line-height: 1.5em; display: flex;}
.text:nth-child(odd) { background-color: #e8f2ff }
.show { display: block !important }
.time { font-size: 12px; opacity: 0.5; margin-right: 10px;}
</style>
</head>
<body>
<div id="input-container">
<input id="search-input" type="text" autofocus />
<button id="search-button">Search</button>
<div id="result-count"></div>
</div>
<div id="results"></div>
<button id="next-button">Next</button>
<script>
const searchButton = document.getElementById('search-button');
const nextButton = document.getElementById('next-button');
const input = document.getElementById('search-input');
const resultContainer = document.getElementById('results');
const resultCount = document.getElementById('result-count');
let pageNumber = 1;
const convertTimeStringToSeconds = (time) => {
// 1:32:47
const [hours, minutes, seconds] = time.split(':');
return parseInt(hours)*3600 + parseInt(minutes)*60 + parseInt(seconds);
}
const incrementPage = () => pageNumber += 1;
const showNextButton = () => nextButton.classList.add('show');
const hideNextButton = () => nextButton.classList.remove('show');
const makeNetflixUrl = (videoId, seconds) => `https://netflix.com/watch/${videoId}?t=${seconds}`;
const makeLineHTML = (line, videoId) => {
const timeSeconds = convertTimeStringToSeconds(line.time)
const netflixUrl = makeNetflixUrl(videoId, timeSeconds)
return (`
<div class='text' data-time="${line.time}">
<a class="time" href=${netflixUrl} target="_blank">${line.time}</a>
<div>${line.text}</div>
</div>
`)
}
const submitQuery = () => {
const queryString = input.value.trim();
const request = new XMLHttpRequest();
const url = new URL("http://localhost:8080/search");
url.searchParams.set('q', queryString);
url.searchParams.set('page', pageNumber);
request.responseType = 'json';
request.open("GET", url, true);
request.onload = function() {
if (this.status != 200) {
alert(`Error ${this.status}: ${this.statusText}`);
}
else {
const {hits, pageNumber, totalPages} = this.response;
incrementPage()
scroll(0, 0)
pageNumber === totalPages ? hideNextButton() : showNextButton()
resultContainer.innerHTML = '';
resultCount.innerHTML = `${hits.length} results | ${pageNumber} of ${totalPages}`;
for (const hit of hits) {
const {title, videoId, isDubbed, startTime, lines} = hit;
if (lines.length === 0) {
continue
}
const startTimeSeconds = convertTimeStringToSeconds(startTime)
const netflixUrl = makeNetflixUrl(videoId, startTimeSeconds)
let lineHTML = ''
for (const line of lines) {
lineHTML += makeLineHTML(line, videoId)
}
console.log(lineHTML)
resultContainer.innerHTML += `
<div class="highlight">
<a class="title" href="${netflixUrl}" target="_blank">
${title}
${isDubbed ? '' : '🇹🇷'}
</a>
${lineHTML}
</div>
`
}
}
}
request.onerror = function() {
alert("Request failed.")
}
request.send()
}
searchButton.addEventListener("click", () => {
pageNumber = 1;
submitQuery();
});
nextButton.addEventListener("click", submitQuery)
input.addEventListener("keyup", (event) => {
event.preventDefault();
if (event.keyCode === 13) {
pageNumber = 1
submitQuery();
}
})
</script>
</body>
</html>