Skip to content

Instantly share code, notes, and snippets.

@gnuton
Created February 3, 2020 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuton/afb9867de796459170d8725bb2b4ff4d to your computer and use it in GitHub Desktop.
Save gnuton/afb9867de796459170d8725bb2b4ff4d to your computer and use it in GitHub Desktop.
JS Fetch sniffer v 0.1
// This PoC code can hook into a JS app, and sniff for all REST connections happening
// storing them into local storage
//
// How to use it
// Open crome, press F12 in the page you wanna sniff the connections
// launch
initFetchSniffer()
let response = await fetch(`https://api.github.com/users/gnuton`);
await response.json()
fetchLog()
//------- The Code -----------
const initFetchSniffer= () => {
// Initialize localStorage and functions
const LS_FETCH_LOG_KEY = 'fetchLog'
localStorage.setItem(LS_FETCH_LOG_KEY, JSON.stringify([]))
window.fetchLog = () => {
return JSON.parse(localStorage.getItem(LS_FETCH_LOG_KEY))
}
window.storeFetchLog = (data) => {
const logs = fetchLog();
logs.push(data);
localStorage.setItem(LS_FETCH_LOG_KEY, JSON.stringify(logs));
}
// Override fetch function
window.original_fetch = fetch
fetch = (...args) => {
storeFetchLog(...args);
return window.original_fetch(...args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment