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

become

v1.5.0

Published

Transform target DOM elements to become incoming HTML

Downloads

13

Readme

become

browser support

Transform target DOM elements to become incoming HTML.

Useful for faking realtime updates on a page. This module works a lot like setting innerHTML but will only update the nodes that have actually changed.

NPM

API

var become = require('become')

become(original, newHtml, options)

original: A Node, Array of Nodes or NodeList that you want to update

newHTML: The new html you would like replace the original with.

options:

  • ignoreAttribtutes: An Array of attributes to ignore and leave in place (e.g. add style to ensure animations work correctly)
  • inner: (default false) Update the innerHTML of original instead of the outer.
  • tolerance: (default 50) An integer that represents the minimum size innerHTML to search for inner changes. Higher values may increase speed, but won't be as precise.
  • onChange: calls function(action, node, changeInfo) every time this module makes a change to the DOM. After a node's attributes or inner content changes, the function is call with action: 'update'. When a node's innerHTML has been replaced, it is called with 'inner', and when added 'append' or removed 'remove'. changeInfo includes oldAttributes if attributes have been changed and inner: true if the inner content was updated.

Example

<html>
  <head>
    <title>Page</title>
  <body>
    <div id='update'>
      Testing 123 <span>some stuff</span>
    </div>
  </body>
</html>
var become = require('become')

var elementToUpdate = document.querySelector('div#update')
var newContent = "<div id='update' class='something'>" + 
    "Testing 123 <span>some <strong>stuff</strong></span>" + 
  "</div>"

become(elementToUpdate, newContent)

This example adds the class "something" to the element and wraps the word 'stuff' with strong tags. No other nodes will be touched.

You can also use become to update the entire dom with a new one, but only updating the elements that have changed:

var become = require('become')
var request = require('request')

function softRefresh(){
  request(window.location.href, function(err, res, content){
    become(document, content)
  })
})

Some tips

When become is run on an element, it will remove any foreign elements (such as in-place editors and menus) that are not in the newContent. Adding data-preserve to these elements will cause them to be ignored and worked around.

If you are wanting to add animations, it's a good idea to add data-preserve-attribute='style' to any elements you want to animate. This will ensure that the style is not overwritten mid-animation causing a horrible mess.