-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
135 lines (117 loc) · 3.17 KB
/
background.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
// Constants
let youtubeVideoIDParamKey = "v"
// Observe Tab updated
browser.tabs.onUpdated.addListener(processTabUpdate)
function processTabUpdate(_, changeInfo, tabInfo) {
if (changeInfo.url) { // URL has changed
browser.tabs.sendMessage(tabInfo.id, {
command: "clear",
url: changeInfo.url,
msg: "from bg"
})
}
}
// Observer web requests
browser.webRequest.onBeforeRequest.addListener(
processWebRequest,
{urls: ["https://www.youtube.com/api/timedtext*"]}
)
function processWebRequest(requestDetails) {
if (!requestDetails.url.startsWith('https://www.youtube.com/api/timedtext?')) {
return
}
let requestURL = new URL(requestDetails.url)
let videoID = requestURL.searchParams.get(youtubeVideoIDParamKey)
if (!videoID) {
return
}
shouldInterceptRequest(requestDetails.originUrl, videoID)
.then((shouldIntercept) => {
if (!shouldIntercept) {
return
}
return interceptTimedTextRequest(requestDetails)
.then(function (captionSlices) {
return saveCaptionsForURL(videoID, captionSlices)
})
})
.catch(function (error) {
console.log("Error: " + error);
});
}
function shouldInterceptRequest(originUrl, videoID) {
if (!originUrl) {
return new Promise((_, reject) => {
reject("No URL!?")
})
}
if (originUrl.startsWith('moz-extension://')) {
return new Promise((resolve, _) => {
resolve(false)
})
}
return hasCaptionsForURL(videoID)
.then((hasCaptions) => {
return !hasCaptions
})
}
// Storage APIs
function hasCaptionsForURL(videoID) {
return browser.storage.local.get([videoID])
.then((data) => {
if (data[videoID]) {
return true
} else {
return false
}
})
}
function saveCaptionsForURL(videoID, captionSlices) {
let storageJSON = { }
storageJSON[videoID] = captionSlices
return browser.storage.local.set(storageJSON)
}
function interceptTimedTextRequest(requestDetails) {
return fetch(requestDetails.url)
.then(function (response) {
return response.json();
})
.then(function (myJson) {
let captionSlices = generateCaptionSlices(myJson)
return captionSlices
})
.catch(function (error) {
console.log("Error: " + error);
})
}
/*
CaptionSlice {
startTime: number,
duration: number,
text: string
};
*/
function generateCaptionSlices(jsonResponse) {
if (jsonResponse["events"]) {
return jsonResponse["events"]
.map(transformJSONToCaptionSlice)
.filter(function(slice) {
return slice != null
})
}
}
function transformJSONToCaptionSlice(eventJson) {
if (!eventJson["segs"]) {
return null
}
let text = eventJson["segs"]
.reduce(function(prev, curr) {
let currString = curr["utf8"] ? curr["utf8"] : ""
return prev + " " + currString
}, "")
return {
"startTime": eventJson["tStartMs"],
"duration": eventJson["dDurationMs"],
"text": text
}
}