I've written a skeleton Chrome extension for a Library kiosk environment. The purpose of the extension is to clear all browsing data and restart Chrome after X seconds of inactivity. I was inspired by the Idle Reset extension on the Chrome web store, but this doesn't provide all features I'd like. Ideally I would like to mimic Presto-based Opera's inactivity reset, present in kiosk mode, where a dialogue window with a countdown is displayed, i.e.:
Inactivity reset
The browser will be reset and all browsing data removed in 60...1 seconds
But I think that's maybe a bit too complicated and I'm not sure whether the chrome.notification
API supports dynamic content such as countdown timers.
So far I have come up with some code that displays a chrome notification after 1 minute of inactivity. The notification informs the user the browser will be reset in 30 seconds unless activity is resumed. The notification then closes itself. After a further 30 seconds of inactivity, the browsing data is cleared, any existing tabs removed and a new tab opened. I am using the chrome.alarms
API and lowering the chrome.idle.setDetectionInterval
after the first idle event. If the user resumes activity the alarms and notifications are cleared, and chrome.idle.setDetectionInterval
is restored to the default value of 60 seconds.
Here is my event.js
code:
chrome.idle.onStateChanged.addListener(function (newState) {
if (newState == "idle") {
var opt = {
type: "basic",
title: "Web Browser about to be reset",
message: "Web browser has been inactive for 1 minute. Move the mouse or type something within 30 seconds to prevent the reset.",
iconUrl: "refresh-icon.png"
};
chrome.notifications.create("browserResetNotification", opt, function() {
chrome.idle.setDetectionInterval(15);
chrome.alarms.create("inactivityAlarm", {'when': Date.now() + 30000})
});
}
else {
chrome.alarms.clear("inactivityAlarm", function() {});
chrome.notifications.clear("browserResetNotification", function() {});
chrome.idle.setDetectionInterval(60);
}
});
chrome.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name == 'inactivityAlarm') {
chrome.browsingData.remove({
}, {
"appcache": true,
"cache": true,
"cookies": true,
"downloads": true,
"fileSystems": true,
"formData": true,
"history": true,
"indexedDB": true,
"localStorage": true,
"pluginData": true,
"passwords": true,
"webSQL": true
});
chrome.tabs.create({
url: "http://myurl.com",
active: true
}, function (newtab) {
chrome.tabs.query({}, function (results) {
for (var i = 0; i < results.length; i++) {
var tab = results[i];
if (tab.id != newtab.id) {
chrome.tabs.remove(tab.id);
}
}
});
});
}
})
My questions
- I've been testing my code and have found it's not altogether reliable (i.e. the notification/reset doesn't always occur), and TBH I'm not surprised. Is there a better approach?
- Is it possible to display a chrome notification for a fixed duration rather than have it open and close of its own accord? I'd like to have it show for the full 30 seconds up to the point the alarm fires and the browser resets.
- I am confused about the
chrome.idle
API, in particular when to usechrome.idle.queryState
method versus when to usechrome.idle.onStateChanged
event. In particular it would be nice to be able to query the state at that very moment without having to provide adetectionIntervalInSeconds
, required by the method. - What does
chrome.notifications.clear
method actually do? Signal to Chrome that any record of a notification should be removed? Or force a notification to be removed from the user's display? I'm unclear on when it'd be appropriate to use it.