Socket
Book a DemoInstallSign in
Socket

are-they-here

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

are-they-here

Browser tab tracker

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

are-they-here

are-they-here is a simple package that determines the last active tab of the domain. Because localStorage is persisted across all tabs of a single domain, this kind of tracking is possible.

How it works

  • All tabs receive a random ID.
  • When any activity* happens in a tab, it's marked as active.
  • There can only be one active tab for a domain in a given time.

That is, if there's only one tab of example.com is open, it's always considered active. When two or more tabs of example.com are open and user switches between them, active tab changes.

* - "any activity" means:

  • window received focus;
  • mouse movement detected;
  • document has been scrolled;
  • key press detected.

Installation

Install with yarn:

$ yarn add are-they-here

Usage

are they here provides four functions:

  • areTheyHere() - true if current tab is active, false otherwise;
  • ifTheyAreHere(cb) - perform callback (cb) if current tab is active;
  • unlessTheyAreHere(cb) - perform callback (cb) if current tab is NOT active;
  • whenTheyCome(cb) - perform callback (cb) every time current tab becomes active.

Example

import { unlessTheyAreHere, whenTheyCome } from 'are-they-here'

document.addEventListener('DOMContentLoaded', () => {
  unlessTheyAreHere(() => console.log('You just missed DOMContentLoaded! :/'))
})

whenTheyCome(() => console.log('Greetings to this tab!'))

Keywords

tab

FAQs

Package last updated on 17 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts