0

Hi i wanted to create extension that will trigger alert every 30mins to remind me check my posture. But i got stuck. I dont know how to make it so that alert triggers only in tab that im currently in. Now it triggers in every tab i have opened. Can someone help me please? Thanks.

As im thinking right now this way it will start new cycle every time i open new tab right? So im gonna see it in 30min only if i stay in that current tab.

setInterval(function() {
    alert("Posture!");
}, 5000);
{
  "name": "Posture Checker",
  "version": "1.0",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": ["posturecheck.js"]
    }
  ]
}

1 Answers1

0

You can check if a tab is in focus by checking if document.hidden.

if (document.hidden) {
    // Document/tab/window is hidden
} else {
    // Document/tab/window is visible
}

Alternatively, you can also check document.visibiliyState, but it does not return a boolean but a string value that you need to check against:

if (document.visibilityState === 'hidden') {
    // Document/tab/window is hidden
} else if (document.visibilityState === 'visible') {
    // Document/tab/window is visible
}
Terry
  • 63,248
  • 15
  • 96
  • 118
  • Thank you and how would you implement it into my function? Im not really programmer so im just trying to make this thing work and keep going. – Jirka Mařík Jan 06 '20 at 19:03