-
-
Save comigor/3d1b46fca61a0063218aaa16c8b09d6a to your computer and use it in GitHub Desktop.
Export all intercepted HTTP exchanges from HTTP Toolkit to JSON
This file contains hidden or 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
| // HTTP Toolkit full export v2 (requests + responses + bodies). | |
| // Paste into the DevTools console inside the HTTP Toolkit desktop app. | |
| (async function () { | |
| var findEventsStore = function () { | |
| var elements = document.querySelectorAll('*'); | |
| for (var i = 0; i < elements.length; i++) { | |
| var el = elements[i]; | |
| var keys = Object.keys(el); | |
| for (var j = 0; j < keys.length; j++) { | |
| var key = keys[j]; | |
| if (key.startsWith('__reactFiber') || key.startsWith('__reactInternalInstance')) { | |
| var current = el[key]; | |
| for (var depth = 0; depth < 100 && current; depth++) { | |
| try { | |
| if (current.memoizedProps && current.memoizedProps.eventsStore) return current.memoizedProps.eventsStore; | |
| if (current.memoizedState && current.memoizedState.eventsStore) return current.memoizedState.eventsStore; | |
| } catch (e) {} | |
| current = current.return; | |
| } | |
| } | |
| } | |
| } | |
| return null; | |
| }; | |
| var toBytes = function (chunk) { | |
| if (!chunk) return new Uint8Array(0); | |
| if (chunk instanceof Uint8Array) return chunk; | |
| if (chunk.data) return Uint8Array.from(chunk.data); // serialized {type:'Buffer',data:[...]} | |
| if (typeof chunk === 'string') return new TextEncoder().encode(chunk); | |
| return new Uint8Array(0); | |
| }; | |
| var extractBody = async function (body) { | |
| if (!body) return null; | |
| var bytes = null, note = null; | |
| // Preferred: let the body decode itself (handles gzip/br/etc). | |
| try { | |
| if (typeof body.waitForDecoding === 'function') { | |
| await body.waitForDecoding(); | |
| if (body.decodedData && body.decodedData.length) { | |
| bytes = toBytes(body.decodedData); | |
| note = 'decoded'; | |
| } | |
| } | |
| } catch (e) { note = 'decode-failed: ' + e.message; } | |
| // Fallback: raw encoded chunks, exactly as received on the wire. | |
| if ((!bytes || !bytes.length) && Array.isArray(body._encodedChunks)) { | |
| var parts = body._encodedChunks.map(toBytes); | |
| var total = parts.reduce(function (n, a) { return n + a.length; }, 0); | |
| bytes = new Uint8Array(total); | |
| var off = 0; | |
| for (var k = 0; k < parts.length; k++) { bytes.set(parts[k], off); off += parts[k].length; } | |
| var enc = body._contentEncoding || []; | |
| note = note || (enc.length ? 'encoded:' + enc.join('+') : 'raw'); | |
| } | |
| if (!bytes || !bytes.length) { | |
| return body._encodedByteLength | |
| ? { note: note || 'no-bytes', encodedByteLength: body._encodedByteLength } | |
| : null; | |
| } | |
| // Chunked base64 (avoids call-arg limit on large bodies). | |
| var bin = ''; | |
| for (var s = 0; s < bytes.length; s += 0x8000) { | |
| bin += String.fromCharCode.apply(null, bytes.subarray(s, s + 0x8000)); | |
| } | |
| var text = null; | |
| try { text = new TextDecoder('utf-8', { fatal: true }).decode(bytes); } catch (e) {} | |
| return { | |
| encodedByteLength: body._encodedByteLength, | |
| byteLength: bytes.length, | |
| note: note, | |
| encoding: 'base64', | |
| base64: btoa(bin), | |
| text: text | |
| }; | |
| }; | |
| var store = findEventsStore(); | |
| if (!store) { console.error('Events store not found.'); return; } | |
| var list = store.eventsList; | |
| var exchanges = Array.from(list.exchanges || list._exchanges || []); | |
| console.log('Found ' + exchanges.length + ' exchanges, decoding bodies...'); | |
| var data = []; | |
| var failed = 0; | |
| for (var idx = 0; idx < exchanges.length; idx++) { | |
| var evt = exchanges[idx]; | |
| try { | |
| if (!evt.request) continue; // skip non-exchange events | |
| var item = { | |
| id: evt.id, | |
| timingEvents: evt.timingEvents || null, | |
| request: { | |
| method: evt.request.method, | |
| url: evt.request.url || (evt.request.parsedUrl && evt.request.parsedUrl.toString()), | |
| path: evt.request.path, | |
| httpVersion: evt.request.httpVersion, | |
| headers: evt.request.headers, | |
| rawHeaders: evt.request.rawHeaders, | |
| body: await extractBody(evt.request.body) | |
| }, | |
| response: null | |
| }; | |
| if (evt.response && evt.response !== 'aborted') { | |
| item.response = { | |
| statusCode: evt.response.statusCode, | |
| statusMessage: evt.response.statusMessage, | |
| headers: evt.response.headers, | |
| rawHeaders: evt.response.rawHeaders, | |
| rawTrailers: evt.response.rawTrailers, | |
| body: await extractBody(evt.response.body) | |
| }; | |
| } else if (evt.response === 'aborted') { | |
| item.response = 'aborted'; | |
| } | |
| data.push(item); | |
| } catch (e) { | |
| failed++; | |
| console.warn('exchange ' + idx + ' failed:', e); | |
| } | |
| if (idx % 100 === 0) console.log(idx + '/' + exchanges.length); | |
| } | |
| var filename = 'http-toolkit-export-' + new Date().toISOString().slice(0, 19).replace(/[:T]/g, '-') + '.json'; | |
| var blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' }); | |
| var a = document.createElement('a'); | |
| a.href = URL.createObjectURL(blob); | |
| a.download = filename; | |
| document.body.appendChild(a); | |
| a.click(); | |
| document.body.removeChild(a); | |
| URL.revokeObjectURL(a.href); | |
| console.log('Exported ' + data.length + ' exchanges (' + failed + ' failed) to ' + filename); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment