Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

qsa-observer

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qsa-observer

A way to be notified about any element that was connected, or disconnected, from a container.

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
103
decreased by-53.6%
Maintainers
1
Weekly downloads
 
Created
Source

qsa-observer

A way to be notified about any element that was connected, or disconnected, from a container.

Differently from MutationObserver

  • each element is granted to be passed along utilities, whenever it's observed via one, or more, selectors
  • an element will pass through a setup(element, selector) utility, and per each matching selector
  • elements injected through innerHTML, within nested elements, will be parsed too
  • all elements that had a setup will pass through an update(element, connected) utility

Example

import QSAO from 'qsa-observer';

// an Array of CSS selectors to observe
const query = ['div'];

// where to observe mutations, document by default
const root = document;

const {
  flush,    // an utility to flush synchronously all queued mutations
  observer, // the MutationObserver created by QSAO(...)
  parse     // an utility to parse new elements
            // (i.e. after adding a selector to the query list)
} = QSAO({
  query,  // list of selectors to observe
  root,   // optional, as it's document by default

  // the method that receives all elements that match one or more
  // selectors in the query, and are either connected or disconnected
  handle(element, connected, selector, index) {
    const event = connected ? 'connected' : 'disconnected';
    console.log(element, event, selector, index);
  }
});

// example: add another selector to observe and passes through the setup
const observeMore = selector => {
  if (!query.includes(selector)) {
    query.push(selector);
    parse(root.querySelectorAll(selector));
  }
};

// example: watch 'p' nodes too after a second
setTimeout(observeMore, 1000, 'p');

FAQs

Package last updated on 31 Jul 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc