嗨,我想创建一个扩展程序,每30分钟触发一次提醒,提醒我检查一下自己的姿势。但是我被卡住了。我不知道如何使警报仅在即时消息当前触发的选项卡中触发。现在在我打开的每个选项卡中触发。有人能帮助我吗?谢谢。
我正以这种方式思考,每次我打开新标签页时,它将开始新的循环吗?因此,只有当我停留在当前标签页中时,我才会在30分钟内看到它。
setInterval(function() {
alert("Posture!");
}, 5000);
{
"name": "Posture Checker",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["posturecheck.js"]
}
]
}
答案 0 :(得分:0)
您可以通过检查document.hidden
是否为焦点来检查标签。
if (document.hidden) {
// Document/tab/window is hidden
} else {
// Document/tab/window is visible
}
或者,您也可以检查document.visibiliyState
,但是它不返回布尔值,而是需要检查的字符串值:
if (document.visibilityState === 'hidden') {
// Document/tab/window is hidden
} else if (document.visibilityState === 'visible') {
// Document/tab/window is visible
}