Git Product home page Git Product logo

xhook's Introduction

XHook

Easily intercept and modify XHR ("AJAX") request and response

With XHook, you could easily implement functionality to:

  • Cache requests in memory, localStorage, etc.
  • Insert authentication headers
  • Simulate responses
    • Create fake transparent backends for testing purposes
  • Sending Error statistics to Google Analytics
  • Create a client-side alternative to CORS by offloading requests to an iframe then splicing the response back in, see XDomain
  • Devious practical jokes
  • Supports RequiresJS and Browserify
  • Preflight GZip compression, see XZip (Incomplete)

Features

  • Intercept and modify XMLHttpRequest ("AJAX") request and response
  • Simulate responses transparently
  • Backwards compatible addEventListener removeEventListener
  • Backwards compatible user controlled progress (download/upload) events

Future Features

  • Add BrowserSwarm or TestlingCI automated cross-browser tests

    Tip: See CONTRIBUTING.md for steps on how to contribute ๐Ÿ˜‰

Example

Here, we're converting vowels to z's in all requests to 'example.txt':

//modify 'responseText' of 'example2.txt'
xhook.after(function(request, response) {
  if(request.url.match(/example\.txt$/))
    response.text = response.text.replace(/[aeiou]/g,'z');
});

Browser Support

Tested in IE8+, Chrome, Firefox, Safari

Run test suite here: http://jpillora.com/xhook/test

Demos

http://jpillora.com/xhook

Download

โš ๏ธ It's important to include XHook first as other libraries may store a reference to XMLHttpRequest before XHook can patch it

API

xhook.before(handler(request[, callback])[, index])

Modifying any property of the request object will modify the underlying XHR before it is sent.

To make the handler is asynchronous, just include the optional callback function, which accepts an optional response object.

To provide a fake response, return or callback() a response object.

xhook.after(handler(request, response[, callback]) [, index])

Modifying any property of the response object will modify the underlying XHR before it is received.

To make the handler is asynchronous, just include the optional callback function.

xhook.enable()

Enables XHook (swaps out the native XMLHttpRequest class). XHook is enabled be default.

xhook.disable()

Disables XHook (swaps the native XMLHttpRequest class back in)


request Object

response Object

Overview

The dark red before hook is returning a response object, which will trigger the after hooks, then trigger the appropriate events, so it appears as if response came from the server.

Reference

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

http://www.w3.org/TR/XMLHttpRequest/

http://www.w3.org/TR/XMLHttpRequest2/

Issues

  • XHook does not attempt to resolve any browser compatibility issues. Libraries like jQuery and https://github.com/ilinsky/xmlhttprequest will attempt to do this. XHook simply proxies to and from XMLHttpRequest, so you may use any library conjunction with XHook, just make sure to load XHook first.

  • You may use synchronous XHR, though this will cause asynchronous hooks to be skipped.

Contributing

See CONTRIBUTING for instructions on how to build and run XHook locally.

Contributors:

MIT License

Copyright ยฉ 2014 Jaime Pillora [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

xhook's People

Contributors

3y3 avatar abrissirba avatar ilyagelman avatar jhorlin avatar jiangts avatar jpillora avatar morsdyce avatar nowells avatar peterjosling avatar rajsite avatar sondremare avatar stevenyxu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.