-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
96 lines (87 loc) · 3.18 KB
/
popup.js
File metadata and controls
96 lines (87 loc) · 3.18 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
// all listeners should go here
document.addEventListener('DOMContentLoaded', populate);
// this is responsible for opening a link in new tab
$(document).ready(function () {
$('body').on('click', 'a', function () {
var location = $(this).attr('href');
if ((location !== '#news') && (location !== '#assignments') && (location !== '#qlinks')) {
open_link(location);
return false;
} else {
return;
}
});
});
//main function which populates all the elements.
function populate() {
// console.log("reached populate");
build_news();
build_assignents();
build_quick_links();
}
function build_news() {
// console.log("build_news");
var news_str = localStorage.getItem('news');
console.log(news_str);
if (news_str === null) {
news_str = '<li class="list-group-item" >Please select courses first</li>';
}
document.getElementById('notif').innerHTML = news_str;
}
function build_assignents() {
var a_list;
if (localStorage["a_list"] === undefined) {
document.getElementById('assignments').innerHTML = "Please select courses first";
} else {
a_list = JSON.parse(localStorage["a_list"]);
if (Object.keys(a_list).length === 0) {
document.getElementById('assignments').innerHTML = "Nothing Yet...";
} else {
var list = "";
var keys = Object.keys(a_list);
for (i = 0; i < keys.length; i++) {
list = list + '<h4 align="center">' + keys[i] + '</h4><table class="table table-striped table-bordered table-hover">' + a_list[keys[i]] + '</table>';
}
document.getElementById('assignments').innerHTML = list;
}
}
}
function build_quick_links() {
console.log("build_quick_links");
chrome.storage.sync.get({
qlinks: {}
}, function (items) {
//console.log(items.qlinks);
if (Object.keys(items.qlinks).length === 0) {
document.getElementById('qlinks_list').innerHTML = "Please select courses from options page first.";
} else {
var list = "";
var keys = Object.keys(items.qlinks);
for (i = 0; i < keys.length; i++) {
list = list + '<a href="' + items.qlinks[keys[i]] + '" class="list-group-item"><i class="fa fa-book fa-fw"></i>' + keys[i] + '</a>';
}
document.getElementById('qlinks_list').innerHTML = list;
}
});
}
function open_link(link) {
// atttempt login to moodle
var xhr = new XMLHttpRequest();
var url = "https://moodle.niituniversity.in/moodle/login/index.php";
var params;
chrome.storage.sync.get({
name: 'test',
pass: 'pass'
}, function (items) {
params = "username=" + items.name + "&" + "password=" + items.pass;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//console.log(params);
xhr.send(params);
});
xhr.onreadystatechange = function () {//Call a function when the state changes.
if (xhr.readyState === 4 && xhr.status === 200) {
chrome.tabs.create({url: link});
}
};
}