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

page-info-js

v2.3.6

Published

Gather information about the DOM, Loading Time, Errors, Mutations, and more (with Custom Callbacks) with Javascript.

Downloads

16

Readme

PageInfoJS

With PageInfoJS you can gather information about time to load the Document DOM as well create custom callbacks for events when:

  • Elements are loaded,
  • A Javascript error detected,
  • DOM loading percentage changes,
  • Document is completely loaded(or interactive, loading, etc).
  • When a mutation happens on page(when a new element is created on page for example)

You can work with information about time too:

  • DOM loading start timestamp,
  • Get the elapsed time in milliseconds when an event occur,
  • Timestamp of the moment that an event occur.

A real world example for PageInfoJS custom callbacks and time info is to send information about your frontend apps for your analytics backend servers.

How to use

With npm

Although you can use as a Node module PageInfo works with DOM, so you can use the package in your frontend app with your webpack configuration to compile the PageInfoJS code together your app code. To install the module type:

npm i page-info-js

Importing the package with ES6:

import 'page-info-js';

Or import with require:

require('page-info-js');

And thus you can instantiate the PageInfoJS:

const PageInfo = new PageInfoJS([]);

OBS: the array on constructor is an array of custom callbacks, learn more about it clicking here.

As a <script> tag included in your html page

Include the /dist/PageInfo.js or /dist/PageInfo.gz script on <head> element and you can work with the PageInfoJS at the end of your html, after close the </body> tag, or include your js script there.

You can find the compiled code on package releases list.

Examples

See the files on /example directory for a simple real world example.
To execute the example on your local browser, run:

npm run start

Your browser will open http://localhost:8080 automatically with the example (open your browser console and enjoy!).

Working with custom callbacks

To use the custom callbacks for PageInfoJS events you can declare an array of callbacks:

var myCallbacks = [];

And create a callback when a DOM element is loaded:

/**  
 * @param element {HTMLElement}  
 * @param PageInfo {PageInfo}  
 */
myCallbacks[PageInfoJS.EventsList.DOM.ElementLoaded] = function (element, PageInfo) {  
  console.log('  >> Element loaded', element);  
  console.log('  >> Loaded elements', PageInfo.getLoadedElementsNumber());  
};

Events List:

You can implement custom callbacks for the bellow events list:

  • PageInfoJS.EventsList.DOM.ElementLoaded
  • PageInfoJS.EventsList.DOM.ElementsLoadingPercentageIncremented
  • PageInfoJS.EventsList.DOM.AllElementsLoaded
  • PageInfoJS.EventsList.DOM.MutationObserved (when an attribute or content of a html element change on page after it loaded)
  • PageInfoJS.EventsList.OnError
  • PageInfoJS.DocumentReadyStateChanged.Any (when ready state changed for any state)
  • PageInfoJS.DocumentReadyStateChanged.ToUninitialized
  • PageInfoJS.DocumentReadyStateChanged.ToLoading
  • PageInfoJS.DocumentReadyStateChanged.ToLoaded
  • PageInfoJS.DocumentReadyStateChanged.ToInteractive
  • PageInfoJS.DocumentReadyStateChanged.ToComplete

And pass the callbacks array for the PageInfoJS instance:

var PageInfo = new PageInfoJS(myCallbacks);

Working with Time

Getting the loading start timestamp:

var timestamp = PageInfo.Time.getStartTimestamp();  
console.log('PageInfoJS start timestamp:', timestamp);

Getting the current timestamp:

var timestamp = PageInfo.Time.getCurrentTimestamp();  
console.log('PageInfoJS current timestamp:', timestamp);

Elapsed time in milliseconds:

var time = PageInfo.Time.getElapsedTime();  
console.log('PageInfoJS elapsed time (milliseconds):', time);

With these time methods you can know when the page becomes Interactive and the elapsed time for that:

/**  
 * @param element {HTMLElement}  
 * @param PageInfo {PageInfo}  
 */
myCallbacks[PageInfoJS.EventsList.DocumentReadyStateChanged.ToInteractive] = function (element, PageInfo) {  
  console.log('Document is Interactive now, elapsed time (milliseconds)', PageInfo.Time.getElapsedTime());  
  console.log('Document is Interactive now, timestamp', PageInfo.Time.getCurrentTimestamp());  
};

You can use Time methods with any custom callback.

Working with Errors

PageInfoJS fire an Event instance when a js error occur on page, you can use it as follow:

/**
 * @param PageInfo {PageInfo}
 * @param e {Event || ErrorEvent}
 */
myCallbacks[PageInfoJS.EventsList.OnError] = function (PageInfo, e) {
  console.log('Error Detected (event timestamp):', PageInfo.Time.getCurrentTimestamp(), e);
};

Or combined with other events too:

/**
 * @param PageInfo {PageInfo}
 * @param changedTo {string}
 */
myCallbacks[PageInfoJS.EventsList.DocumentReadyStateChanged.Any] = function (PageInfo, changedTo) {
  console.log('Document ready state changed to `' + changedTo + '` after (milliseconds)', PageInfo.Time.getElapsedTime());
  console.log('Document have errors?', PageInfo.hasErrors(), PageInfo.getAllErrors());
};

Compiling the code

Clone this repo and you can use npm and webpack to compile the code. Install the requirements:

git clone https://github.com/Maykonn/PageInfoJS.git
npm install

In your dev environment you can run (will compile the code and open the example app at localhost:8080/):

npm run start

Build the code to production at /dist directory (minify, uglify, remove comments, logs, etc):

npm run build

The npm run build command will generate two file at /dist directory, PageInfo.gz and PageInfo.js.

Contributing

You can contribute to PageInfoJS cloning this repository from github clicking here. So, you just need to create a new branch using a name related to the new functionality which you'll create.
And after finish your code, you just need to create a pull request which will be revised, merged to master(if the code doesn't break the lib) branch and published a new release of PageInfoJS.