-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathscript.js
171 lines (138 loc) · 6.75 KB
/
script.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
document.addEventListener('DOMContentLoaded', () => {
const frmConvert = document.getElementById('frm-convert');
const removeButton = document.getElementById('remove');
const frmSearch = document.getElementById('frm-search');
const frmInfo = document.getElementById('frm-info');
frmConvert.addEventListener('submit', (e) => {
e.preventDefault();
const submitButton = frmConvert.querySelector("button[type=submit]");
const link = document.getElementById('link').value;
const format = document.getElementById('format').value;
submitButton.classList.add("disabled");
submitButton.innerHTML = "<i class=\"fas fa-spin fa-sync-alt\"></i> Converting...";
const endpoint = `${frmConvert.getAttribute("action")}?youtubelink=${link}&format=${format}`;
fetch(endpoint)
.then(response => response.json())
.then(data => handleConvertResponse(data, submitButton));
});
removeButton.addEventListener('click', () => {
const id = removeButton.getAttribute('data-id');
fetch(`convert.php?delete=${id}`)
.then(response => response.json())
.then(data => alert(data.message));
});
frmSearch.addEventListener('submit', (e) => {
e.preventDefault();
const submitButton = frmSearch.querySelector("button[type=submit]");
const query = document.getElementById('q_search').value;
const maxResults = document.getElementById('max_results').value;
submitButton.classList.add("disabled");
submitButton.innerHTML = "<i class=\"fas fa-spin fa-sync-alt\"></i> Searching...";
fetch(`${frmSearch.getAttribute('action')}?q=${query}&max_results=${maxResults}`)
.then(response => response.json())
.then(data => handleSearchResponse(data, submitButton));
});
frmInfo.addEventListener('submit', (e) => {
e.preventDefault();
const submitButton = frmInfo.querySelector("button[type=submit]");
const query = document.getElementById('q_info').value;
submitButton.classList.add("disabled");
submitButton.innerHTML = "<i class=\"fas fa-spin fa-sync-alt\"></i> Getting info...";
fetch(`${frmInfo.getAttribute('action')}?q=${query}`)
.then(response => response.json())
.then(data => handleInfoResponse(data, submitButton));
});
});
function handleConvertResponse(data, submitButton) {
document.getElementById("convert-response").querySelector('pre').innerText = JSON.stringify(data, null, 4);
submitButton.innerHTML = "<i class=\"fa fa-sync-alt\"></i> Convert";
submitButton.classList.remove("disabled");
const tableCells = document.querySelectorAll("#convert-response table tr td:last-child");
const downloadButton = document.getElementById('download');
const removeButton = document.getElementById('remove');
if(data.error) {
tableCells[0].innerHTML = "<i class=\"fa fa-check\"></i>";
tableCells[1].innerText = data.message;
tableCells[2].innerText = "-";
tableCells[3].innerText = "0";
tableCells[4].innerText = "-";
tableCells[5].innerText = "-";
downloadButton.setAttribute("href", "#");
downloadButton.classList.add("disabled");
} else {
tableCells[0].innerHTML = "<i class=\"fa fa-times\"></i>";
tableCells[1].innerText = "-";
tableCells[2].innerText = `${data.title} (${data.alt_title})`;
tableCells[3].innerText = data.duration;
tableCells[4].innerText = data.youtube_id;
tableCells[5].innerText = new Date(data.uploaded_at.date).toLocaleString();
downloadButton.setAttribute("href", data.file);
downloadButton.classList.remove("disabled");
removeButton.setAttribute('data-id', data.youtube_id);
removeButton.classList.remove('disabled');
}
}
function handleSearchResponse(data, submitButton) {
submitButton.innerHTML = "<i class=\"fa fa-search\"></i> Search";
submitButton.classList.remove("disabled");
const preElement = document.getElementById("search-response").querySelector('pre');
preElement.innerText = JSON.stringify(data, null, 4);
const tableCells = document.querySelectorAll("#search-response table tr td:last-child");
const ulElement = document.querySelector("#search-response table tr:nth-child(3) td:last-child ul");
ulElement.innerHTML = '';
if(data.error) {
for (let i = 0; i < tableCells.length; i++)
tableCells[i].innerText = "-";
tableCells[0].innerHTML = "<i class=\"fa fa-check\"></i>";
tableCells[1].innerText = data.message;
} else {
tableCells[0].innerHTML = "<i class=\"fa fa-times\"></i>";
tableCells[1].innerText = "-";
data.results.forEach(el => {
const btn = document.createElement('button');
btn.className = 'ms-3 btn btn-sm btn-outline-secondary';
btn.innerText = 'Convert';
btn.onclick = () => {
document.getElementById('link').value = el.full_link;
document.getElementById('link').scrollIntoView({behavior: "smooth"});
return false;
};
const a = document.createElement('a');
a.href = el.full_link;
a.innerText = el.title;
const item = document.createElement('li');
item.appendChild(a);
item.appendChild(btn);
ulElement.appendChild(item);
});
}
}
function handleInfoResponse(data, submitButton)
{
submitButton.innerHTML = "<i class=\"fa fa-search\"></i> Retrieve info";
submitButton.classList.remove("disabled");
const preElement = document.getElementById("info-response").querySelector('pre');
preElement.innerText = JSON.stringify(data, null, 4);
const tableCells = document.querySelectorAll("#info-response table tr td:last-child");
if (data.error)
{
for (let i = 0; i < tableCells.length; i++)
tableCells[i].innerText = "-";
tableCells[0].innerHTML = "<i class=\"fa fa-check\"></i>";
tableCells[1].innerText = data.message;
}
else
{
tableCells[0].innerHTML = "<i class=\"fa fa-times\"></i>";
tableCells[1].innerText = "-";
tableCells[2].innerText = data.channel;
tableCells[3].innerText = data.channel_id;
tableCells[4].innerHTML = `<a target='_blank' href='${data.channel_url}'>${data.channel_url}</a>`;
tableCells[5].innerText = data.description;
tableCells[6].innerText = data.duration + ' seconds';
tableCells[7].innerText = data.id;
tableCells[8].innerText = data.published_at;
tableCells[9].innerText = data.title;
tableCells[10].innerHTML = `<a target='_blank' href='${data.url}'>${data.url}</a>`;
}
}