mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-04-01 14:20:01 +07:00
* Remove docs folder. This was a redirect from a GitHub pages site that does not appear to be in use. * Rename api folder to api-samples. * Move examples to functional-samples folder. * Move cookbook sample to functional-samples. * Move tutorials to functional-samples folder. * Move mv2 and apps folders to _archive. * Rename tools folder to .repo. * Move reference folder to functional-samples. * Update README. Update README with new relative links for reorg. * Update README.md Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com> --------- Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
102 lines
2.5 KiB
JavaScript
102 lines
2.5 KiB
JavaScript
// 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); });
|
|
});
|