npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

visibly.js

v0.1.1

Published

A cross-browser Page Visibility API shim

Downloads

558

Readme

visibly.js

A shim for the Page Visibility API

visibly lets you easily establish whether a page currently being viewed in the browser is visible to the user or has been hidden by them switching to another tab.

The visibly polyfill wraps around the Page Visibility API defined by the W3C. Native support for the API can be found in all modern browsers based on data from caniuse.com.

Demo

See the demo for a quick preview of the shim in action.

Why care about page visibility?

Creating user experiences around page visibility detection allows one to improve performance by opting to do things such as stopping data-streams/AJAX requests from being pulled should they decide to switch tabs. Data-flow can then be resumed when a user tabs back with very little visible cost to the experience.

Creative uses such as pausing video streams until a user returns, changing content depending on their tab behaviour or even saving interim versions of content being written in the browser (such as with GMail) are also possibly use-cases.

##Usage examples

###Polyfilled methods based on the official specifications

// visibilityChange()
visibly.visibilitychange(function(state){
	console.log('The current visibility state is:' + state);
});

// hidden()
if(visibly.hidden()){
	console.log('the current page is hidden')
}

// visibilityState()
console.log('The current visibility state is:' + visibly.visibilityState());

Performing an action when the page is visible or hidden

visibly.onVisible(callback) allows you to trigger a callback when a page switches from a hidden state to a visible state. Similarly visibly.onHidden(callback) performs the converse.

The following is an example where both the document title will be altered based on the page state and a message will be logged to the browser's console based on the same.

visibly.onVisible(function () {
    console.log('page is visible');
});

visibly.onHidden(function () {
    console.log('page is hidden');
});

visibly.onVisible(function (){
	document.title = 'visible';
});

visibly.onHidden(function (){
	document.title = 'hidden';
});

Other features

visibly.isSupported() will return true if browser supports the Page Visibility API natively.

if(visibly.isSupported()) {
    console.log('page visibly natively supported');
}

Compatibility & Size

visibly is approx. 2K (minified) and has been tested with all Chrome release channels (36+), Opera stable, Safari 7.1, Firefox stable & nightlies, Internet Explorer 8.0, 9.0 and 10PP2.

Alternatives

An alternative to the visibly polyfill is visibility.js. This solution offers a similar set of features but for an additional 1.5KB you are also able to get time-related features should you need them. For more on this solution see: https://github.com/ai/visibilityjs