[英]Detecting user desktop inactivity using WebExtensions
我有一個附加組件,我想在桌面上發出通知,但前提是用戶正在使用計算機(例如,鼠標指針移動到任何地方,即不僅在瀏覽器窗口中)。
可以使用WebExtensions來做到這一點嗎?
我相信會是這樣的:
的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);
Chrome具有專用的chrome.idle
API來檢測空閑狀態。
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
}
}
);
它還提供事件chrome.idle.onStateChanged
,以在狀態更改時通知您。
WebExtensions 列出了此API 尚不支持 (2016-07-21)。
queryState
始終報告"active"
狀態。 onStateChanged
不可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.