-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.js
More file actions
106 lines (84 loc) · 2.98 KB
/
utils.js
File metadata and controls
106 lines (84 loc) · 2.98 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
function parseData(html){
var parsed = [];
$('<div/>').html(html).contents().each(function(index){
var section = {};
section["time"]=$(this).find(".caption-line-time").text();
section["text"]=$(this).find(".caption-line-text").text();
console.log(JSON.stringify(section));
parsed.push(section);
});
return parsed;
}
function search(term, parsed){
var result = [];
var reg = new RegExp(term, "i");
for(var i = 0; i < parsed.length; i++){
//var execed = reg.exec(parsed[i]["text"]);
if(parsed[i]["text"].match(reg)!=null){
//console.log("asdfasd");
var matched = parsed[i]["text"].replace(reg, "<b style='background-color:#fff7aa'>$&</b>");
var context = matched;
if(i>0){
context = parsed[i-1]["text"]+"<br/>" + context;
}if(i<parsed.length-1){
context = context+"<br/>"+parsed[i+1]["text"];
}
result.push([result.length, parsed[i]["time"], parsed[i]["time"]+": "+matched, context]);
}
}
return result;
}
// Appends a new search result to the search results section
function appendSearchResult(id, actualTime, timeDesc, context){
var html = '<li><div id="attribute'+id+'" class="collapsible-header';
html+= '"><i class="material-icons">subtitles</i><a class="videolink" href="#" data-time="';
html += actualTime+'">';
html += timeDesc;
html += '</a></div><div class="collapsible-body"><p>';
html += context;
html += '</p></div></li>'
console.log(html);
$("#search-results-list").append(html);
}
function setSearchResult(results){
$("#search-results-list").html("");
for (var i = 0; i<results.length; i++){
var current = results[i];
appendSearchResult(current[0], current[1], current[2], current[3])
}
$(".videolink").click(function () {
var time = $(this).attr('data-time');
console.log("asdfasf");
console.log(time);
time = ""+time;
var a = time.split(':');
var seconds = 0;
if(a.length==3) {
seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
}else if(a.length==2){
seconds = (+a[0]) * 60 + (+a[1]);
}else{
seconds = (+a[0]);
}
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
var newUrl = updateQueryStringParameter(url, "t", seconds);
chrome.tabs.update(tab.id, {url: newUrl});
});
})
}
function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}