-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
88 lines (57 loc) · 1.54 KB
/
search.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
console.log("Hello, world!");
var $x;
var searchResults;
var index = 0;
var ready = false;
var serializer = new XMLSerializer();
var video = $("video")[0];
var video_id = video.baseURI.split("?v=")[1]
var xmlLink = "https://www.youtube.com/api/timedtext?v=" + video_id + "&lang=en"
var parser = new DOMParser();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
ready = true;
var xmlDoc = parser.parseFromString(xhttp.responseText,"text/xml");
$x = $(xmlDoc);
}
};
xhttp.open("GET", xmlLink, true);
xhttp.send();
//Finds all occurrences of the keyword
function search(keyword) {
console.log(keyword)
searchResults = $x.find("text:contains(" + keyword + ")");
if (len)
index = 0;
}
//moves the video to the current search result
function skip(dir) {
if (index + dir >= searchResults.length) {
index = 0
console.log("end")
}
if (index + dir < 0) {
index = searchResults.length - 1;
console.log("beg")
}
time = searchResults[index + dir].attributes.start;
video.currentTime = serializer.serializeToString(time)
index += dir;
}
//Listens for Search Input from Omnibar
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (ready) {
occurrences = search(request.msg, $x);
skip(0);
}
});
addEventListener("keydown", function(e) {
if (e.code == "KeyD") {
skip(1)
}
if (e.code == "KeyS") {
skip(-1)
}
});