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

sb-placer

v1.0.0

Published

Change content of the page according given criteria.

Downloads

3

Readme

SB Placer

About

  • This is the tool to change content of html-element according given criteria.
  • There is a pure JavaScript version, without any dependency from third-party libraries.
  • And jQuery-plugin for jQuery lovers.
  • You can use it for phones switch, titles switch, any site content switch.

Grab it

# from `npm`
npm install --save sb-placer

# `bower`
bower install --save sb-placer

Or you can download this repo and use sb-placer.min.js / sb-placer.jquery.min.js from /dist folder.

Setup

In the <head>:

<!-- Stand-alone -->
<script src="/path/to/sb-placer.min.js"></script>

<!-- jQuery -->
<script src="/path/to/jquery.min.js"></script>
<script src="/path/to/sb-placer.jquery.min.js"></script>

You can use it as CommonJS / AMD module.

// Stand-alone
var sbPlacer = require('sb-placer');

// jQuery
var $ = require('jquery');
        require('sb-placer/jquery');

Now we can change the things.

// Stand-alone
sbPlacer({
  /* settings */
});

// jQuery
$('.sb-phone').sbPlacer({
  /* settings */
});

Configure

What we have to do to make things happen:

  • give the DOM-node to place the data
  • give the criteria
  • exec callback right after the change (if you want to)

DOM-node

Here it is:

// Stand-alone
sbPlacer({
  targets: '.sb-phone'
  /* settings */
});

// jQuery
$('.sb-phone').sbPlacer({
  /* settings */
});

.sb-phone — it's a simple css-selector. You can use classes, ids and all the stuff. In this example all nodes with the class sb-phone will be innerHTML'ed with the given data.

This is the only difference between the stand-alone and jQuery configurations.

Criteria

// get the visitor's current source with Sourcebuster
var source = sbjs.get.current.src;

// magic!
$('.sb-phone').sbPlacer({
  default_value: '8-800-DEFAULT-PHONE',
  conditions: [
    {
      check: source,
      when: 'google',
      place: '8-800-GOOGLE-PHONE'
    },
    {
      check: source,
      when: 'bing',
      place: '8-800-BING-PHONE'
    }
  ]
});
  • default_valueString (you can use html here)
  • conditionsArray of Objects

Condition object

  • checkString / Array of Strings
  • whenString / Array of Strings / RegExp
  • placeString (you can use html here)

We place data if check === when. In the example above if source is 'google' — it will place '8-800-GOOGLE-PHONE' inside the node.

Ok, it was simple. What about this one:

$('.sb-phone').sbPlacer({
  default_value: '8-800-DEFAULT-PHONE',
  conditions: [
    {
      check: source,
      when: ['google', 'bing'],
      place: '8-800-GOOGLE-BING-PHONE'
    },
    {
      check: source,
      when: 'facebook',
      place: '8-800-FACEBOOK-PHONE'
    }
  ]
});

If source is 'google' OR 'bing' — place '8-800-GOOGLE-BING-PHONE'.

We want more!

var source = sbjs.get.current.src,
    medium = sbjs.get.current.mdm;

$('.sb-phone').sbPlacer({
  default_value: '8-800-DEFAULT-PHONE',
  conditions: [
    {
      check: [source, medium],
      when: [['google', 'bing'], 'organic'],
      place: '8-800-GOOGLE-BING-PHONE'
    },
    {
      check: [source, medium],
      when: ['facebook', 'social'],
      place: '8-800-FACEBOOK-PHONE'
    }
  ]
});

If source is ('google' OR 'bing') AND (medium is 'organic') — place '8-800-GOOGLE-BING-PHONE'.

NB! This is important: [check].length === [when].length, or this condition will be omitted.

And finally RegExp:

var source   = sbjs.get.current.src,
    medium   = sbjs.get.current.mdm,
    campaign = sbjs.get.current.cmp;

$('.sb-phone').sbPlacer({
  default_value: '8-800-DEFAULT-PHONE',
  conditions: [
    {
      check: [source, medium, campaign],
      when: [['google', 'bing'], 'cpc', /^gb_.*_cpc$/i],
      place: '8-800-GOOGLE-BING-PHONE'
    },
    {
      check: [source, medium, campaign],
      when: [['facebook', 'twitter'], 'social', /^ft_.*_social$/i],
      place: '8-800-FACEBOOK-TWITTER-PHONE'
    }
  ]
});

Callback

You can exec callback right after the placement was done (it's optional). It takes 3 arguments: check, when & place — values from matched object.

If nothing matched:

  • check = false
  • when = false
  • place = default_value
var doSmth = function(check, when, place) {
  // bla-bla
}

$('.sb-phone').sbPlacer({
  /* settings */
  callback: doSmth
});

P.S.

This script interacts with DOM-nodes, so pay attention to where you put the placer. Use jQuery's $(document).ready (or native event DOMContentLoaded with fallback) or place the placer after the target DOM-node.

P.P.S.

It's MIT.

Have fun!