-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
105 lines (88 loc) · 3.01 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
function checkForInternet() {
console.log("checking for internet connection...");
pingMCL(function() {
console.log("we are online");
chrome.browserAction.setIcon({path: "icon.png"});
}, function(respText) {
console.log("no internet!");
chrome.browserAction.setIcon({path: "icon-offline.png"});
// Try find a token in the NU-NET auth page
var token = grabTokenNuNet(respText);
// If found then lets attempt an auth
if (token) {
console.log("NU-NET detected: " + token);
connectNuNet(token);
}
});
}
function pingMCL(success, fail) {
var xhrCheck = new XMLHttpRequest();
var url = "http://mobcomlab.s3.amazonaws.com/pingtest.json?_=" + new Date().getTime();
console.log(url);
xhrCheck.open("GET", "http://mobcomlab.s3.amazonaws.com/pingtest.json?_=" + new Date().getTime(), true);
xhrCheck.onreadystatechange = function() {
if (xhrCheck.readyState == 4 && xhrCheck.status === 200) {
try {
if (JSON.parse(xhrCheck.responseText).success) {
success();
}
} catch (e) {
fail(xhrCheck.responseText);
}
}
else {
fail("");
}
};
xhrCheck.send();
}
function grabTokenNuNet(respText) {
console.log("Getting token from NU-NET");
var pattern = new RegExp('magic" value="[a-z0-9]+');
var token = pattern.exec(respText);
if (token === null) {
console.log("Token from NU-NET: null");
return null;
}
token = token[0].substr(14);
return token;
}
function connectNuNet(token) {
console.log("connecting to NU-NET");
var username = localStorage["username"];
var password = localStorage["password"];
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://login3.nu.ac.th:1000/", true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
pingMCL(function() {
console.log("connect success");
chrome.browserAction.setIcon({path: "icon.png"});
}, function(respText) {
console.log("connect unsuccessful, maybe not NU");
});
}
};
xhr.send("magic="+token+"&username="+username+"&password="+password);
}
function connectNuWireless() {
}
// Default icon is offline
chrome.browserAction.setIcon({path: "icon-offline.png"});
// Open options on button click
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({url: "options.html"});
});
// Check for internet when the first tab is loaded
chrome.tabs.onCreated.addListener(function() {
checkForInternet();
});
chrome.windows.onCreated.addListener(function() {
checkForInternet();
});
// Set up alarm to check internet every 3 minutes
chrome.alarms.onAlarm.addListener(function() {
checkForInternet();
});
chrome.alarms.create({when: 0, periodInMinutes: 3});