-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
104 lines (78 loc) · 2.18 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 getTemp(Station,callback, errorCallback) {
var searchUrl = 'http://stationdata.wunderground.com/cgi-bin/stationlookup?station='+ Station +'&units=english&v=2.0&format=json&_=' +
+ Date.now();
//console.log(searchUrl);
var x = new XMLHttpRequest();
x.open('GET', searchUrl);
x.responseType = 'json';
x.onload = function() {
var response = x.response;
if (!response) {
errorCallback('No response from WU!');
return;
}
//console.log(x.response.stations)
var temp = response.stations[Station].temperature;
callback(temp);
};
x.onerror = function() {
errorCallback('Network error.');
};
x.send();
}
var LastTemp = 1;
var LastColor = '#000000'
function doUpdate() {
chrome.storage.sync.get({
PWS: 'KORPHILO13',
ForC: 0
}, function(items) {
var Station = items.PWS;
var ForC = items.ForC;
getTemp(Station,function(temp) {
if (ForC) {
temp = (temp-32)*5/9;
}
var canvas = document.createElement('canvas');
canvas.width = 19;
canvas.height = 19;
var context = canvas.getContext('2d');
context.font = "bold 10px Silkscreen";
if (LastTemp > parseFloat(temp)) {
LastColor = '#FF1F1F';
} else if (LastTemp < parseFloat(temp)) {
LastColor = '#298A1E';
}
context.fillStyle = LastColor
context.fillText(String(temp),0,14);
var imageData = context.getImageData(0, 0, 19, 19);
chrome.browserAction.setIcon({
imageData: imageData
});
LastTemp = parseFloat(temp);
}, function(errorMessage) {
document.getElementById('status').textContent = errorMessage;
});
});
};
document.addEventListener('DOMContentLoaded', function() {
doUpdate();
});
var pollInterval = 1*1000;
function startRequest() {
doUpdate();
window.setTimeout(startRequest,pollInterval);
}
startRequest()
function OpenWUPage() {
chrome.storage.sync.get({
PWS: 'KORPHILO13'
}, function(items) {
var Station = items.PWS;
chrome.tabs.create({url: "http://www.wunderground.com/personal-weather-station/dashboard?ID=" + Station});
});
}
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function(tab) {
OpenWUPage();
});