|
| 1 | +chrome.devtools.panels.create( |
| 2 | + "My Panel", |
| 3 | + "icon.png", |
| 4 | + "devtools.html", |
| 5 | + function (panel) {} |
| 6 | +); |
| 7 | + |
| 8 | +import { Cookie, Header, QueryString } from "har-format"; |
| 9 | + |
| 10 | + |
| 11 | +interface RequestSample { |
| 12 | + id: string; |
| 13 | + request: { |
| 14 | + method: string; |
| 15 | + url: string; |
| 16 | + body: string | undefined; |
| 17 | + cookies: Cookie[]; |
| 18 | + headers: Header[]; |
| 19 | + queryString: QueryString[]; |
| 20 | + href: string; |
| 21 | + search: string; |
| 22 | + pathname: string; |
| 23 | + }; |
| 24 | + response: { |
| 25 | + status: number; |
| 26 | + body: string; |
| 27 | + headers: Header[]; |
| 28 | + }; |
| 29 | +} |
| 30 | +let isFirst = true; |
| 31 | +let filterURLRegex = "github.com"; |
| 32 | +let items: { [pathname: string]: RequestSample[] } = {}; |
| 33 | +const requestSamplerListener = (req: chrome.devtools.network.Request) => { |
| 34 | + req.getContent((content) => { |
| 35 | + const { request, response } = req; |
| 36 | + if (request.url) { |
| 37 | + const url = new URL(request.url); |
| 38 | + |
| 39 | + // only save requests that passes the given URL regex |
| 40 | + if (!url.pathname.includes(filterURLRegex)) return; |
| 41 | + const requestSample: RequestSample = { |
| 42 | + id: req._request_id as string, |
| 43 | + request: { |
| 44 | + method: request.method, |
| 45 | + url: request.url, |
| 46 | + body: request.postData?.text, |
| 47 | + cookies: request.cookies, |
| 48 | + headers: request.headers, |
| 49 | + queryString: request.queryString, |
| 50 | + href: url.href, |
| 51 | + search: url.search, |
| 52 | + pathname: url.pathname, |
| 53 | + }, |
| 54 | + response: { |
| 55 | + status: response.status, |
| 56 | + body: content, |
| 57 | + headers: response.headers, |
| 58 | + }, |
| 59 | + }; |
| 60 | + if (isFirst) { |
| 61 | + chrome.storage.sync.clear(); |
| 62 | + isFirst = false; |
| 63 | + } else { |
| 64 | + if (!items[requestSample.request.pathname]) { |
| 65 | + items[requestSample.request.pathname] = []; |
| 66 | + } |
| 67 | + items[requestSample.request.pathname].push(requestSample); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | +}; |
| 72 | +chrome.devtools.network.onRequestFinished.addListener(requestSamplerListener); |
| 73 | + |
| 74 | +function saveDBAsJSON() { |
| 75 | + chrome.storage.sync.get((items) => { |
| 76 | + console.log(JSON.stringify(items)); |
| 77 | + }); |
| 78 | +} |
| 79 | +document |
| 80 | + .getElementById("save-db-as-json")! |
| 81 | + .addEventListener("click", saveDBAsJSON); |
| 82 | +function startSampling() {} |
| 83 | +function stopSampling() { |
| 84 | + chrome.storage.sync.clear(); |
| 85 | + |
| 86 | + chrome.devtools.network.onRequestFinished.removeListener( |
| 87 | + requestSamplerListener |
| 88 | + ); |
| 89 | +} |
0 commit comments