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

web-sniffer

v1.0.4

Published

A web tool for tracking dom events & visibility, javascript runtime errors, resource loading, page performance, network, route changes, memory leaks and custom behavior etc.

Downloads

11

Readme

web-sniffer

Overview

The web-sniffer library is a tool which can track dom events & visibility, javascript runtime errors, resource loading, page performance, network, route changes, memory leaks and custom behavior etc.

Install and load the library

From npm

npm i web-sniffer

From a CDN

<script src="https://unpkg.com/[email protected]/dist/web-sniffer.min.js"></script>

Or

<script type="module">
  import { createDomWatcher } from 'https://unpkg.com/[email protected]/dist/web-sniffer.esm.js'
  createDomWatcher({
    visibility: true
  }, console.log)
</script>

Usage

createDomWatcher

const reportCallback = (data) => {
  const body = JSON.stringify(data);
  const url = 'http://127.0.0.1:8080/analytics';

  // Use `navigator.sendBeacon()` if available, falling back to `fetch()`
  if (navigator.sendBeacon) {
    navigator.sendBeacon(url, body);
  } else {
    fetch(url, { body, method: 'POST', keepalive: true });
  }
}

const domConfig = {
  visibility: true,
  event: true
}

createDomWatcher(domConfig, reportCallback)

The domConfig has the following configuration items:

| name | type | default | description | | ---- | ----- | ------ | ----------- | | visibility | boolean | true | Dom visibility detection | | root | HTMLElement | document.documentElement | The Element or Document whose bounds are used as the bounding box when testing for intersection. | | threshold | number | 0.2 | A list of thresholds, sorted in increasing numeric order, where each threshold is a ratio of intersection area to bounding box area of an observed target. Notifications for a target are generated when any of the thresholds are crossed for that target. If no value was passed to the constructor, 0 is used. | | event | boolean | true | Dom event detection | | eventListeners | array | ['click'] | Dom events like click, dbclick, mouseenter etc. |

In Vue

// App.vue
import HelloWorld from './components/HelloWorld.vue'
import { createDomWatcher, createResourceWatcher } from 'web-sniffer/dist/web-sniffer.esm'

export default {
  name: 'App',
  components: {
    HelloWorld
  },
  mounted () {
    createDomWatcher({
      visibility: true,
      event: true
    }, this.reportCallback)

    createResourceWatcher(this.reportCallback)

    let img = document.createElement('img')
    img.src = '//localhost:8081/101.png'
    document.body.appendChild(img)
  },
  methods: {
    reportCallback: (data) => {
      const body = JSON.stringify(data);
      const url = 'http://127.0.0.1:8080/analytics';

      // Use `navigator.sendBeacon()` if available, falling back to `fetch()`
      if (navigator.sendBeacon) {
        navigator.sendBeacon(url, body);
      } else {
        fetch(url, { body, method: 'POST', keepalive: true });
      }
    }
  }
}
<!-- HelloWorld.vue -->
<h3 :data-event="msg" :data-expose="msg">Ecosystem</h3>

Before using custom data attributes, you have to register event type in eventListeners. Using data attributes can avoid the third-party javascript frameworks filtering custom directives.

In React

// App.js
import { createDomWatcher } from 'web-sniffer/dist/web-sniffer.esm';
import Hello from './components/hello'

function App() {
  const reportCallback = (data) => {
    const body = JSON.stringify(data);
    const url = 'http://127.0.0.1:8080/analytics';

    // Use `navigator.sendBeacon()` if available, falling back to `fetch()`
    if (navigator.sendBeacon) {
      navigator.sendBeacon(url, body);
    } else {
      fetch(url, { body, method: 'POST', keepalive: true });
    }
  }

  useEffect(() => {
    createDomWatcher({
      visibility: true,
      event: true
    }, reportCallback)
  }, [])

  return (
    <div className="App" data-expose="App show">
      <Hello/>
    </div>
  );
}

export default App;
// Hello.js
export default function Hello () {
  return (
    <h1 data-expose="hello world" data-event="clicked me">hello world</h1>
  )
}

createJsErrorWatcher

import { createJsErrorWatcher } from 'web-sniffer/dist/web-sniffer.esm'

createJsErrorWatcher(console.log)

The web-sniffer will catch js errors, including synchronous errors and asynchronous errors such as promise unhandledrejection. You can pass on a callback function to handle captured errors.

createResourceWatcher

import { createResourceWatcher } from 'web-sniffer/dist/web-sniffer.esm'

createResourceWatcher(console.log)

The web-sniffer will track the img, script and link resources loading, if loaded failed it will report the target info which has tag name and url.

Notice: When the page is first to load resources, the web-sniffer maybe loaded behind the css stylesheets link, in such case it could not track the failed link loadings.

If performance API is not supported in the browser, a work around way is combining onerror and onreadystatechange handlers. When the page is first to load, onerror handler may be called before onreadystatechange handler and onreadystatechange handler works only once, so there is a flag to check if the loading errors are first loading.

Notice: Using background-image to load would not be detected.

createRouteWatcher

import { createRouteWatcher } from 'web-sniffer/dist/web-sniffer.esm'

createRouteWatcher(console.log)

It will detect hash and history change, you can pass on a callback function to handle the detail info which contains oldURL and newURL.

Performance

Network

Memory

Behavior