Mozilla Extension Alert每30分钟

问题描述 投票:0回答:1

您好,我想创建一个扩展程序,每30分钟触发一次提醒,提醒我检查一下自己的姿势。但是我被卡住了。我不知道如何使警报仅在当前位于的选项卡中触发。现在在我打开的每个选项卡中触发。有谁可以帮助我吗?谢谢。

我正在以这种方式思考,每次我打开新标签页时,它将开始新的循环吗?因此,只有当我停留在当前标签页中时,我才能在30分钟内看到它。

setInterval(function() {
    alert("Posture!");
}, 5000);
{
  "name": "Posture Checker",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["posturecheck.js"]
    }
  ]
}
javascript mozilla
1个回答
0
投票

您可以通过检查document.hidden是否检查选项卡是否处于焦点。

document.hidden

或者,您也可以检查if (document.hidden) { // Document/tab/window is hidden } else { // Document/tab/window is visible } ,但是它不返回布尔值而是需要检查的字符串值:

document.visibiliyState
© www.soinside.com 2019 - 2024. All rights reserved.