• Stars
    star
    944
  • Rank 46,541 (Top 1.0 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily intercept and modify XHR request and response

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

Browser Support

Support Modern Browser.

Demos

http://jpillora.com/xhook

Usage

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

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

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

We can also install xhook via npm.

npm install xhook

Then use ESM syntax to load xhook.

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

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.

License

MIT License Copyright Β© 2022 Jaime Pillora [email protected]

More Repositories

1

chisel

A fast TCP/UDP tunnel over HTTP
Go
10,711
star
2

cloud-torrent

☁️ Cloud Torrent: a self-hosted remote torrent client
Go
5,651
star
3

xdomain

A pure JavaScript CORS alternative
JavaScript
3,072
star
4

overseer

Monitorable, gracefully restarting, self-upgrading binaries in Go (golang)
Go
2,215
star
5

notifyjs

Notify.js - A simple, versatile notification library
1,903
star
6

webproc

Wrap any program in a simple web-based user-interface
Go
710
star
7

go-tcp-proxy

A small TCP proxy written in Go
Go
694
star
8

docker-dnsmasq

dnsmasq in a docker container, configurable via a simple web UI
Dockerfile
686
star
9

jquery.rest

A jQuery plugin for easy consumption of RESTful APIs
CoffeeScript
614
star
10

backoff

Simple backoff algorithm in Go (golang)
Go
613
star
11

ipfilter

A package for IP Filtering in Go (golang)
Go
379
star
12

node-edit-google-spreadsheet

A simple API for editing Google Spreadsheets
JavaScript
304
star
13

base64-encoder

Base64 Encoder
HTML
260
star
14

node-torrent-cloud

Torrent Cloud – A self-hosted Bittorrent client in the Cloud
JavaScript
183
star
15

grunt-aws

A Grunt interface into the Amazon Node.JS SDK
JavaScript
174
star
16

velox

Real-time Go struct to JS object synchronisation over SSE and WebSockets
Go
173
star
17

installer

One-liner for installing binaries from Github releases
Go
171
star
18

opts

A Go (golang) package for building frictionless command-line interfaces
Go
161
star
19

verifyjs

Verify.js - A powerful, customizable asynchronous validation library
JavaScript
159
star
20

ssh-tron

Multiplayer Tron over SSH, written in Go
Go
143
star
21

go-ogle-analytics

Monitor your Go (golang) servers with Google Analytics
HTML
133
star
22

cloud-gox

A Go (golang) Cross-Compiler in the cloud
Go
132
star
23

media-sort

Automatically organise your movies and tv series
Go
115
star
24

go-tld

TLD Parser in Go
Go
114
star
25

node-load-tester

Simple load testing with Node.js
JavaScript
88
star
26

csv-to-influxdb

Import CSV files into InfluxDB
Go
81
star
27

sshd-lite

A feature-light sshd(8) for Windows, Mac, and Linux written in Go
Go
75
star
28

node-logbook

A simple, unobtrusive logger for Node
JavaScript
61
star
29

spy

Spy - Watches for file changes, restarts stuff
Go
61
star
30

go-and-ssh

Go and the Secure Shell protocol
Go
58
star
31

node-glob-all

Provide multiple patterns to node-glob
JavaScript
57
star
32

go-sandbox

An alternate frontend to the Go Playground
JavaScript
52
star
33

serve

Your personal HTTP file server in Go
Go
52
star
34

scraper

A dual interface Go module for building simple web scrapers
Go
49
star
35

dedup

A cross platform command-line tool to deduplicate files, fast
Go
42
star
36

node-google-sheets

Google Sheets v4 API using Node.js
JavaScript
40
star
37

archive

Archiver is a high-level API over Go's archive/tar,zip
Go
33
star
38

icmpscan

ICMP scan all hosts across a given subnet in Go (golang)
Go
29
star
39

hashedpassword

A small Go (Golang) package for hashed passwords
Go
26
star
40

gswg-examples

Getting Started with Grunt - Code Examples
JavaScript
25
star
41

webfont-downloader

A small web service which converts webfonts into zip archives
Go
24
star
42

conncrypt

Symmetrically encrypt your Go net.Conns
Go
19
star
43

s3hook

Transparent Client-side S3 Request Signing
JavaScript
19
star
44

grunt-source

Reuse a Grunt environment across multiple projects
JavaScript
18
star
45

aoc-in-go

A template repository for rapidly writing Advent of Code solutions in Go
Go
17
star
46

docker-cloud-torrent-openvpn

cloud-torrent and OpenVPN in a docker container
Shell
17
star
47

longestcommon

Longest common prefix/suffix across of list of strings in Go (Golang)
Go
15
star
48

node-gitlab-deploy

Deploy a Node server via a Gitlab Webhook
JavaScript
13
star
49

mega-stream

Stream media content from Mega
JavaScript
13
star
50

go-template

An automatic cross-compiling Go (golang) repository template using goreleaser and Github actions
Shell
12
star
51

js-play

A JavaScript playground/sandbox for learning, testing and prototyping
CSS
12
star
52

ansi

Easy to use ANSI control codes
Go
12
star
53

subfwd

URL shortening via sub-domains, written in Go
HTML
12
star
54

go-echo-server

View your requests in JSON format
Go
10
star
55

node-echo-server

Responds with the JSONified Request
JavaScript
10
star
56

ddns-daemon

A Simple Dynamic DNS Daemon using Node.js and Route53
JavaScript
10
star
57

whos-home

ARP scan your subnet and POST findings
Go
10
star
58

upnpctl

A small UPnP client
Go
10
star
59

dynflare

DynamicDNS using Cloudflare
Go
9
star
60

pnode

peer-to-peer dnode over anything!
JavaScript
9
star
61

go433

Send and receive 433 MHz using a RaspberryPi and Go
Go
9
star
62

sockfwd

Forward a unix socket to a tcp socket
Go
9
star
63

uploader

A small server to receive files over HTTP
JavaScript
8
star
64

vip

An IPv4 addressing Go (golang) module, based on uint32 instead of []byte
Go
8
star
65

ipflare

Find your public IP address according to Cloudflare
Go
8
star
66

eventsource

An eventsource event encoder in Go (golang)
Go
7
star
67

node-ssh-http-agent

An HTTP agent for tunnelling through SSH connections
JavaScript
7
star
68

sleep-on-lan

Send your computer to sleep via HTTP
JavaScript
7
star
69

sizestr

Pretty print byte counts in Go
Go
6
star
70

pnode-store

A synchronized data store between connected Node.js applications
JavaScript
6
star
71

docker-caddy

Caddy in a docker container, configurable via a simple web UI
Dockerfile
6
star
72

castlebot

🏰 A bot for your castle
Go
5
star
73

docker-vpn

Dockerized SoftEther VPN with a Web GUI
Shell
5
star
74

node-imdb-sort

Sort files based on IMDB data
CoffeeScript
5
star
75

jquery.prompt

Styled text prompts any element
CoffeeScript
5
star
76

gswg-io

Getting Started with Grunt - Homepage
HTML
5
star
77

requestlog

Simple request logging in Go (golang)
Go
5
star
78

compilejs

A mini Grunt.js for the browser
CoffeeScript
5
star
79

go-realtime

Keep your Go structs in sync with your JS objects
JavaScript
4
star
80

cookieauth

Cookie-based Basic-Authentication HTTP middleware for Go (golang)
Go
4
star
81

opts-examples

A Go (golang) package for building frictionless command-line interfaces
Go
4
star
82

go-mime

Extends pkg/mime with embedded mime types
Go
4
star
83

ipmath

IP Address Math in Go (golang)
Go
3
star
84

tranquil

Generate powerful RESTful JSON APIs
CoffeeScript
3
star
85

prettyprinter

Simple Pretty Printer using Google's Prettify
HTML
3
star
86

puzzler

A programming puzzle framework in Go
Go
3
star
87

goff

Concatenate audio files, built with Go and FFmpeg
Go
3
star
88

md-tmpl

Simple markdown templating using shell commands
Go
3
star
89

github-badge-maker

Github Badge Maker
JavaScript
3
star
90

opts-talk

A talk on opts, for the Sydney Go Meetup
Go
3
star
91

maplock

A map of locks in Go
Go
3
star
92

grunt-source-web

A Grunt Source project to build optimized static websites
CoffeeScript
3
star
93

xtls

TLS utils
Go
2
star
94

playground

Next version of https://js.jpillora.com
2
star
95

node-king

The king of your nodes - A powerful command and control center for your server infrastructure
JavaScript
2
star
96

webscan

Scans the entire Web for particular server types and devices
2
star
97

bookshelf

Your personal bookshelf
HTML
2
star
98

debator-lander

Interactive and transparent debates online
2
star
99

js-the-game-of-life

Interactive Game of Life in JavaScript
JavaScript
2
star
100

vigilant

Simple CLI tool for running multiple CLI tools in the same process
JavaScript
2
star