github.com/miguelangel-nubla/oauth2commander@v0.3.7/oauth2server/defaultPage.html (about) 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 7 <title>Command Execution</title> 8 <style> 9 :root { 10 color: white; 11 --background-color: #f8f9fa; 12 --button-color: #007bff; 13 --button-color-hover: #0056b3; 14 --font-stack: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; 15 --font-stack-monospace: 'Source Code Pro', 'Consolas', 'Menlo', 'Monaco', 'Courier New', Courier, monospace; 16 } 17 18 body { 19 font-family: var(--font-stack); 20 background-color: var(--background-color); 21 padding: 20px; 22 background-color: black; 23 } 24 25 #loading { 26 display: none; 27 margin-top: 10px; 28 } 29 30 .stdout { 31 color: white; 32 } 33 34 .stderr { 35 color: red; 36 } 37 38 .stdout, .stderr { 39 font-family: var(--font-stack-monospace); 40 white-space: pre; 41 } 42 43 /* Modal styles */ 44 .modal-bg { 45 position: fixed; 46 top: 0; 47 left: 0; 48 width: 100%; 49 height: 100%; 50 background: rgba(0, 0, 0, 0.5); 51 display: flex; 52 align-items: center; 53 justify-content: center; 54 z-index: 9999; 55 } 56 57 .modal { 58 color: black; 59 background: white; 60 padding: 20px; 61 border-radius: 5px; 62 width: 300px; 63 text-align: center; 64 } 65 66 .modal button { 67 padding: 5px 15px; 68 margin: 5px; 69 border: none; 70 border-radius: 3px; 71 cursor: pointer; 72 font-size: 1.2em; 73 } 74 </style> 75 </head> 76 77 <body> 78 <div id="loading">Loading...</div> 79 <div id="output"></div> 80 81 <script> 82 document.addEventListener('DOMContentLoaded', function () { 83 const loading = document.getElementById('loading'); 84 const outputDiv = document.getElementById('output'); 85 86 const fetchAndHandleResponse = async () => { 87 try { 88 const response = await fetch(window.location.pathname, { 89 method: 'POST', 90 headers: { 91 'Content-Type': 'application/json', 92 'X-CSRF-Token': '{{.CSRFToken}}' // TODO: Set this from a cookie or another mechanism 93 } 94 }); 95 96 const contentType = response.headers.get('Content-Type'); 97 98 if (contentType.includes('application/x-ndjson')) { 99 await handleNDJSONStream(response); 100 } else if (contentType.includes('application/octet-stream')) { 101 await downloadBinaryFile(response); 102 } else { 103 await showResponseAsIs(response); 104 } 105 } catch (error) { 106 console.error("Error:", error); 107 } finally { 108 loading.style.display = 'none'; 109 } 110 }; 111 112 // Handle NDJSON stream 113 const handleNDJSONStream = async (response) => { 114 const reader = response.body.getReader(); 115 const decoder = new TextDecoder('utf-8'); 116 let buffer = ''; 117 118 return new Promise((resolve, reject) => { 119 const processChunk = async ({ done, value }) => { 120 if (done) { 121 if (buffer.trim() !== '') { 122 try { 123 const json = JSON.parse(buffer.trim()); 124 handleJsonObject(json); 125 } catch (e) { 126 reject(new Error("Invalid NDJSON format")); 127 } 128 } 129 return resolve(); 130 } 131 132 buffer += decoder.decode(value); 133 const lines = buffer.split('\n'); 134 135 for (let i = 0; i < lines.length - 1; i++) { 136 const line = lines[i].trim(); 137 if (line) { 138 try { 139 const json = JSON.parse(line); 140 handleJsonObject(json); 141 } catch (e) { 142 reject(new Error("Invalid NDJSON format")); 143 } 144 } 145 } 146 buffer = lines[lines.length - 1]; 147 reader.read().then(processChunk, reject); 148 }; 149 150 reader.read().then(processChunk, reject); 151 }); 152 }; 153 154 function showCharacterCodes(str) { 155 for (let i = 0; i < str.length; i++) { 156 console.log(`${str[i]}: ${str.charCodeAt(i)}`); 157 } 158 } 159 160 // Handle individual JSON object 161 const handleJsonObject = (json) => { 162 console.log(json); 163 const lineDiv = document.createElement('div'); 164 lineDiv.className = json.source; 165 lineDiv.textContent = json.text; 166 outputDiv.appendChild(lineDiv); 167 168 showCharacterCodes(json.text); 169 }; 170 171 // Download binary file 172 const downloadBinaryFile = async (response) => { 173 const blob = await response.blob(); 174 const a = document.createElement('a'); 175 a.href = URL.createObjectURL(blob); 176 a.download = 'downloaded_file'; 177 a.style.display = 'none'; 178 document.body.appendChild(a); 179 a.click(); 180 document.body.removeChild(a); 181 }; 182 183 // Show response as plain text 184 const showResponseAsIs = async (response) => { 185 const text = await response.text(); 186 const lineDiv = document.createElement('div'); 187 lineDiv.textContent = text; 188 outputDiv.appendChild(lineDiv); 189 }; 190 191 // Function that mimics the `confirm` but asynchronously 192 function asyncConfirm(message) { 193 return new Promise((resolve) => { 194 const modal = document.createElement('div'); 195 modal.innerHTML = ` 196 <div class="modal-bg"> 197 <div class="modal"> 198 <p>${message}</p> 199 <button id="confirmYes">Yes</button> 200 <button id="confirmNo">No</button> 201 </div> 202 </div> 203 `; 204 document.body.appendChild(modal); 205 206 modal.querySelector('#confirmYes').onclick = () => { 207 modal.remove(); 208 resolve(true); 209 }; 210 211 modal.querySelector('#confirmNo').onclick = () => { 212 modal.remove(); 213 resolve(false); 214 }; 215 }); 216 } 217 218 (async function() { 219 try { 220 const confirmed = await asyncConfirm("Are you sure you want to run " + window.location.pathname + window.location.search + "?"); 221 if (!confirmed) throw new Error('User declined.'); 222 223 loading.style.display = 'block'; 224 await fetchAndHandleResponse(); 225 } catch (error) { 226 console.error("Error:", error); 227 } finally { 228 loading.style.display = 'none'; 229 } 230 })(); 231 }); 232 </script> 233 </body> 234 235 </html>