-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
183 lines (160 loc) · 6.51 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
172
173
174
175
176
177
178
179
180
181
182
183
function showLoader() {
document.getElementById("loader").style.display = "block";
}
function hideLoader() {
document.getElementById("loader").style.display = "none";
}
document.getElementById("youtubeLink").addEventListener("input", function() {
var youtubeLink = document.getElementById("youtubeLink").value.trim();
var isDisabled = youtubeLink === "";
["getTranscriptButton", "getPDFButton", "getVIDButton", "getAIButton", "copyButton", "getMP3Button"].forEach(id => {
document.getElementById(id).disabled = isDisabled;
});
});
document.getElementById("copyButton").addEventListener("click", function() {
var transcriptElement = document.getElementById("transcript");
var textToCopy = transcriptElement.innerText;
var tempTextArea = document.createElement("textarea");
tempTextArea.value = textToCopy;
document.body.appendChild(tempTextArea);
tempTextArea.select();
document.execCommand("copy");
document.body.removeChild(tempTextArea);
alert("Copied the transcript/summary to clipboard!");
});
function fetchTranscript(youtubeLink) {
var videoID = youtubeLink.split('v=')[1];
if (!videoID) throw new Error('Invalid YouTube link');
var apiUrl = 'https://youtube-transcriber-api.vercel.app/v1/transcripts?id=' + videoID + '&lang=en';
return fetch(apiUrl).then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
});
}
function displayError(message) {
document.getElementById("transcript").innerText = message;
}
function getTranscript() {
var youtubeLink = document.getElementById("youtubeLink").value;
showLoader();
fetchTranscript(youtubeLink)
.then(data => {
var transcript = data.transcripts[0].text;
document.getElementById("transcript").innerText = transcript;
hideLoader();
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
displayError("Error retrieving transcript. Reasons could be the language is not in English, incorrect video ID, or API issues.");
hideLoader();
});
}
function getPDF() {
var youtubeLink = document.getElementById("youtubeLink").value;
showLoader();
fetchTranscript(youtubeLink)
.then(data => {
var transcript = data.transcripts[0].text;
document.getElementById("transcript").innerText = transcript;
const { jsPDF } = window.jspdf;
var doc = new jsPDF();
var pageHeight = doc.internal.pageSize.height;
var margin = 10;
var maxLineWidth = 180;
var lineHeight = 10;
var y = margin;
var lines = doc.splitTextToSize(transcript, maxLineWidth);
lines.forEach(line => {
if (y + lineHeight > pageHeight - margin) {
doc.addPage();
y = margin;
}
doc.text(line, margin, y);
y += lineHeight;
});
hideLoader();
doc.save('transcript.pdf');
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
displayError("Error retrieving transcript");
hideLoader();
});
}
// Client-side JavaScript
function getVID() {
var URLInput = document.getElementById("youtubeLink").value;
sendURL(URLInput); // Call sendURL with the input value directly, without calling .value again
}
function getMP3() {
var URLInput = document.getElementById("youtubeLink").value;
sendMP3(URLInput); // Call sendMP3 with the input value directly, without calling .value again
}
function sendMP3(URL) {
window.location.href = `https://transcript-r2z3.onrender.com/downloadmp3?URL=${encodeURIComponent(URL)}`;
hideLoader(); // Encode the URL before sending it to the server
}
function sendURL(URL) {
window.location.href = `https://transcript-r2z3.onrender.com/download?URL=${encodeURIComponent(URL)}`; // Encode the URL before sending it to the server
hideLoader();
}
function getAI() {
var youtubeLink = document.getElementById("youtubeLink").value;
showLoader();
fetchTranscript(youtubeLink)
.then(data => {
var transcript = data.transcripts[0].text;
var cleanedTranscript = transcript.replace(/\[\s*__\s*\]/g, ''); // Remove [ __ ]
var isTrimmed = cleanedTranscript.length > 5100;
var trimmedTranscript = isTrimmed ? cleanedTranscript.slice(0, 5100) : cleanedTranscript;
console.log(trimmedTranscript)
return fetch('https://transcript-r2z3.onrender.com/summarize', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ transcript: trimmedTranscript })
}).then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
hideLoader();
}).then(summary => {
var summaryText = isTrimmed ? summary.summary + ' (Only summarised first 5100 characters)' : summary.summary;
document.getElementById("transcript").innerText = summaryText;
hideLoader();
});
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
displayError("Error summarizing transcript");
hideLoader();
});
}
function getAIText() {
var userPrompt = prompt("Enter AI Prompt: ");
if (userPrompt) {
showLoader();
fetch('https://transcript-r2z3.onrender.com/prompt', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ prompt: userPrompt })
})
.then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
hideLoader();
})
.then(data => {
var reply = data.reply; // Assuming the server response has a 'reply' field
document.getElementById("transcript").innerText = reply;
hideLoader();
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
displayError("Error processing AI prompt");
hideLoader();
});
}
}