-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
59 lines (50 loc) · 1.5 KB
/
Copy pathcontent_script.js
File metadata and controls
59 lines (50 loc) · 1.5 KB
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
//gets title from youtube video
function title() {
let title = document.getElementsByClassName(
"title style-scope ytd-video-primary-info-renderer"
);
title = title[0].innerHTML.split(">")[1].split("<")[0];
return title;
}
var trans;
var token;
chrome.runtime.sendMessage({message:"askToken"}, function(response){
token = response.token;
})
//checking if video has a valid transcript
transcript = async () => {
if (token) {
if (location.href.includes("https://www.youtube.com/watch?v=")) {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == XMLHttpRequest.DONE) {
trans = JSON.parse(xhttp.responseText);
trans = Boolean(trans.items.length)
}
};
let link =
"https://www.googleapis.com/youtube/v3/captions?videoId=" +
location.href.split("=")[1]+ "&part=id&access_token=" + token;
await xhttp.open("GET", link);
xhttp.send(null);
}
}
};
transcript();
//sends title back to popup
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
switch (message.type) {
case "getTitle":
let url = location.href;
let ans = { title: title(), transcript: trans, url: url };
sendResponse(ans);
break;
case "updateTranscript":
token = message.token;
//if url change must check for transcript again
transcript();
break;
default:
console.error("Unrecognised message: ", message);
}
});