Files
chrome-extensions-samples/api-samples/idle/service-worker.js
Xuezhou Dai 50e377c44c Add idle sample (#961)
* Add idle sample

* Fix typo

* Fix onStateChanged threshold

* Add README.md

* Update file structure

* Remove tabs permission

* Update api-samples/idle/README.md

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>

* Update api-samples/idle/service-worker.js

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>

* Update api-samples/idle/README.md

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>

* Update api-samples/idle/README.md

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>

---------

Co-authored-by: amysteamdev <37001393+AmySteam@users.noreply.github.com>
2023-07-07 15:42:14 +01:00

28 lines
692 B
JavaScript

/**
* Stores a state every time it changes, up to 20 items.
*/
chrome.idle.onStateChanged.addListener(async function (newstate) {
let { history_log } = await chrome.storage.session.get(['history_log']);
if (!history_log) {
history_log = [];
}
const time = Date.now();
if (history_log.length >= 20) {
history_log.pop();
}
history_log.unshift({ state: newstate, time: time });
chrome.storage.session.set({ history_log: history_log });
});
/**
* Opens history.html when the browser action is clicked.
*/
chrome.action.onClicked.addListener(function () {
chrome.windows.create({
url: 'history.html',
width: 700,
height: 600,
type: 'popup'
});
});