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

pageutilities

v1.0.8

Published

Helper function to build client side A/B tests

Downloads

5

Readme

pageutilities License: MIT

This is a library of helper function. It provides utility functions for checking and waiting for conditions on a page, and finding DOM elements. Installation

You can install the package using npm:

npm install pageutilities

:hammer_and_wrench: How tos

The library exposes multiple functions:

:gem: getElement

getElement(cssSelector, outTimer = 10000)

This function returns a Promise that resolves with an object containing all elements that match a given CSS selector. If no elements are found, it will wait for mutations on the document body and retry until elements matching the selector are found or a timeout is reached.

:gear: Parameters

  • cssSelector (required): A string that specifies the CSS selector for the desired elements.
  • outTimer (optional): An integer that specifies the maximum number of milliseconds to wait for the desired elements to appear. If the elements are not found within this time, the function will reject with an error. The default value is 10000 (10 seconds).

:handshake: Return Value

This function returns a Promise that resolves with an object containing the CSS selector and the matching elements. The object has the following properties:

  • selector: A string that specifies the CSS selector used to find the elements.
  • elements: An array of DOM elements that match the CSS selector.

If the desired elements are not found within the specified time, the function will reject with an error.

:scroll: Usage

import { getElement } from 'pageutilities';

// Find all elements with class 'my-class'
getElement('.my-class')
  .then((result) => {
    console.log(`Found ${result.elements.length} elements with selector '${result.selector}'`);
    // Do something with the found elements
  })
  .catch((error) => {
    console.error(error);
  });

Notes

  • This function uses querySelectorAll to find the desired elements, so the CSS selector must conform to the rules for this function.
  • The MutationObserver is used to detect changes to the DOM and update the element selection accordingly. This allows the function to be resilient to changes in the DOM structure.
  • If the outTimer parameter is not provided, the default value of 10000 milliseconds (10 seconds) will be used.
  • If the desired elements are not found within the specified time, the function will reject with an error.

:gem: waitForConditions

waitForConditions(conditions, callback, timeout = 10000, pollFreq = 100)

This function waits for a set of conditions to be true before executing a callback function. It takes an array of conditions to be checked, a callback function to execute once all conditions are true, and an optional timeout and polling frequency.

:gear: Parameters

  • conditions: An array of conditions to be checked. Each condition can be a function that returns a boolean, or a string that represents a CSS selector for an element to be found.
  • callback: A callback function to be executed once all conditions are true. The callback function is passed an object that maps CSS selectors to the matching elements.
  • timeout: (Optional) The maximum time in milliseconds to wait for all conditions to be true. The default value is 10000ms.
  • pollFreq: (Optional) The polling frequency in milliseconds. The default value is 100ms.

:handshake: Return Value

This function does not return a value, but instead executes the callback function once all conditions are true.

:red_circle: Errors

This function may throw the following errors:

  • TypeError: If the conditions parameter is not an array, or the callback parameter is not a function, or the timeout parameter is not a number greater than or equal to 1000.
  • Error: If a condition function throws an error or times out, or if the getElement function throws an error when trying to find an element.

:scroll: Usage


import { waitForConditions } from 'pageutilities';

// Wait for two elements with class 'my-class' and an element with ID 'my-id'
waitForConditions(['.my-class', '.my-class', '#my-id'], (elements) => {
  console.log(`Found ${elements['.my-class'].length} elements with class 'my-class'`);
  console.log(`Found element with ID 'my-id': ${elements['#my-id'][0].outerHTML}`);
  // Do something with the found elements
}, 10000, 500);

:gem: pollerLite

pollerLite(conditions, callback, maxTime = 10000)

This is a light-weight version of 'waitForConditions' This function polls the DOM for a set of conditions to be met before executing a callback. It takes an array of conditions to check for, a callback function to execute when all conditions are true, and an optional maximum time limit for the polling.

:gear: Parameters

  • conditions (Array): An array of conditions to check for. Each element can either be a string selector or a function that returns a boolean value.
  • callback (Function): The function to execute when all conditions are met.
  • maxTime (Number, optional): The maximum time to wait in milliseconds. Default is 10000 (10 seconds).

:scroll: Usage

import { pollerLite } from 'pageutilities';

// Poll for two elements with class 'my-class'
pollerLite(['.my-class', '.my-class'], () => {
  console.log('Both elements found');
  // Do something when both elements are found
}, 5000);

:gem: onUrlChange

onUrlChange(callback)

This function allows you to execute a callback function whenever the URL changes in a single page application (SPA). It uses a MutationObserver to observe changes to the document body and detect URL changes.

:gear: Parameters

  • callback (required): The callback function to execute when the URL changes. This function should accept two parameters: oldHref and mutation. oldHref is a string that contains the URL before it changed. mutation is an object that describes the change that triggered the callback.

:red_circle: Errors

  • It should throw an error if callback is not a function and if any error is caused by the callback itself.

:scroll: Usage

onUrlChange((oldHref, mutation) => {
  console.log(`URL changed from ${oldHref} to ${window.location.href}`);
  console.log(mutation);
});

Contributing

Contributions are welcome! If you have any issues, suggestions or improvements, please submit a pull request or open an issue.

License

This package is licensed under the MIT license.