| 1 | <!DOCTYPE html> |
| 2 | <html lang="en"><head><meta charset="utf-8"> |
| 3 | <meta name="viewport" content="width=device-width,initial-scale=1"> |
| 4 | <title>webhook</title> |
| 5 | <!--TOKENS--> |
| 6 | <style> |
| 7 | *{box-sizing:border-box} |
| 8 | html,body{margin:0} |
| 9 | body{background:var(--bg);color:var(--text);font-family:var(--font);font-size:14px; |
| 10 | line-height:1.5;-webkit-font-smoothing:antialiased} |
| 11 | .wrap{max-width:1040px;margin:0 auto;padding:16px} |
| 12 | .head{display:flex;align-items:flex-start;gap:12px;flex-wrap:wrap} |
| 13 | h1{font-size:20px;margin:0 0 2px;font-weight:650;letter-spacing:-.01em} |
| 14 | .sub{color:var(--text-2);font-size:12.5px} |
| 15 | .sub2{color:var(--text-2);font-size:11.5px;margin-top:2px} |
| 16 | .spacer{flex:1 1 auto} |
| 17 | .card{background:var(--bg-card);border:1px solid var(--line);border-radius:14px; |
| 18 | padding:16px;margin-bottom:14px} |
| 19 | .card h2{font-size:12px;font-weight:650;margin:0 0 12px;color:var(--text-2); |
| 20 | text-transform:uppercase;letter-spacing:.06em} |
| 21 | .row{display:flex;flex-wrap:wrap;gap:10px;align-items:flex-end} |
| 22 | .col{display:flex;flex-direction:column;gap:8px} |
| 23 | label{display:block;font-size:11px;color:var(--text-2);margin-bottom:4px; |
| 24 | text-transform:uppercase;letter-spacing:.05em} |
| 25 | input,select,textarea{background:var(--input-bg);border:1px solid var(--line); |
| 26 | color:var(--text);border-radius:9px;padding:7px 10px;font-size:13.5px; |
| 27 | font-family:var(--font);outline:none;min-width:0;width:100%} |
| 28 | textarea{font-family:var(--mono);font-size:12.5px;resize:vertical;min-height:88px; |
| 29 | line-height:1.45} |
| 30 | input:focus,select:focus,textarea:focus{border-color:var(--blue)} |
| 31 | button{background:var(--blue);color:#fff;border:0;border-radius:980px; |
| 32 | padding:7px 15px;font-size:13px;font-weight:600;cursor:pointer;font-family:var(--font)} |
| 33 | button.ghost{background:var(--surface-2);color:var(--text)} |
| 34 | button.danger{background:var(--red)} |
| 35 | button.tiny{padding:3px 10px;font-size:12px;border-radius:8px} |
| 36 | button:active{transform:scale(.98)} |
| 37 | button:disabled{opacity:.55;cursor:default} |
| 38 | .grow1{flex:1 1 200px}.grow2{flex:2 1 320px} |
| 39 | table{width:100%;border-collapse:collapse;font-size:13px} |
| 40 | th{text-align:left;font-size:10.5px;color:var(--text-2);text-transform:uppercase; |
| 41 | letter-spacing:.05em;font-weight:600;padding:0 8px 7px 0; |
| 42 | border-bottom:1px solid var(--line);white-space:nowrap} |
| 43 | td{padding:9px 8px 9px 0;border-bottom:1px solid var(--line);vertical-align:top} |
| 44 | tr:last-child td{border-bottom:0} |
| 45 | td.mono,.mono{font-family:var(--mono);font-size:12px; |
| 46 | font-variant-numeric:tabular-nums;word-break:break-all} |
| 47 | .tbl-wrap{overflow-x:auto;-webkit-overflow-scrolling:touch} |
| 48 | .pill{display:inline-block;font-size:10.5px;font-weight:600;padding:2px 8px; |
| 49 | border-radius:980px;border:1px solid var(--line);color:var(--text-2); |
| 50 | background:var(--surface-2);white-space:nowrap} |
| 51 | .pill.ok{color:var(--green);border-color:rgba(48,209,88,.35)} |
| 52 | .pill.bad{color:var(--red);border-color:rgba(255,69,58,.35)} |
| 53 | .pill.sig{color:var(--blue);border-color:rgba(10,132,255,.35)} |
| 54 | .pill.muted{color:var(--text-2)} |
| 55 | .empty{color:var(--text-2);font-size:12.5px;padding:10px 0} |
| 56 | .hint{color:var(--text-2);font-size:11.5px;margin-top:6px} |
| 57 | .out{margin-top:12px;padding:12px;border-radius:11px;background:var(--code-bg); |
| 58 | font-family:var(--mono);font-size:12px;white-space:pre-wrap; |
| 59 | word-break:break-word;display:none} |
| 60 | .out.on{display:block} |
| 61 | .out.err{color:var(--red)} |
| 62 | .hdr-row{display:grid;grid-template-columns:1fr 1fr auto;gap:8px;align-items:end} |
| 63 | .icon-btn{background:transparent;border:1px solid var(--line);color:var(--text-2); |
| 64 | border-radius:8px;padding:6px 9px;font-size:12px} |
| 65 | .hidden{display:none!important} |
| 66 | .foot{color:var(--text-2);font-size:11.5px;margin-top:2px} |
| 67 | kbd{font-family:var(--mono);font-size:11px;background:var(--surface-2); |
| 68 | border:1px solid var(--line);border-radius:5px;padding:1px 5px} |
| 69 | /* A five-column table cannot fit 390 px without squeezing the name into a |
| 70 | * vertical ribbon, so below 700 px each row becomes a labelled block. */ |
| 71 | @media(max-width:700px){ |
| 72 | #tbl thead{display:none} |
| 73 | #tbl,#tbl tbody{display:block} |
| 74 | #tbl tr{display:block;border:1px solid var(--line);border-radius:12px; |
| 75 | padding:10px 12px;margin-bottom:10px} |
| 76 | #tbl td{display:grid;grid-template-columns:76px minmax(0,1fr);gap:8px; |
| 77 | padding:2px 0;border:0;align-items:baseline;word-break:break-word} |
| 78 | #tbl td::before{content:attr(data-label);color:var(--text-2);font-size:10.5px; |
| 79 | text-transform:uppercase;letter-spacing:.05em} |
| 80 | #tbl td > .hint{grid-column:2;color:var(--text-2);font-size:11.5px} |
| 81 | #tbl td.actions{grid-template-columns:1fr 1fr;margin-top:8px;gap:6px} |
| 82 | #tbl td.actions::before{content:none} |
| 83 | #tbl td > span,#tbl td > button{justify-self:start} |
| 84 | #tbl td.actions > button{justify-self:stretch} |
| 85 | /* The delivery log has the same problem: six columns at 390 px. */ |
| 86 | #logtbl thead{display:none} |
| 87 | #logtbl,#logtbl tbody{display:block} |
| 88 | #logtbl tr{display:block;border-bottom:1px solid var(--line);padding:8px 0} |
| 89 | #logtbl td{display:grid;grid-template-columns:86px minmax(0,1fr);gap:8px; |
| 90 | padding:1px 0;border:0;word-break:break-word} |
| 91 | #logtbl td::before{content:attr(data-label);color:var(--text-2);font-size:10.5px; |
| 92 | text-transform:uppercase;letter-spacing:.05em} |
| 93 | #logtbl td > span{justify-self:start} |
| 94 | } |
| 95 | @media(max-width:640px){ |
| 96 | .wrap{padding:12px} |
| 97 | .hdr-row{grid-template-columns:1fr} |
| 98 | .head{gap:8px} |
| 99 | } |
| 100 | </style> |
| 101 | </head> |
| 102 | <body> |
| 103 | <div class="wrap"> |
| 104 | |
| 105 | <div class="head"> |
| 106 | <div> |
| 107 | <h1>webhook</h1> |
| 108 | <div class="sub">Named webhook & REST triggers — secrets stay on this node</div> |
| 109 | <div class="sub2" id="policynote"></div> |
| 110 | </div> |
| 111 | <div class="spacer"></div> |
| 112 | <button class="embed-hide icon-btn" id="themeToggle" type="button">Theme</button> |
| 113 | </div> |
| 114 | |
| 115 | <div class="card"> |
| 116 | <h2>Hooks <span id="count" class="pill muted">0</span></h2> |
| 117 | <div class="tbl-wrap"> |
| 118 | <table id="tbl"> |
| 119 | <thead><tr> |
| 120 | <th>Name</th><th>Method</th><th>Endpoint</th><th>Signature</th><th>Secrets</th><th></th> |
| 121 | </tr></thead> |
| 122 | <tbody id="rows"><tr><td colspan="6" class="empty">Loading…</td></tr></tbody> |
| 123 | </table> |
| 124 | </div> |
| 125 | <div class="row" style="margin-top:12px"> |
| 126 | <button id="btnNew" type="button">New hook</button> |
| 127 | <button class="ghost" id="btnReload" type="button">Reload</button> |
| 128 | </div> |
| 129 | </div> |
| 130 | |
| 131 | <div class="card hidden" id="editor"> |
| 132 | <h2 id="editorTitle">New hook</h2> |
| 133 | <div class="row"> |
| 134 | <div class="grow1"><label for="fName">Name</label> |
| 135 | <input id="fName" placeholder="deploy-note" autocomplete="off"></div> |
| 136 | <div class="grow1"><label for="fMethod">Method</label> |
| 137 | <select id="fMethod"> |
| 138 | <option>POST</option><option>GET</option><option>PUT</option> |
| 139 | <option>PATCH</option><option>DELETE</option><option>HEAD</option> |
| 140 | </select></div> |
| 141 | <div class="grow2"><label for="fUrl">URL</label> |
| 142 | <input id="fUrl" placeholder="https://example.com/hooks/abc" autocomplete="off"></div> |
| 143 | </div> |
| 144 | <div class="row" style="margin-top:10px"> |
| 145 | <div class="grow2"><label for="fDesc">Description</label> |
| 146 | <input id="fDesc" placeholder="Deploy notification" autocomplete="off"></div> |
| 147 | <div class="grow1"><label for="fSig">Signature</label> |
| 148 | <select id="fSig"> |
| 149 | <option value="">none</option> |
| 150 | <option value="github">github — X-Hub-Signature-256</option> |
| 151 | <option value="gitlab">gitlab — X-Gitlab-Token</option> |
| 152 | <option value="v2">v2 — timestamped HMAC</option> |
| 153 | </select></div> |
| 154 | <div class="grow1"><label for="fSecret">HMAC secret</label> |
| 155 | <input id="fSecret" type="password" autocomplete="new-password" |
| 156 | placeholder="leave blank to keep"></div> |
| 157 | </div> |
| 158 | |
| 159 | <div style="margin-top:12px"> |
| 160 | <label>Headers</label> |
| 161 | <div class="col" id="hdrList"></div> |
| 162 | <div class="row" style="margin-top:8px"> |
| 163 | <button class="ghost tiny" id="btnAddHdr" type="button">Add header</button> |
| 164 | </div> |
| 165 | <div class="hint">A stored value is never sent back to the browser. Leave the |
| 166 | value blank to keep the secret already saved for that header name.</div> |
| 167 | </div> |
| 168 | |
| 169 | <div class="row" style="margin-top:14px"> |
| 170 | <button id="btnSave" type="button">Save hook</button> |
| 171 | <button class="ghost" id="btnCancel" type="button">Cancel</button> |
| 172 | <span id="saveMsg" class="hint"></span> |
| 173 | </div> |
| 174 | </div> |
| 175 | |
| 176 | <div class="card"> |
| 177 | <h2>Test fire</h2> |
| 178 | <div class="row"> |
| 179 | <div class="grow1"><label for="fFire">Hook</label> |
| 180 | <select id="fFire"></select></div> |
| 181 | <div class="grow2"><label for="fQuery">Query string (optional)</label> |
| 182 | <input id="fQuery" placeholder="event=deploy&env=prod" autocomplete="off"></div> |
| 183 | </div> |
| 184 | <div style="margin-top:10px"> |
| 185 | <label for="fPayload">Payload (JSON)</label> |
| 186 | <textarea id="fPayload">{ |
| 187 | "event": "deploy", |
| 188 | "service": "scorpiox-bot", |
| 189 | "status": "ok" |
| 190 | }</textarea> |
| 191 | </div> |
| 192 | <div class="row" style="margin-top:10px"> |
| 193 | <button id="btnFire" type="button">Send</button> |
| 194 | <span class="hint">Sent from this node with the stored headers and signature.</span> |
| 195 | </div> |
| 196 | <div class="out" id="fireOut"></div> |
| 197 | </div> |
| 198 | |
| 199 | <div class="card"> |
| 200 | <h2>Recent deliveries</h2> |
| 201 | <div class="tbl-wrap"> |
| 202 | <table id="logtbl"> |
| 203 | <thead><tr><th>When (UTC)</th><th>Hook</th><th>Status</th><th>Bytes</th><th>ms</th><th>Detail</th></tr></thead> |
| 204 | <tbody id="logRows"><tr><td colspan="6" class="empty">No deliveries yet.</td></tr></tbody> |
| 205 | </table> |
| 206 | </div> |
| 207 | </div> |
| 208 | |
| 209 | <div class="foot"> |
| 210 | Tool contract: <span class="mono">webhook_list</span>, |
| 211 | <span class="mono">webhook_trigger</span>. Press |
| 212 | <kbd>Ctrl</kbd>+<kbd>Enter</kbd> in the payload box to send. |
| 213 | </div> |
| 214 | </div> |
| 215 | |
| 216 | <script> |
| 217 | /* ------------------------------------------------------------------ routes |
| 218 | * Two call shapes, and getting this wrong is the classic plugin bug: |
| 219 | * served through the hub (bot.scorpiox.net/plugins?plugin=plugin_webhook…) |
| 220 | * -> call the hub proxy back: /plugins?plugin=plugin_webhook&action=… |
| 221 | * opened directly on the node |
| 222 | * -> the plugin IS the route: /plugin_webhook?action=… |
| 223 | * Never call /plugins?plugin= on the node itself: that is the discovery |
| 224 | * endpoint and it answers with the plugin list, not with this plugin. */ |
| 225 | var Q = new URLSearchParams(location.search); |
| 226 | var HUB = Q.get('plugin') || ''; |
| 227 | var NODE = Q.get('node') || ''; |
| 228 | var PW = Q.get('pwd') || (NODE && localStorage.getItem('bot_pwd_' + NODE)) || |
| 229 | localStorage.getItem('bot_active_pwd') || ''; |
| 230 | |
| 231 | function api(action, payload, method) { |
| 232 | var u = (HUB ? '/plugins?plugin=' + encodeURIComponent(HUB) |
| 233 | : '/plugin_webhook') + |
| 234 | '?action=' + encodeURIComponent(action) + '&format=json' + |
| 235 | (NODE ? '&node=' + encodeURIComponent(NODE) : '') + |
| 236 | (PW ? '&pwd=' + encodeURIComponent(PW) : ''); |
| 237 | var opt = { |
| 238 | method: method || (payload ? 'POST' : 'GET'), |
| 239 | headers: PW ? {'X-Bot-Password': PW} : {} |
| 240 | }; |
| 241 | if (payload) { |
| 242 | opt.headers['Content-Type'] = 'application/json'; |
| 243 | opt.body = JSON.stringify(payload); |
| 244 | } |
| 245 | return fetch(u, opt).then(function (r) { |
| 246 | return r.text().then(function (t) { |
| 247 | var d; |
| 248 | try { d = JSON.parse(t); } catch (e) { throw new Error('not JSON: ' + t.slice(0, 200)); } |
| 249 | if (!r.ok || d.ok === false) { |
| 250 | throw new Error(d.error || ('HTTP ' + r.status)); |
| 251 | } |
| 252 | return d; |
| 253 | }); |
| 254 | }); |
| 255 | } |
| 256 | |
| 257 | function el(id) { return document.getElementById(id); } |
| 258 | function clear(n) { while (n.firstChild) n.removeChild(n.firstChild); } |
| 259 | function txt(s) { return document.createTextNode(s == null ? '' : String(s)); } |
| 260 | |
| 261 | var state = { hooks: [], log: [], editing: null, allowPrivate: false }; |
| 262 | |
| 263 | function renderPolicy() { |
| 264 | el('policynote').textContent = state.allowPrivate |
| 265 | ? 'Private / loopback endpoints are allowed on this node (WEBHOOK_ALLOW_PRIVATE=1).' |
| 266 | : 'Outbound only to public addresses; loopback and private ranges are refused.'; |
| 267 | } |
| 268 | |
| 269 | function headersSummary(h) { |
| 270 | var n = (h.headers || []).length; |
| 271 | var s = []; |
| 272 | if (n) s.push(n + ' header' + (n === 1 ? '' : 's')); |
| 273 | if (h.has_secret) s.push('HMAC'); |
| 274 | return s.length ? s.join(' + ') : 'none'; |
| 275 | } |
| 276 | |
| 277 | function renderHooks() { |
| 278 | var tb = el('rows'); |
| 279 | clear(tb); |
| 280 | el('count').textContent = String(state.hooks.length); |
| 281 | if (!state.hooks.length) { |
| 282 | var tr = document.createElement('tr'); |
| 283 | var td = document.createElement('td'); |
| 284 | td.colSpan = 6; td.className = 'empty'; |
| 285 | td.appendChild(txt('No hooks yet. Create one to give the agent a tool it can fire.')); |
| 286 | tr.appendChild(td); tb.appendChild(tr); |
| 287 | } |
| 288 | state.hooks.forEach(function (h) { |
| 289 | var tr = document.createElement('tr'); |
| 290 | |
| 291 | var c1 = document.createElement('td'); |
| 292 | c1.className = 'mono'; |
| 293 | c1.dataset.label = 'Name'; |
| 294 | c1.appendChild(txt(h.name)); |
| 295 | if (h.desc) { |
| 296 | var d = document.createElement('div'); |
| 297 | d.className = 'hint'; |
| 298 | d.appendChild(txt(h.desc)); |
| 299 | c1.appendChild(d); |
| 300 | } |
| 301 | tr.appendChild(c1); |
| 302 | |
| 303 | var c2 = document.createElement('td'); |
| 304 | c2.dataset.label = 'Method'; |
| 305 | var p = document.createElement('span'); |
| 306 | p.className = 'pill'; p.appendChild(txt(h.method)); |
| 307 | c2.appendChild(p); |
| 308 | tr.appendChild(c2); |
| 309 | |
| 310 | var c3 = document.createElement('td'); |
| 311 | c3.className = 'mono'; |
| 312 | c3.dataset.label = 'Endpoint'; |
| 313 | c3.appendChild(txt(h.url || '')); |
| 314 | tr.appendChild(c3); |
| 315 | |
| 316 | var c4 = document.createElement('td'); |
| 317 | c4.dataset.label = 'Signature'; |
| 318 | var sp = document.createElement('span'); |
| 319 | if (h.sig) { sp.className = 'pill sig'; sp.appendChild(txt(h.sig)); } |
| 320 | else { sp.className = 'pill muted'; sp.appendChild(txt('none')); } |
| 321 | c4.appendChild(sp); |
| 322 | tr.appendChild(c4); |
| 323 | |
| 324 | var c5 = document.createElement('td'); |
| 325 | c5.className = 'hint'; |
| 326 | c5.dataset.label = 'Secrets'; |
| 327 | c5.appendChild(txt(headersSummary(h))); |
| 328 | tr.appendChild(c5); |
| 329 | |
| 330 | var c6 = document.createElement('td'); |
| 331 | c6.className = 'actions'; |
| 332 | c6.dataset.label = ''; |
| 333 | c6.style.whiteSpace = 'nowrap'; |
| 334 | var bEdit = document.createElement('button'); |
| 335 | bEdit.className = 'ghost tiny'; bEdit.type = 'button'; |
| 336 | bEdit.appendChild(txt('Edit')); |
| 337 | bEdit.onclick = function () { openEditor(h); }; |
| 338 | var bDel = document.createElement('button'); |
| 339 | bDel.className = 'ghost tiny'; bDel.type = 'button'; |
| 340 | bDel.style.marginLeft = '6px'; |
| 341 | bDel.appendChild(txt('Delete')); |
| 342 | bDel.onclick = function () { removeHook(h.name); }; |
| 343 | c6.appendChild(bEdit); c6.appendChild(bDel); |
| 344 | tr.appendChild(c6); |
| 345 | |
| 346 | tb.appendChild(tr); |
| 347 | }); |
| 348 | |
| 349 | var sel = el('fFire'); |
| 350 | var keep = sel.value; |
| 351 | clear(sel); |
| 352 | state.hooks.forEach(function (h) { |
| 353 | var o = document.createElement('option'); |
| 354 | o.value = h.name; o.appendChild(txt(h.name + ' (' + h.method + ')')); |
| 355 | sel.appendChild(o); |
| 356 | }); |
| 357 | if (keep) sel.value = keep; |
| 358 | } |
| 359 | |
| 360 | function renderLog() { |
| 361 | var tb = el('logRows'); |
| 362 | clear(tb); |
| 363 | if (!state.log.length) { |
| 364 | var tr = document.createElement('tr'); |
| 365 | var td = document.createElement('td'); |
| 366 | td.colSpan = 6; td.className = 'empty'; |
| 367 | td.appendChild(txt('No deliveries yet.')); |
| 368 | tr.appendChild(td); tb.appendChild(tr); |
| 369 | return; |
| 370 | } |
| 371 | /* The store keeps 50 deliveries; a page that lists all of them is a wall. |
| 372 | * The newest eight are what a person is looking for. */ |
| 373 | state.log.slice(0, 8).forEach(function (e) { |
| 374 | var tr = document.createElement('tr'); |
| 375 | var c1 = document.createElement('td'); |
| 376 | c1.className = 'mono'; c1.dataset.label = 'When'; c1.appendChild(txt(e.at)); |
| 377 | tr.appendChild(c1); |
| 378 | var c2 = document.createElement('td'); |
| 379 | c2.className = 'mono'; c2.dataset.label = 'Hook'; c2.appendChild(txt(e.name)); |
| 380 | tr.appendChild(c2); |
| 381 | var c3 = document.createElement('td'); |
| 382 | c3.dataset.label = 'Status'; |
| 383 | var p = document.createElement('span'); |
| 384 | p.className = 'pill ' + (e.ok ? 'ok' : 'bad'); |
| 385 | p.appendChild(txt(e.status ? String(e.status) : 'error')); |
| 386 | c3.appendChild(p); |
| 387 | tr.appendChild(c3); |
| 388 | var c4 = document.createElement('td'); |
| 389 | c4.className = 'mono'; c4.dataset.label = 'Bytes'; c4.appendChild(txt(e.bytes)); |
| 390 | tr.appendChild(c4); |
| 391 | var c5 = document.createElement('td'); |
| 392 | c5.className = 'mono'; c5.dataset.label = 'ms'; c5.appendChild(txt(e.ms)); |
| 393 | tr.appendChild(c5); |
| 394 | /* An empty Detail row is five lines of nothing on a phone, so it is only |
| 395 | * emitted when there is something to say. */ |
| 396 | if (e.error) { |
| 397 | var c6 = document.createElement('td'); |
| 398 | c6.className = 'hint'; c6.dataset.label = 'Detail'; |
| 399 | c6.appendChild(txt(e.error)); |
| 400 | tr.appendChild(c6); |
| 401 | } |
| 402 | tb.appendChild(tr); |
| 403 | }); |
| 404 | } |
| 405 | |
| 406 | function load() { |
| 407 | return api('list').then(function (d) { |
| 408 | state.hooks = d.hooks || []; |
| 409 | state.log = d.log || []; |
| 410 | state.allowPrivate = !!d.allow_private; |
| 411 | renderPolicy(); |
| 412 | renderHooks(); |
| 413 | renderLog(); |
| 414 | }).catch(function (e) { |
| 415 | var tb = el('rows'); |
| 416 | clear(tb); |
| 417 | var tr = document.createElement('tr'); |
| 418 | var td = document.createElement('td'); |
| 419 | td.colSpan = 6; td.className = 'empty'; |
| 420 | td.appendChild(txt('Could not load hooks: ' + e.message)); |
| 421 | tr.appendChild(td); tb.appendChild(tr); |
| 422 | }); |
| 423 | } |
| 424 | |
| 425 | /* ---------------------------------------------------------------- editor */ |
| 426 | |
| 427 | function addHeaderRow(name, value, hasSecret) { |
| 428 | var row = document.createElement('div'); |
| 429 | row.className = 'hdr-row'; |
| 430 | |
| 431 | var n = document.createElement('div'); |
| 432 | var ln = document.createElement('label'); ln.appendChild(txt('Header')); |
| 433 | var inName = document.createElement('input'); |
| 434 | inName.className = 'hdr-name'; inName.value = name || ''; |
| 435 | inName.placeholder = 'Authorization'; inName.autocomplete = 'off'; |
| 436 | n.appendChild(ln); n.appendChild(inName); |
| 437 | |
| 438 | var v = document.createElement('div'); |
| 439 | var lv = document.createElement('label'); lv.appendChild(txt('Value')); |
| 440 | var inVal = document.createElement('input'); |
| 441 | inVal.className = 'hdr-value'; inVal.value = value || ''; |
| 442 | inVal.type = 'password'; inVal.autocomplete = 'new-password'; |
| 443 | inVal.placeholder = hasSecret ? 'stored — leave blank to keep' : 'Bearer …'; |
| 444 | v.appendChild(lv); v.appendChild(inVal); |
| 445 | |
| 446 | var b = document.createElement('button'); |
| 447 | b.type = 'button'; b.className = 'icon-btn'; b.appendChild(txt('Remove')); |
| 448 | b.onclick = function () { row.parentNode.removeChild(row); }; |
| 449 | |
| 450 | row.appendChild(n); row.appendChild(v); row.appendChild(b); |
| 451 | el('hdrList').appendChild(row); |
| 452 | } |
| 453 | |
| 454 | function openEditor(hook) { |
| 455 | state.editing = hook ? hook.name : null; |
| 456 | el('editor').classList.remove('hidden'); |
| 457 | el('editorTitle').textContent = hook ? ('Edit ' + hook.name) : 'New hook'; |
| 458 | el('fName').value = hook ? hook.name : ''; |
| 459 | el('fName').disabled = !!hook; |
| 460 | el('fMethod').value = hook ? hook.method : 'POST'; |
| 461 | el('fUrl').value = hook ? (hook.url || '') : ''; |
| 462 | el('fDesc').value = hook ? (hook.desc || '') : ''; |
| 463 | el('fSig').value = hook ? (hook.sig || '') : ''; |
| 464 | el('fSecret').value = ''; |
| 465 | el('fSecret').placeholder = hook && hook.has_secret |
| 466 | ? 'stored — leave blank to keep' : 'optional'; |
| 467 | clear(el('hdrList')); |
| 468 | if (hook) { |
| 469 | (hook.headers || []).forEach(function (hd) { |
| 470 | addHeaderRow(hd.name, '', !!hd.has_secret); |
| 471 | }); |
| 472 | } else { |
| 473 | addHeaderRow('Content-Type', 'application/json', false); |
| 474 | } |
| 475 | el('saveMsg').textContent = ''; |
| 476 | el('fName').focus(); |
| 477 | } |
| 478 | |
| 479 | function collectHook() { |
| 480 | var headers = []; |
| 481 | var rows = el('hdrList').children; |
| 482 | for (var i = 0; i < rows.length; i++) { |
| 483 | var n = rows[i].querySelector('.hdr-name'); |
| 484 | var v = rows[i].querySelector('.hdr-value'); |
| 485 | if (!n || !n.value.trim()) continue; |
| 486 | var entry = { name: n.value.trim() }; |
| 487 | /* An empty value means "keep whatever is stored for this name". Sending |
| 488 | * the key at all with an empty string would clear the secret. */ |
| 489 | if (v && v.value !== '') entry.value = v.value; |
| 490 | headers.push(entry); |
| 491 | } |
| 492 | var body = { |
| 493 | name: el('fName').value.trim(), |
| 494 | url: el('fUrl').value.trim(), |
| 495 | method: el('fMethod').value, |
| 496 | desc: el('fDesc').value.trim(), |
| 497 | sig: el('fSig').value, |
| 498 | headers: headers |
| 499 | }; |
| 500 | if (el('fSecret').value) body.secret = el('fSecret').value; |
| 501 | return body; |
| 502 | } |
| 503 | |
| 504 | function saveHook() { |
| 505 | var body = collectHook(); |
| 506 | if (!body.name) { el('saveMsg').textContent = 'A name is required.'; return; } |
| 507 | if (!body.url) { el('saveMsg').textContent = 'A URL is required.'; return; } |
| 508 | el('saveMsg').textContent = 'Saving…'; |
| 509 | api('save', body).then(function () { |
| 510 | el('editor').classList.add('hidden'); |
| 511 | return load(); |
| 512 | }).catch(function (e) { |
| 513 | el('saveMsg').textContent = 'Error: ' + e.message; |
| 514 | }); |
| 515 | } |
| 516 | |
| 517 | function removeHook(name) { |
| 518 | api('delete', { name: name }).then(load).catch(function (e) { |
| 519 | el('fireOut').className = 'out on err'; |
| 520 | el('fireOut').textContent = 'Delete failed: ' + e.message; |
| 521 | }); |
| 522 | } |
| 523 | |
| 524 | /* ------------------------------------------------------------------- fire */ |
| 525 | |
| 526 | function fire() { |
| 527 | var name = el('fFire').value; |
| 528 | var out = el('fireOut'); |
| 529 | if (!name) { |
| 530 | out.className = 'out on err'; |
| 531 | out.textContent = 'Create a hook first.'; |
| 532 | return; |
| 533 | } |
| 534 | var payload = el('fPayload').value; |
| 535 | try { JSON.parse(payload || '{}'); } |
| 536 | catch (e) { |
| 537 | out.className = 'out on err'; |
| 538 | out.textContent = 'The payload is not valid JSON: ' + e.message; |
| 539 | return; |
| 540 | } |
| 541 | var body = { name: name, payload: payload || '{}' }; |
| 542 | if (el('fQuery').value.trim()) body.query = el('fQuery').value.trim(); |
| 543 | |
| 544 | out.className = 'out on'; |
| 545 | out.textContent = 'Sending…'; |
| 546 | api('fire', body).then(function (d) { |
| 547 | out.className = 'out on' + (d.ok_status ? '' : ' err'); |
| 548 | out.textContent = JSON.stringify(d, null, 2); |
| 549 | return load(); |
| 550 | }).catch(function (e) { |
| 551 | out.className = 'out on err'; |
| 552 | out.textContent = 'Send failed: ' + e.message; |
| 553 | }); |
| 554 | } |
| 555 | |
| 556 | /* ------------------------------------------------------------------- wire */ |
| 557 | |
| 558 | el('btnNew').onclick = function () { openEditor(null); }; |
| 559 | el('btnReload').onclick = function () { load(); }; |
| 560 | el('btnCancel').onclick = function () { el('editor').classList.add('hidden'); }; |
| 561 | el('btnSave').onclick = saveHook; |
| 562 | el('btnAddHdr').onclick = function () { addHeaderRow('', '', false); }; |
| 563 | el('btnFire').onclick = fire; |
| 564 | el('fPayload').addEventListener('keydown', function (e) { |
| 565 | if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') fire(); |
| 566 | }); |
| 567 | el('themeToggle').onclick = function () { |
| 568 | if (window.sxApplyTheme) { |
| 569 | window.sxApplyTheme(window.sxTheme && window.sxTheme() === 'dark' ? 'light' : 'dark'); |
| 570 | } |
| 571 | }; |
| 572 | |
| 573 | load(); |
| 574 | </script> |
| 575 | </body></html> |