forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
101 lines (91 loc) · 2.5 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
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* States that the extension can be in.
*/
var StateEnum = {
DISABLED: 'disabled',
DISPLAY: 'display',
SYSTEM: 'system'
};
/**
* Key used for storing the current state in {localStorage}.
*/
var STATE_KEY = 'state';
/**
* Loads the locally-saved state asynchronously.
* @param {function} callback Callback invoked with the loaded {StateEnum}.
*/
function loadSavedState(callback) {
chrome.storage.local.get(STATE_KEY, function(items) {
var savedState = items[STATE_KEY];
for (var key in StateEnum) {
if (savedState == StateEnum[key]) {
callback(savedState);
return;
}
}
callback(StateEnum.DISABLED);
});
}
/**
* Switches to a new state.
* @param {string} newState New {StateEnum} to use.
*/
function setState(newState) {
var imagePrefix = 'night';
var title = '';
switch (newState) {
case StateEnum.DISABLED:
chrome.power.releaseKeepAwake();
imagePrefix = 'night';
title = chrome.i18n.getMessage('disabledTitle');
break;
case StateEnum.DISPLAY:
chrome.power.requestKeepAwake('display');
imagePrefix = 'day';
title = chrome.i18n.getMessage('displayTitle');
break;
case StateEnum.SYSTEM:
chrome.power.requestKeepAwake('system');
imagePrefix = 'sunset';
title = chrome.i18n.getMessage('systemTitle');
break;
default:
throw 'Invalid state "' + newState + '"';
}
var items = {};
items[STATE_KEY] = newState;
chrome.storage.local.set(items);
chrome.browserAction.setIcon({
path: {
'19': 'images/' + imagePrefix + '-19.png',
'38': 'images/' + imagePrefix + '-38.png'
}
});
chrome.browserAction.setTitle({title: title});
}
chrome.browserAction.onClicked.addListener(function() {
loadSavedState(function(state) {
switch (state) {
case StateEnum.DISABLED:
setState(StateEnum.DISPLAY);
break;
case StateEnum.DISPLAY:
setState(StateEnum.SYSTEM);
break;
case StateEnum.SYSTEM:
setState(StateEnum.DISABLED);
break;
default:
throw 'Invalid state "' + state + '"';
}
});
});
chrome.runtime.onStartup.addListener(function() {
loadSavedState(function(state) { setState(state); });
});
chrome.runtime.onInstalled.addListener(function(details) {
loadSavedState(function(state) { setState(state); });
});