-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
57 lines (44 loc) · 1.76 KB
/
Copy pathbackground.js
File metadata and controls
57 lines (44 loc) · 1.76 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
function download() {
var captions = document.querySelectorAll('*[id^="UserCreatedTranscript"]');
var element = document.createElement('a');
var captionsstr = '';
var currentSentence = '';
var punctuation = -1;
var maxCaptionLen = 0;
for (var i = 0; i < captions.length; i++) {
if (captions[i].getElementsByClassName('index-event-row').length > 0) {
currentcaption = captions[i].getElementsByClassName('index-event-row')[0].getElementsByClassName('event-text')[0].getElementsByTagName('span')[0].innerText;
if (currentcaption.length > maxCaptionLen){
maxCaptionLen = currentcaption.length;
}
punctuation = Math.max(currentcaption.indexOf("."), currentcaption.indexOf("?"), currentcaption.indexOf("!"));
if (punctuation>0 && (currentSentence.length) > 80 && (currentcaption.charAt(punctuation+1) == "" || currentcaption.charAt(punctuation+1) == " ")){
captionsstr += currentSentence + currentcaption.substring(0,punctuation+1) + '\n\n';
if (punctuation+2 < currentcaption.length){
currentSentence = currentcaption.substring(punctuation+2) + " ";
}
else{
currentSentence = "";
}
}
else {
currentSentence += currentcaption + " ";
}
}
}
if (currentSentence != ""){
captionsstr += currentSentence
}
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(captionsstr));
element.setAttribute('download', document.title + ' Captions');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: download
});
});