-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add chrome.power/keep awake as an api sample
- Loading branch information
1 parent
578c684
commit 1ec292e
Showing
13 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# chrome.power | ||
|
||
This extension demonstrates the `chrome.power` API by allowing users to override their system's power management features. | ||
|
||
## Overview | ||
|
||
The extension creates add a popup, that cycles different states when clicked. It will go though a mode that prevents the display from dimming or going to sleep, a mode that keeps the system awake but allows the screen to dim/go to sleep, and whatever the system's default is. | ||
|
||
## Running this extension | ||
|
||
1. Clone this repository. | ||
2. Load this directory in Chrome as an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked). | ||
3. Pin the extension to the taskbar and click the action button. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"extensionName": { | ||
"message": "Keep Awake", | ||
"description": "Extension name." | ||
}, | ||
"extensionDescription": { | ||
"message": "Override system power-saving settings.", | ||
"description": "Extension description." | ||
}, | ||
"disabledTitle": { | ||
"message": "Default power-saving settings", | ||
"description": "Browser action title when disabled." | ||
}, | ||
"displayTitle": { | ||
"message": "Screen will be kept on", | ||
"description": "Browser action title when preventing screen-off." | ||
}, | ||
"systemTitle": { | ||
"message": "System will stay awake", | ||
"description": "Browser action title when preventing system sleep." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// 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. | ||
*/ | ||
let StateEnum = { | ||
DISABLED: 'disabled', | ||
DISPLAY: 'display', | ||
SYSTEM: 'system' | ||
}; | ||
|
||
/** | ||
* Key used for storing the current state in {localStorage}. | ||
*/ | ||
let 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) { | ||
let savedState = items[STATE_KEY]; | ||
for (let 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) { | ||
let imagePrefix = 'night'; | ||
let 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 + '"'; | ||
} | ||
|
||
let items = {}; | ||
items[STATE_KEY] = newState; | ||
chrome.storage.local.set(items); | ||
|
||
chrome.action.setIcon({ | ||
path: { | ||
19: 'images/' + imagePrefix + '-19.png', | ||
38: 'images/' + imagePrefix + '-38.png' | ||
} | ||
}); | ||
chrome.action.setTitle({ title: title }); | ||
} | ||
|
||
chrome.action.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); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"manifest_version": 3, | ||
|
||
"name": "__MSG_extensionName__", | ||
"description": "__MSG_extensionDescription__", | ||
"version": "1.9", | ||
"icons": { | ||
"16": "images/icon-16.png", | ||
"48": "images/icon-48.png", | ||
"128": "images/icon-128.png" | ||
}, | ||
|
||
"permissions": ["power", "storage"], | ||
"action": { | ||
"default_title": "__MSG_disabledTitle__", | ||
"default_icon": { | ||
"19": "images/night-19.png", | ||
"38": "images/night-38.png" | ||
} | ||
}, | ||
"background": { | ||
"service_worker": "background.js" | ||
}, | ||
|
||
"default_locale": "en" | ||
} |