gpt4 book ai didi

google-chrome-extension - Chrome 扩展桌面通知在 sleep 时工作

转载 作者:行者123 更新时间:2023-12-04 20:02:56 25 4
gpt4 key购买 nike

我做了一个桌面通知,它每 1 分钟显示一次通知。 10 秒后自行关闭。

我去吃午饭,然后电脑进入休眠状态。当我回来时,我唤醒我的电脑,然后开始大量通知。

我该如何处理这个问题?我想如果计算机休眠它不应该显示通知。我该如何控制它?

背景.js

function show() {

var notification = webkitNotifications.createHTMLNotification(
'notification.html'
);

notification.show();
}

// Conditionally initialize the options.
if (!localStorage.isInitialized) {
localStorage.isActivated = true; // The display activation.
localStorage.frequency = 1; // The display frequency, in minutes.
localStorage.isInitialized = true; // The option initialization.
}


if (window.webkitNotifications) {
// While activated, show notifications at the display frequency.

if (JSON.parse(localStorage.isActivated)) { show(); }

var interval = 0; // The display interval, in minutes.


setInterval(function() {
interval++;
chrome.idle.queryState(15, state);
if(localStorage.frequency <= interval && state=="active")
{ show();
interval = 0;
}

}, 60000);

}

最佳答案

使用Chrome.idle用于检测浏览器是否处于事件状态并触发您的通知的 API。我想您可以使用可配置的时间间隔来查询状态并推迟通知。
引用

  • Chrome.idle

  • 编辑 1
    您代码中的状态是回调函数,而不是字符串!所以更改此代码
    setInterval(function () {
    chrome.idle.queryState(15, state);
    if (localStorage.frequency <= interval && state == "active") {
    show();
    interval = 0;
    }

    }, 60000);
    setInterval(function () {
    chrome.idle.queryState(15, function (state) {
    if (localStorage.frequency <= interval && state == "active") {
    show();
    interval = 0;
    }

    });
    }, 60000);

    关于google-chrome-extension - Chrome 扩展桌面通知在 sleep 时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14458019/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com