Skip to content

Commit

Permalink
add chrome.power/keep awake as an api sample
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkettner committed Jan 1, 2024
1 parent 578c684 commit 1ec292e
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api-samples/power/README.md
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.
22 changes: 22 additions & 0 deletions api-samples/power/_locales/en/messages.json
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."
}
}
99 changes: 99 additions & 0 deletions api-samples/power/background.js
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);
});
});
Binary file added api-samples/power/images/day-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/day-38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/icon-128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/icon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/icon-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/night-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/night-38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/sunset-19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api-samples/power/images/sunset-38.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions api-samples/power/manifest.json
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"
}

0 comments on commit 1ec292e

Please sign in to comment.