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

@posthog/simmerjs

v0.7.7

Published

A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.

Downloads

65

Readme

Simmer JS Build Status Code Climate

Docs

A pure Javascript reverse CSS selector engine which calculates a DOM element's unique CSS selector on the current page.

Installation

Simmer is now meant to be consumed as a module via npm and is no longer meant as a drop in script into the browser. That said, we still build a version meant to be dropped into the browser which resides on window.Simmer as before.

We highly recommend you consume Simmer in the following manner, but in the interest of supporting exosting users we've retained the previous API.

npm i --save simmerjs

And then in your code:

import Simmer from 'simmerjs'

const simmer = new Simmer()

const el =  document.getElementById('#SomeElement')

expect(
    simmer(el)
).to.equal(
    '#SomeElement'
)

When using the distribution located in the repo under dist/simmer.js a global Simmer function will be exposed on the window. This is not the constructor, but rather a default instance which has exposed itself on the window wit ha default configuration. This is not an idea API and is meant to maintain the original API dating back to 2011 when this library was originally written.

Basic usage

To use Simmer to analyze an element and produce a unique CSS selector for it, all you have to do is instansiate a simmer query engine and pass it an element on the page. Usually you'd create a single instance which you would then use multiple times.

<div id="myUniqueElement">
    <span></span>
    <br/>
    <span></span>
</div>
import Simmer from 'simmerjs'

const simmer = new Simmer(window, { /* some custom configuration */ })
var myElement = document.getElementById("#myUniqueElement");

console.log(simmer(myElement)); // prints "[id='myUniqueElement']"

API

Simmer

    Simmer([Scope], [Options], [query])

Scope

When you create an instance of Simmer its first argument is the context in which Simmer should query for elements. Generally speaking this would be the window, which is the default value, but it would be overriden in a situation where you might be using Simmer against a Virtual Dom implementation. If you are using a Virtual DOM, you should provide the Window object of the Virtual DOM

Options

The second argument is an Options object allowing you to override the default configuration for Simmer's behaviour.

The options are:

| Option | Default | | ------ | ------- | | specificityThreshold - A minimum specificty level. Once the parser reaches this level it starts verifying the selector after every method is called. This can cut down our execution time by avoiding needless parsing but can also hurt execution times by performing many verifications. Specificity is calculated based on the W3C spec: http://www.w3.org/TR/css3-selectors/#specificity | 100 | | depth - How deep into the DOM hierarchy should Simmer go in order to reach a unique selector. This is a delicate game because the higher the number the more likely you are to reach a unique selector, but it also means a longer and more breakable one. Assuming you want to store this selector to use later, making it longer also means it is more likely to change and loose it's validity. | 3 | | | errorHandling - How to handle errors which occur during the analysis Valid Optionsfalse: errors are ignored by Simmertrue: errors rethrown and expected to be caught by the user_a function callback will be called with two parameters: the exception and the element being analyzed | false | | selectorMaxLength - A maximum length for the CSS selector can be specified - if no specific selector can be found which is shorter than this length then it is treated as if no selector could be found. | 520 |

query

The third argument is a query engine you wish Simmer to use when evaluating generated selectors. By default Simmer uses the window.document.querySelectorAll function and if you provide a window to the scope, Simmer will assume that you want it to use the document.querySelectorAll on that window. But if you wish Simmer to use another custom function, such as your own tweaked version of jQuery, you can do so by passing the third argument to the Simmer constructor.

The signature the query function should provide is this:

query(selector: String, onError: Error => {}) : Array(DomElements)

What this means is that the function you provide should expect to receive a string CSS selector and a function. It should them query for elements matching the selector and return an array of the results (even if there is only one result, it should be returned in an Array.).

The second argument is a function which should be called if any error is encountered when querying for the elements. If an error occurs or a problem is encountered, instead of throwing, you should call the function and pass the error object to it. Simmer will then handle the error as per its configuration.

Reconfiguring

If you have an existing instance of Simmer, you can use its configure method to instanciate a new Simmer which has the same scope and configuration as the existing one, with any new configuration you wish to apply.

So, for example, in the browser, you can replace the global Simmer wit ha newly configured version:


window.Simmer = window.Simmer.configure({
    depth: 10
})

And in Node:

import Simmer from 'simmerjs'

const virtualWindow = new JSDom()

const simmer = new Simmer(virtualWindow)

const reconfiguredSimmer = simmer.configure({ /* some custom configuration */ })

Conflict

When the Simmer browser dist located at dist/simmer.js is injected in to the browser it adds the noConflict function on itself. This is not relevant in a Node environment and isn't available there.

Just in case you also had the brilliant idea of using a variable called "Simmer", or you wish to move it off of the global object then you can use the noConflict method to receive a reference to the object and remove it from the window. Calling it will also revert the original value of window.Simmer which was there before loading the Simmer.js script (if there was one)

    var mySimmer = Simmer.noConflict();
    mySimmer(myElm);