-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
63 lines (59 loc) · 2.48 KB
/
popup.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
/*
Popup window script
Author: @sergeyzavg
*/
document.addEventListener('DOMContentLoaded', function () {
chrome.tabs.executeScript(null, {file: "jquery.js"}, function () {
chrome.tabs.executeScript(null, {file: "content.js"}, function () {
getCourseInfo(function (updatedData) {
var courses = {};
chrome.storage.sync.get("courses", function (data) {
if (data.courses)
courses = JSON.parse(data.courses);
if (updatedData) {
$('.container').append($('#alert').html());
courses[updatedData.id] = updatedData;
delete courses['undefined'];
chrome.storage.sync.set({"courses": JSON.stringify(courses)}, function () {
});
}
for (var i in courses) {
$('.container').append(renderItem(courses[i]));
$("#" + courses[i].id).click(function (i) {
return function () {
chrome.tabs.create({url: "http://class.coursera.org" + courses[i].courseUrl});
}
}(i));
}
$('.alert').fadeOut(3000);
});
});
});
});
});
function getCourseInfo(callback) {
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.sendRequest(tab.id, {req: "getCourseInfo"}, function (response) {
callback(response.reply);
});
});
}
function renderTime(time) {
return Math.floor(time / 3600) + " h "
+ Math.round(time % 3600 / 60) + " min";
}
function renderItem(courseInfo) {
var progress = Math.round(courseInfo.viewed * 100 / courseInfo.total);
var color = (progress < 33) ? "progress-bar-danger" :
((progress < 66) ? "progress-bar-warning" : "progress-bar-success");
return $("#item-template").html()
.replace(/\{id\}/, courseInfo.id)
.replace(/\{logoUrl\}/, courseInfo.logoUrl)
.replace(/\{name\}/, courseInfo.name)
.replace(/\{courseUrl\}/, courseInfo.courseUrl)
.replace(/\{total\}/, renderTime(courseInfo.total))
.replace(/\{viewed\}/, renderTime(courseInfo.viewed))
.replace(/\{remained\}/, renderTime(courseInfo.total - courseInfo.viewed))
.replace(/\{progress\}/g, progress)
.replace(/\{color\}/g, color);
}