-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopupscript.js
More file actions
142 lines (113 loc) · 4.31 KB
/
Copy pathpopupscript.js
File metadata and controls
142 lines (113 loc) · 4.31 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
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
//when popup loads it sends message to content script to fetch video title
var url;
//get and sets user name via oauth from backend
function setName() {
chrome.runtime.sendMessage({message:"getName"}, function(response) {
let name = response.info.given_name;
document.getElementById("welcome").innerHTML += (", "+name+"!");
})
}
setName();
//checks internal chrome storage if current video has already been summarized
function checkForSummary(){
let k = url.split("=")[1].slice(0,11);
chrome.storage.local.get([k], function(result){
if (result[k]) {
document.getElementById("summarizedText").innerHTML = result[k]
document.getElementById("summaryTag").style.display = "block"
}
})
}
//gets title of youtube video
chrome.tabs.query({active:true,currentWindow:true}, function(tabs) {
if (tabs[0].url.includes("https://www.youtube.com/watch?v=")) {
chrome.tabs.sendMessage(tabs[0].id, {type:"getTitle"}, function (response) {
document.getElementById("currentVideo").innerHTML += response.title;
url = response.url;
checkForSummary();
if (response.transcript === false) {
//removing functionality if transcript doesnt exist
document.getElementById("sentences").remove();
document.getElementById("summarize").remove();
document.getElementById("sentenceValue").remove();
//displaying error message
document.getElementById("sentenceAmount").innerHTML = "TRANSCRIPT UNAVAILABLE";
document.getElementById("sentenceAmount").style = "background-color:lightgray;color:red;margin-top:10px";
}
})
} else {
document.getElementById("main").innerHTML = `<div style="background:rgb(173,173,173);margin-bottom: 10px;">
<img src = "/headerLogo.png" width="200px" height="70px" alt = "QuickTube" >
</div>` + "<h3 style='background-color:lightgray;color:red;margin-top:10px'>Please Navigate to a Youtube Video</h3>"
}
})
//hides summarize button to prevent spam clicking during request processing
axios.interceptors.request.use(function(config){
let btn = document.getElementById("summarize");
let load = document.getElementById("loadingImg");
let para = document.getElementById("summarizedText");
document.getElementById("summaryTag").style.display = "none"
btn.style.display = "none"
para.style.display = "none"
load.style.display = "block"
load.style.margin = "auto"
return config
}, function(error) {
return Promise.reject(error);
})
axios.interceptors.response.use(function(response) {
let btn = document.getElementById("summarize");
let load = document.getElementById("loadingImg");
let para = document.getElementById("summarizedText");
document.getElementById("summaryTag").style.display = "block"
btn.style.display = ""
para.style.display = "block"
btn.style.margin = "auto"
load.style.display = "none"
btn.style.marginBottom = "10px"
return response
}, function(error) {
return Promise.reject(error);
})
var slider = document.getElementById("sentences")
slider.onchange = function(event){
var output = document.getElementById("sentenceValue")
if (slider.value >= 10 && slider.value < 15) {
output.innerHTML = "Less Text";
} else {
output.innerHTML = "More Text";
}
}
function storeSummary(summary) {
let urlKey = url.split("=")[1].slice(0,11);
chrome.storage.local.set({[urlKey]:summary}, function() {
})
}
sendInfo = async (data) => {
result = await axios.post("https://quicktube-backend.herokuapp.com/summarize",data).then(response=>{
console.log(response);
let result = response.data.summary;
document.getElementById("summarizedText").innerHTML = result;
document.getElementById("summaryTag").style.display = "block"
storeSummary(result)
})
}
function reverser(n) {
if (n < 15) {
return ((15-n) + 15)
} else if (n > 15) {
return (15 - (n-15))
} else {
return 15
}
}
//fetches video url to send to backend for NLP summarization
document.getElementById("summarize").addEventListener("click", function () {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let sentences = parseInt(slider.value);
sentences = reverser(sentences)
console.log({url:tabs[0].url,sentences:sentences});
let info = {url:tabs[0].url,sentences:sentences};
sendInfo(info)
});
});