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

@evolv-ai/flow

v0.0.7

Published

Utils for front end DOM manipulation.

Downloads

4

Readme

evolv-flow

Utils for front end DOM manipulation.

Getting started

To use evolv-flow in an integration, install the package.

npm i @evolv-ai/flow

The, import it in your integration's code.

import { waitQuery } from "@evolv/flow";
waitQuery(".my-element", (element) => {
  element.innerHTML = "Hello world!";
});

Usage with the Evolv editor

To use evolv-flow in an experiment, add the package on the evolvEditor using the project configuration:

File > Project Settings > Dependencies > Add Dependency.

Add @evolv-ai/flow as an NPM module and set the alias to flow.

Then, use it in any of your experiment's variants.

flow.waitQuery(".my-element", function(element) {
  element.innerHTML = "Hello world!";
});

More info about evolv-editor


From now on, the docs will use ES6 for the examples. Rember to use ES5 in the experiments.

Usage

waitQuery

Wait for an element to be present in the DOM. This function will run only once.

waitQuery(selector, callback(element))

import { waitQuery } from '@evolv-ai/flow';

waitQuery('#my-popup-form .cta', (element) => {
  element.innerHTML = 'Click me!';
});

waitQueryAll

Loos for elements to be present in the DOM. If a new element that matches the selector is found, the callback will be called with the added elements and all the elements that match the selector.

waitQueryAll(selector, callback(newElements, allElements))

This method could be usefull with infinite scrolls.

import { waitQueryAll } from '@evolv-ai/flow';

waitQueryAll('.infinite-scroll-container .item', (newElements, allElements) => {
  newElements.forEach((element) => {
    element.innerHTML = 'Hello world!';
  });
});

Also, an element that reappears on the page

waitQueryAll('.confirm-checkout .cta', (newElements) => {
  const checkoutConfirm = newElements[0];
  checkoutConfirm.innerHTML = 'Click me!';
});

If .confirm-checkout is an element that gets distroyed and created again, the callback will be called each time it appears.

getElement

Search for an element in the DOM.

getElement(query: string, container?: Element): Element | null
import { getElement } from '@evolv/flow';

const element = getElement("#selector");
if(element) {
  element.innerHTML = "Hello world!";
}

You can pass a container to search in.

import { waitQuery, getElement } from '@evolv/flow';

waitQuery('#my-popup-form', (container) => {
  const element = getElement("button[type='submit']", container);
  if(element) {
    element.innerHTML = "Click me!";
  }
});

Or

const container = getElement("#some-container");

if(container) {
  const innerElement = getElement("button", container);
}

getElementAll

Similar to getElement, but returns all the elements that match the query in an array.

getElementAll(query: string, container?: Element): Element[]
import { getElementAll } from '@evolv/flow';

const rows = getElementAll("ul.list li");
rows.forEach((row) => {
  row.innerHTML = "Hello world!";
});

listenEvent

Listen for an event and filter by a selector. This method will not attach a listener to that element, insead, it will attach a listener to the document and check if the target element matches the selector.

listenEvent = (event: keyof HTMLElementEventMap, selector: string, callback: (event: Event) => void, options?: { once?: boolean, continer?: Element })

Example:

import { listenEvent } from '@evolv-ai/flow';
listenEvent('click', '#my-popup-form .cta', (event) => {
  event.preventDefault();
  console.log('Clicked!');
});

Options:

  • once: if true, the callback will be called only once.
  • continer: if you want to listen inside a specific element.
import { waitQuery, listenEvent } from '@evolv-ai/flow';

waitQuery('#my-popup', (formEl) => {
  listenEvent('click', '.cta', (e) => {
    e.preventDefault();
    console.log('Clicked!');
  }, { continer: formEl, once: true });
});

matchUrl

Test if the current url matches a regex.

matchUrl(regex: RegExp): boolean
import { matchUrl } from '@evolv-ai/flow';

matchUrl(/checkout/, () => {
  console.log('Checkout page!');
});

onDomChange

Triggers a callback whenever the DOM changes.

onDomChange = (callback: () => void, container?: Element) => void
import { onDomChange } from '@evolv-ai/flow';

onDomChange(() => {
  console.log('DOM changed!');
});

You can pass a custom element to be observed. If an element is not passed, it will suscribe to the global listener.

import { onDomChange, getElement } from '@evolv-ai/flow';

waitQuery('#my-container', (containerEl) => {
  onDomChange(() => {
    console.log('DOM changed!');
  }, containerEl);
});

createObserver

Creates an observer

createObserver = (
  element: Element,
  callback: () => void,
  options?: MutationObserverInit) => MutationObserver
import { waitQuery, createObserver } from '@evolv-ai/flow';

waitQuery('#popup-form', (myFormEl) => {
  createObserver(myFormEl, () => {
    console.log('DOM changed!');
  });
})

You can pass custom options to the observer

import { waitQuery, createObserver } from '@evolv-ai/flow';

waitQuery('#an-input', (myFormEl) => {
  createObserver(myFormEl, () => {
    console.log('attribute changed!');
  });
}, {
  childlist: false,
  attributes: true,
})

setContext

An util for setting an evolv context variable.

setContext = (key: string, value: string) => void
import { setContext } from '@evolv-ai/flow';

setContext('my-key', 'my-value');