8000 GitHub - Ain477/xhook: Easily intercept and modify XHR request and response
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ain477/xhook

 
 

Repository files navigation

fhook

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

With fhook, 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

Browser Support

Support Modern Browser.

Demos

Usage

⚠️ It's important to include fhook first as other libraries may store a reference to XMLHttpRequest before fhook can patch it

Using script link to load fhook and use it, like so:

<script src="//unpkg.com/fhook@latest/dist/fhook.min.js"></script>
<script>
  fhook.after(function (request, response) {
    if (request.url.match(/example\.txt$/))
      response.text = response.text.replace(/[aeiou]/g, "z");
  });
</script>

We can also install fhook via npm.

npm install fhook

Then use ESM syntax to load fhook.

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

API

fhook.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.

fhook.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.

fhook.enable()

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

fhook.disable()

Disables fhook (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

  • fhook does not attempt to resolve any browser compatibility issues. Libraries like jQuery and https://github.com/ilinsky/xmlhttprequest will attempt to do this. fhook simply proxies to and from XMLHttpRequest, so you may use any library conjunction with fhook, just make sure to load fhook 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 fhook locally.

License

MIT License Copyright © 2022 Jaime Pillora dev@jpillora.com

About

Easily intercept and modify XHR request and response

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 35.2%
  • TypeScript 33.1%
  • JavaScript 31.6%
  • Shell 0.1%
0