-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
70 lines (62 loc) · 2.05 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
function defval(val1, defvalue)
{
return (val1 && val1 != undefined) ? val1 : defvalue;
}
function loadStations() {
var stations = {from: {code: defval(localStorage.fromStationCode, "HBB"), name: defval(localStorage.fromStationName, "哈尔滨")},
to: {code: defval(localStorage.toStationCode, "BJP"), name: defval(localStorage.toStationName, "北京")}
};
return stations;
}
function loadLogins() {
var logins = {username: defval(localStorage.username, ""), password: defval(localStorage.password, "")};
return logins;
}
function saveStations(stations) {
console.log ("prepare to save stations " + stations.from.code + " --> " + stations.to.code);
localStorage.fromStationCode = stations.from.code;
localStorage.fromStationName = stations.from.name;
localStorage.toStationCode = stations.to.code;
localStorage.toStationName = stations.to.name;
return true;
}
function saveLogins(logins) {
localStorage.username = logins.username;
localStorage.password = logins.password;
return true;
}
function savePassengers(passengerList) {
localStorage.activePassengers = passengerList;
return true;
}
function loadPassengers() {
return {passengers: defval(localStorage.activePassengers, [[]])};
}
chrome.extension.onRequest.addListener( function(request, sender, sendResponse)
{
var reqItem = request.what;
console.log ("recv request: " + reqItem);
if (reqItem == "stations") {
var stations = loadStations();
sendResponse(stations);
} else if(reqItem == "logins") {
sendResponse(loadLogins()); // snub them.
} else if (reqItem == "saveStations") {
var ret = saveStations(request.stations);
sendResponse({ok: ret});
} else if (reqItem == "saveLogins") {
var ret = saveLogins (request.logins);
sendResponse({ok: ret});
} else if (reqItem == "ticketNotify") {
if (Audio) {
new Audio("hasticket.ogg").play();
}
sendResponse({ok:true});
} else if (reqItem == "savePassengers") {
savePassengers(request.passengers);
sendResponse({ok:true});
} else if (reqItem == "passengers") {
var passList = loadPassengers();
sendResponse(passList);
}
});