-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgazelle_proxy.user.js
69 lines (60 loc) · 2.47 KB
/
gazelle_proxy.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ==UserScript==
// @name New features for the Gazelle proxy
// @namespace ahdis
// @match https://gazelle.ihe.net/proxy/messages/http.seam*
// @grant none
// @version 1.0.1
// @author Quentin Ligier
// @description This script allows to automatically decompress GZIP-encoded messages.
// @updateURL https://github.com/ahdis/userscripts/raw/master/gazelle_proxy.user.js
// ==/UserScript==
const headers = document.querySelector('pre').innerText;
if (headers.includes('Content-Encoding: gzip')) {
const downloadButton = document.querySelectorAll('span.gzl-icon-download + input')[1];
const largerId = downloadButton.name;
const parts = downloadButton.name.split(':');
const smallerId = `${parts[0]}:${parts[1]}`;
const params = new URLSearchParams();
params.set(smallerId, smallerId);
params.set(largerId, 'Télécharger le fichier');
params.set('javax.faces.ViewState', document.querySelector(`form[id="${smallerId}"] input[name="javax.faces.ViewState"]`).value);
fetch('https://gazelle.ihe.net/proxy/messages/http.seam', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: params
}).then(async res => {
const ds = new DecompressionStream("gzip");
const blob = await res.blob();
const decompressedStream = blob.stream().pipeThrough(ds);
let decoded = await new Response(decompressedStream).text();
if (decoded[0] === '[' || decoded[0] === '{') {
decoded = indentJson(decoded);
} else if (decoded[0] === '<') {
decoded = indentXml(decoded);
}
const messageDiv = document.querySelector(`form[id="${parts[0]}:insertForm"] div.well`);
messageDiv.innerHTML = `ahdis userscript: the message has been GZIP-decompressed:\n\n<pre>${escapeHtml(decoded)}</pre>`;
});
}
function indentXml(xml) {
let formatted = "",
indent = "";
const tab = " ";
xml.split(/>\s*</).forEach(function (node) {
if (node.match(/^\/\w/)) indent = indent.substring(tab.length); // decrease indent by one 'tab'
formatted += indent + "<" + node + ">\r\n";
if (node.match(/^<?\w[^>]*[^\/]$/)) indent += tab; // increase indent
});
return formatted.substring(1, formatted.length - 3);
}
function indentJson(json) {
return JSON.stringify(JSON.parse(json), null, 2);
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}