Reputation: 9724
I have an add-on and I want to issue notifications on the desktop, but only if the user is actively using the computer (e.g the mouse pointer is moving anywhere, i.e, not only in the browser window).
It is possible to do this using WebExtensions?
I believe it would be something like this:
script.js
function Notify(id, title, message)
{
var props = {
"type": "basic",
"title": title,
"iconUrl": "images/icon-128px.png",
"message": message
};
var propsCopy = JSON.parse(JSON.stringify(props));
propsCopy.requireInteraction = true;
//Prevent exception in Firefox
try {
chrome.notifications.create(id, propsCopy, function() {});
} catch (ee) {
chrome.notifications.create(id, props, function() {});
}
}
background.js
var standByNotifications = [];
function registerNotification(id, title, message) {
if ([user is inactive]) {
standByNotifications.push({ "id": id, "title": title, "message": message });
} else {
Notify(id, title, message);
}
}
setInterval(function() {
if ([user is inactive] === false) {
var tmp = standByNotifications.reverse();
for (var i = tmp.length - 1; i >= 0; i--) {
Notify(tmp[i].id, tmp[i].title, tmp[i].message);
}
standByNotifications = []; //Clear
}
}, 1000);
Upvotes: 2
Views: 195
Reputation: 77513
Chrome has a dedicated API, chrome.idle
, to detect idle state.
chrome.idle.queryState(
5 * 60, // seconds
function(state) {
if (state === "active") {
// Computer not locked, user active in the last 5 minutes
} else {
// Computer is locked, or the user was inactive for 5 minutes
}
}
);
It also provides an event, chrome.idle.onStateChanged
, to notify you when the state changes.
WebExtensions list this API as not yet supported (2016-07-21).
queryState
always reports "active"
state.onStateChanged
is not available.Upvotes: 3