我有一个附加组件,我想在桌面上发出通知,但前提是用户正在积极使用计算机(例如,鼠标指针在任何地方移动,即不仅在浏览器窗口中)。
可以使用 WebExtensions 做到这一点吗?
我相信它会是这样的:
脚本.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() {}); } }
背景.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);