v1.1
With XHook, you could easily implement functionality to:
- Cache requests in memory, localStorage, etc.
- Insert authentication headers
- S3 Request Signing, see S3 Hook
- Simulate responses
- Create fake transparent backends for testing purposes
- Sending Error statistics to Google Analytics
- Polyfil 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 (In progress)
- Intercept and modify XHR request and response
- Simulate responses transparently
- Backwards compatible
addEventListener
removeEventListener
- Backwards compatible user controlled progress (download/upload) events
-
Add BrowserSwarm or TestlingCI automated cross-browser tests
Tip: See CONTRIBUTING.md for steps on how to contribute 😉
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');
});
Tested in IE8+, Chrome, Firefox, Safari
-
Development xhook.js 11KB
-
Production xhook.min.js 5KB (1.2KB Gzip)
⚠️ It's important to include XHook first as other libraries may store a reference toXMLHttpRequest
before XHook can patch it
Modifying any property of the request
object will modify the underlying XHR before it is sent.
If our handler
is asynchronous, just include the optional callback
argument, which accepts an optional response
object.
To provide a fake response, return
or callback()
a response
object.
Modifying any property of the response
object will modify the underlying XHR before it is received.
If our handler
is asynchronous, just include the optional callback
argument.
method
(String) (open(method,url)
)url
(String) (open(method,url)
)body
(String) (send(body)
)headers
(Object) (All Name-Value pairs set withsetRequestHeader(name,value)
)timeout
(Number) (timeout
)type
(String) (responseType
)withCredentials
(String) (withCredentials
)
status
(Number) Required when for fakeresponse
s (status
)statusText
(String) (statusText
)text
(String) (responseText
)headers
(Object) (All Name-Value pairs retrieved withgetAllResponseHeaders()
)xml
(XML) (responseXML
)data
(Varies) (response
)
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.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
http://www.w3.org/TR/XMLHttpRequest/
http://www.w3.org/TR/XMLHttpRequest2/
-
xhr instanceof XMLHttpRequest
checks will returnfalse
-
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.
See CONTRIBUTING for instructions on how to build and run XHook locally.
Version 0.x docs and downloads can be found here
Copyright © 2013–2014 Jaime Pillora [email protected]
Copyright © 2014 Daniel Gasienica [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.