Skip to content

Commit 93a86df

Browse files
author
RaizeTheLimit
committed
allow downloading JSON version of requests
1 parent 313b4d6 commit 93a86df

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

src/views/print-protos.html

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,33 @@ <h5 class="modal-title">
126126
</div>
127127
<div class="modal-body">
128128
<p>Method name: <span class="method-name"></span></p>
129-
<p>Incoming data:</p>
129+
<p>Request data (Sent to server):</p>
130130
<pre class="method-data-incoming"></pre>
131-
<p>Outgoing data:</p>
131+
<p>Response data (Received from server):</p>
132132
<pre class="method-data-outgoing"></pre>
133133
</div>
134134
<div class="modal-footer">
135+
<button
136+
type="button"
137+
class="btn btn-primary"
138+
id="download-incoming-json"
139+
>
140+
<i class="fas fa-download"></i> Download Request
141+
</button>
142+
<button
143+
type="button"
144+
class="btn btn-primary"
145+
id="download-outgoing-json"
146+
>
147+
<i class="fas fa-download"></i> Download Response
148+
</button>
149+
<button
150+
type="button"
151+
class="btn btn-success"
152+
id="download-combined-json"
153+
>
154+
<i class="fas fa-download"></i> Download Both
155+
</button>
135156
<button
136157
type="button"
137158
class="btn btn-secondary"
@@ -264,9 +285,11 @@ <h5 class="modal-title">
264285
);
265286
}
266287
});
288+
let currentModalMessage = null;
267289
$("#data_socket tbody").on("click", "tr", function () {
268290
const message = messages.filter((msg) => msg.id == $(this)[0].id)[0];
269291
if (message == null) return;
292+
currentModalMessage = message;
270293
$("#modal-detail").find(".method-id").html(message.methodId);
271294
$("#modal-detail").find(".method-name").html(message.methodName);
272295
$("#modal-detail")
@@ -277,6 +300,47 @@ <h5 class="modal-title">
277300
.jsonViewer(message.response.data);
278301
$("#modal-detail").modal();
279302
});
303+
function downloadJSON(data, filename) {
304+
const jsonStr = JSON.stringify(data, null, 2);
305+
const blob = new Blob([jsonStr], { type: "application/json" });
306+
const url = URL.createObjectURL(blob);
307+
const a = document.createElement("a");
308+
a.href = url;
309+
a.download = filename;
310+
document.body.appendChild(a);
311+
a.click();
312+
document.body.removeChild(a);
313+
URL.revokeObjectURL(url);
314+
}
315+
316+
$("#download-incoming-json").click(function () {
317+
if (!currentModalMessage) return;
318+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
319+
const filename = `request_${currentModalMessage.methodName}_${currentModalMessage.methodId}_${timestamp}.json`;
320+
downloadJSON(currentModalMessage.data, filename);
321+
});
322+
323+
$("#download-outgoing-json").click(function () {
324+
if (!currentModalMessage || !currentModalMessage.response) return;
325+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
326+
const filename = `response_${currentModalMessage.methodName}_${currentModalMessage.methodId}_${timestamp}.json`;
327+
downloadJSON(currentModalMessage.response.data, filename);
328+
});
329+
330+
$("#download-combined-json").click(function () {
331+
if (!currentModalMessage) return;
332+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
333+
const filename = `combined_${currentModalMessage.methodName}_${currentModalMessage.methodId}_${timestamp}.json`;
334+
const combinedData = {
335+
identifier: currentModalMessage.identifier,
336+
methodId: currentModalMessage.methodId,
337+
methodName: currentModalMessage.methodName,
338+
request: currentModalMessage.data,
339+
response: currentModalMessage.response ? currentModalMessage.response.data : null
340+
};
341+
downloadJSON(combinedData, filename);
342+
});
343+
280344
$("#darkmode-button").click(function () {
281345
if (document.body.classList.contains("dark")) {
282346
document.body.classList.remove("dark");

0 commit comments

Comments
 (0)