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

hijax

v0.2.1

Published

XHR Proxy to intercept AJAX calls independent of libraries

Downloads

18

Readme

Hijax XHR Proxy

NPM

Hijax is meant to be a lightweight library-independent way of intercepting XHR requests. It is meant to be used with the Mobify Adaptive and MobifyJS frameworks.

Browser support

Hijax has been tested on the following browsers:

  • Chrome (mobile and desktop)
  • Firefox (desktop)
  • Safari (desktop and iOS6+)

Library support

Although Hijax should intercept XHR requests regardless of how the XHR request is invoked, its proxying features are known only to work with the following libraries at present:

  • jQuery 2.1.1, 1.3.2
  • Zepto 1.1

Hijax should be able to work with other libraries, but support isn't guaranteed.

Adapters

Support for proxying other libraries can be added with the use of adapters, as shown in the example in the example folder, which uses an adapter for jQuery 1.3.2 from the adapter folder.

How to use Hijax

Import Hijax distribution file from the dist folder (and any adapters you need) into your project. Hijax uses an UMD format, but AMD is the preferred way of using Hijax.

Hijax is initialized by creating a hijax instance like so:

var hijax = new Hijax();

If an adapter is used, it should be passed to the constructor:

// Eg:
var myAdapter = require('adapters/jquery.legacy');
var hijax = new Hijax(myAdapter);

An XHR request can be proxied by calling the set method, and providing a name, url/truth function and the callbacks for the events to be proxied:

hijax.set(<name>, <url> OR <function>, {
    beforeSend: <function>,
    receive: <function>,
    complete: <function>
});

// Eg:
hijax
    .set(
        // A unique name for the proxy
        'homeProxy',
        // Either a URL, or a function that takes in the URL as an argument,
        // and returns true/false
        '/example/response.json',
        {
        // Request is being sent
        beforeSend: function(xhr) {
            console.log(this.name, 'Intercepting send.');
        },
        // Received response data
        receive: function(data, xhr) {
            console.log(this.name, 'Intercepting receive.');
        },
        // Request completed (desktop listener has finished processing it)
        complete: function(data, xhr) {
            console.log(this.name, 'Request complete.');
        }
    });

Additional listeners can be set like so:

hijax.addListener(<name>, <event>, <callback>);

// Eg:
hijax.addListener('proxy1', 'complete', function(data, xhr) {
    console.log(this.name, 'Another listener.');
});

You can remove listeners on a given instance like so:

hijax.removeListener(<name>, <event>);

// Eg:
var cb = function() { ... }
hijax.removeListener('proxy1', 'beforeSend', cb);

// Removes all listeners on the beforeSend event
hijax.removeListener('proxy', 'beforeSend');

Data Parsers

Hijax will attempt to detect the type of data by reading the response header. If the response is available in the respective format (for instance, responseJSON for a json content type), Hijax will just pipe it through.

In other instances, Hijax will attempt to use a data parser to parse the data into a usable format. Currently only JSON is parsed (using the browser's native JSON.parse method). Text, HTML and XML are piped through a stub parser which does not modify the data.

You can override the parsers by passing it in the options when creating an instance:

hijax.set(<name>, <url> OR <function>, { ... }, {
    dataParsers: { ... }
});

// Eg:
hijax.set('homeProxy', '/home.html', {
    complete: function(data) {
        // ...
    }
}, {
    dataParsers: {
        // When receiving HTML content, invoke this callback
        html: function(data) {
            return data;
        }
    }
});

Development

Developing Hijax locally involves:

# Grab dev dependencies
npm install
./node_modules/.bin/bower install

Check out some of the example usages in examples/

Building dist/ code:

Run grunt or grunt build

Open a PR!