Created
February 3, 2020 10:32
-
-
Save gnuton/afb9867de796459170d8725bb2b4ff4d to your computer and use it in GitHub Desktop.
JS Fetch sniffer v 0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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