forked from emiliogambrell/caffeine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventPage.js
74 lines (71 loc) · 2.47 KB
/
eventPage.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
/* Caffeine main event javascript v2 */
var storage = chrome.storage.local;
var enabled = false;
initStorage();
// init storage engine on load
function initStorage() {
storage.get('enabled', function(result){
//console.log(result['enabled']);
if(result['enabled'] === undefined) {
enabled = true;
} else {
enabled = result['enabled'];
}
if(enabled) {
caffeineEnable();
} else {
caffeineDisable();
}
});
}
// saves the current value of enabled to storage
function saveStorage() {
storage.set({'enabled':enabled}, function() { console.log('saved in chrome.storage.local: '+enabled); });
}
//shows a notification telling the user that Caffeine has been enabled
function showNotification() {
chrome.notifications.create("welcome", {
type: "basic",
iconUrl: "caffeineEnabled.png",
title: "Caffeine Enabled!",
message: "Your Chromebook now has a good supply of Caffeine and will stay awake forever! (meaning that automatic power management has been disabled) You can always take away the Caffeine (in effect turn back on the automatic power management) by clicking / toggling the Caffeine extension icon."
}, function (notificationId) {
//callback needs to be included for notifications to work!
});
}
// toggles the power management to off
function caffeineEnable() {
chrome.power.requestKeepAwake("display");
chrome.browserAction.setIcon({path: '/caffeineEnabled.png'});
//chrome.browserAction.setBadgeText({ text: "c" });
//console.log('requestKeepAwake: '+enabled);
saveStorage();
showNotification();
}
// re-engages the system power management
function caffeineDisable() {
chrome.power.releaseKeepAwake();
chrome.browserAction.setIcon({path: '/caffeineDisabled.png'});
//chrome.browserAction.setBadgeText({ text: '0'});
//console.log('releaseKeepAwake: '+enabled);
saveStorage();
}
//listens for events when storage changes (debug)
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
//console.log('Storage key "%s" in namespace "%s" changed. Old value was: %s, new value is: %s', key, namespace, storageChange.oldValue, storageChange.newValue);
}
});
// listens for events when the button is toggled
chrome.browserAction.onClicked.addListener(function() {
if(!enabled) {
enabled = true;
caffeineEnable();
} else {
enabled = false;
caffeineDisable();
}
});
// coming soon: chrome.alarms
// http://developer.chrome.com/extensions/alarms.html