Branchwell / source / static / app.js
| 1 | // Branchwell client: passkey sign-in, push keys, votes, careers form. No trackers. |
| 2 | (function () { |
| 3 | 'use strict'; |
| 4 | var b2u = function (buf) { var s = ''; new Uint8Array(buf).forEach(function (b) { s += String.fromCharCode(b); }); return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); }; |
| 5 | var u2b = function (s) { s = s.replace(/-/g, '+').replace(/_/g, '/'); while (s.length % 4) s += '='; var bin = atob(s), out = new Uint8Array(bin.length); for (var i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i); return out.buffer; }; |
| 6 | function post(url, body) { |
| 7 | return fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body || {}), credentials: 'same-origin' }) |
| 8 | .then(function (r) { return r.json().then(function (j) { if (!r.ok || j.error) throw new Error(j.error || j.message || 'Something went wrong.'); return j; }); }); |
| 9 | } |
| 10 | function say(el, text, bad) { if (!el) return; el.textContent = text; el.className = bad ? 'err' : 'muted'; } |
| 11 | var back = function () { var p = new URLSearchParams(location.search).get('back'); return p && p.charAt(0) === '/' && p.charAt(1) !== '/' ? p : '/account'; }; |
| 12 | |
| 13 | function register(handle, msg) { |
| 14 | if (!window.PublicKeyCredential) { say(msg, 'This browser does not support passkeys. Try a recent Safari, Chrome, Edge or Firefox.', true); return Promise.resolve(); } |
| 15 | say(msg, 'Follow your device to make a passkey…'); |
| 16 | return post('/api/auth/register/options', { handle: handle }).then(function (r) { |
| 17 | var o = r.options; |
| 18 | var pk = { challenge: u2b(o.challenge), rp: o.rp, user: { id: u2b(o.user.id), name: o.user.name, displayName: o.user.displayName }, |
| 19 | pubKeyCredParams: o.pubKeyCredParams, timeout: o.timeout, attestation: o.attestation, authenticatorSelection: o.authenticatorSelection, |
| 20 | excludeCredentials: (o.excludeCredentials || []).map(function (c) { return { id: u2b(c.id), type: c.type, transports: c.transports }; }) }; |
| 21 | return navigator.credentials.create({ publicKey: pk }).then(function (c) { |
| 22 | return post('/api/auth/register/verify', { cid: r.cid, response: { id: c.id, rawId: b2u(c.rawId), type: c.type, |
| 23 | response: { clientDataJSON: b2u(c.response.clientDataJSON), attestationObject: b2u(c.response.attestationObject), transports: c.response.getTransports ? c.response.getTransports() : [] }, |
| 24 | clientExtensionResults: c.getClientExtensionResults ? c.getClientExtensionResults() : {}, authenticatorAttachment: c.authenticatorAttachment } }); |
| 25 | }); |
| 26 | }); |
| 27 | } |
| 28 | function login(msg) { |
| 29 | if (!window.PublicKeyCredential) { say(msg, 'This browser does not support passkeys.', true); return Promise.resolve(); } |
| 30 | say(msg, 'Follow your device to use your passkey…'); |
| 31 | return post('/api/auth/login/options').then(function (r) { |
| 32 | var o = r.options; |
| 33 | return navigator.credentials.get({ publicKey: { challenge: u2b(o.challenge), rpId: o.rpId, timeout: o.timeout, userVerification: o.userVerification, allowCredentials: [] } }).then(function (c) { |
| 34 | return post('/api/auth/login/verify', { cid: r.cid, response: { id: c.id, rawId: b2u(c.rawId), type: c.type, |
| 35 | response: { clientDataJSON: b2u(c.response.clientDataJSON), authenticatorData: b2u(c.response.authenticatorData), signature: b2u(c.response.signature), userHandle: c.response.userHandle ? b2u(c.response.userHandle) : undefined }, |
| 36 | clientExtensionResults: {}, authenticatorAttachment: c.authenticatorAttachment } }); |
| 37 | }); |
| 38 | }); |
| 39 | } |
| 40 | var fail = function (msg) { return function (e) { say(msg, e && e.name === 'NotAllowedError' ? 'Cancelled. Try again when you are ready.' : (e.message || 'Something went wrong.'), true); }; }; |
| 41 | |
| 42 | var signup = document.getElementById('signup'); |
| 43 | if (signup) signup.addEventListener('submit', function (e) { |
| 44 | e.preventDefault(); var msg = document.getElementById('signup-msg'); |
| 45 | register(signup.handle.value.trim().toLowerCase(), msg).then(function (r) { if (r && r.ok) location.href = back(); }).catch(fail(msg)); |
| 46 | }); |
| 47 | var signin = document.getElementById('signin'); |
| 48 | if (signin) signin.addEventListener('click', function () { |
| 49 | var msg = document.getElementById('signin-msg'); |
| 50 | login(msg).then(function (r) { if (r && r.ok) location.href = back(); }).catch(fail(msg)); |
| 51 | }); |
| 52 | var addpk = document.getElementById('add-passkey'); |
| 53 | if (addpk) addpk.addEventListener('click', function () { |
| 54 | var msg = document.getElementById('pk-msg'); |
| 55 | register('', msg).then(function (r) { if (r && r.ok) location.reload(); }).catch(fail(msg)); |
| 56 | }); |
| 57 | var out = document.getElementById('signout'); |
| 58 | if (out) out.addEventListener('click', function () { post('/api/auth/logout').then(function () { location.href = '/'; }); }); |
| 59 | var mk = document.getElementById('make-key'); |
| 60 | if (mk) mk.addEventListener('submit', function (e) { |
| 61 | e.preventDefault(); var msg = document.getElementById('key-msg'); |
| 62 | post('/api/auth/push-keys/create', { name: mk.keyname.value }).then(function (r) { |
| 63 | var box = document.getElementById('new-key'); box.hidden = false; |
| 64 | document.getElementById('new-key-value').textContent = r.key; |
| 65 | document.getElementById('new-key-user').textContent = r.handle; |
| 66 | say(msg, 'Copy it now. We only show it once.'); |
| 67 | }).catch(fail(msg)); |
| 68 | }); |
| 69 | document.querySelectorAll('[data-del-key]').forEach(function (b) { |
| 70 | b.addEventListener('click', function () { post('/api/auth/push-keys/delete', { id: b.getAttribute('data-del-key') }).then(function () { location.reload(); }); }); |
| 71 | }); |
| 72 | document.querySelectorAll('[data-copy]').forEach(function (b) { |
| 73 | b.addEventListener('click', function () { |
| 74 | var t = document.getElementById(b.getAttribute('data-copy')).textContent; |
| 75 | (navigator.clipboard ? navigator.clipboard.writeText(t) : Promise.reject()).then(function () { b.textContent = 'Copied'; setTimeout(function () { b.textContent = 'Copy'; }, 1500); }).catch(function () {}); |
| 76 | }); |
| 77 | }); |
| 78 | // Votes on commits and branches |
| 79 | document.querySelectorAll('.votes button[data-vote]').forEach(function (b) { |
| 80 | b.addEventListener('click', function () { |
| 81 | var box = b.closest('.votes'); var msg = box.parentNode.querySelector('.vote-msg') || document.getElementById('vote-msg'); |
| 82 | var pressed = b.getAttribute('aria-pressed') === 'true'; |
| 83 | post('/api/vote', { repo: box.dataset.repo, kind: box.dataset.kind, target: box.dataset.target, value: pressed ? 0 : +b.dataset.vote }) |
| 84 | .then(function (r) { |
| 85 | if (r.ok === false) { say(msg, r.message, true); return; } |
| 86 | box.querySelector('[data-vote="1"] span').textContent = r.up; box.querySelector('[data-vote="-1"] span').textContent = r.down; |
| 87 | box.querySelectorAll('button').forEach(function (x) { x.setAttribute('aria-pressed', 'false'); }); |
| 88 | if (!pressed) b.setAttribute('aria-pressed', 'true'); |
| 89 | say(msg, r.message); |
| 90 | if (r.status === 'removed' || r.status === 'kept') setTimeout(function () { location.reload(); }, 1200); |
| 91 | }).catch(function (e) { if (/Sign in/i.test(e.message)) location.href = '/signin?back=' + encodeURIComponent(location.pathname); else say(msg, e.message, true); }); |
| 92 | }); |
| 93 | }); |
| 94 | // Feature vote page (opened from an app's New badge) |
| 95 | document.querySelectorAll('[data-fvote]').forEach(function (b) { |
| 96 | b.addEventListener('click', function () { |
| 97 | var msg = document.getElementById('fv-msg'); |
| 98 | post('/api/feature-vote', { app: b.dataset.app, feature: b.dataset.feature, value: +b.dataset.fvote }).then(function (r) { |
| 99 | say(msg, r.ok ? (r.status === 'kept' ? 'Thanks. 3 thumbs up: it stays for good.' : r.status === 'removed' ? 'Thanks. 3 thumbs down: it is switched off.' : 'Thanks. Your vote counts.') : r.message); |
| 100 | try { if (window.opener) window.opener.postMessage({ bwVoted: true }, '*'); } catch (_) {} |
| 101 | }).catch(function (e) { say(msg, e.message, true); }); |
| 102 | }); |
| 103 | }); |
| 104 | // New repo |
| 105 | var nr = document.getElementById('new-repo'); |
| 106 | if (nr) nr.addEventListener('submit', function (e) { |
| 107 | e.preventDefault(); var msg = document.getElementById('repo-msg'); |
| 108 | post('/api/repos', { owner: nr.owner.value, name: nr.repo.value.trim(), description: nr.description.value }).then(function (r) { location.href = '/' + r.name; }).catch(fail(msg)); |
| 109 | }); |
| 110 | // Careers |
| 111 | var f = document.getElementById('cr-form'); |
| 112 | if (f) f.addEventListener('submit', function (e) { |
| 113 | e.preventDefault(); var m = document.getElementById('cr-msg'); var nm = f.name.value.trim(), em = f.email.value.trim(), msg = f.request.value.trim(); |
| 114 | if (!nm || !em || !msg) { m.textContent = 'Please fill in your name, email and message.'; return; } |
| 115 | if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(em)) { m.textContent = 'Please check your email address.'; return; } |
| 116 | var t = 'Name: ' + nm + '\nEmail: ' + em + '\n\n' + msg; |
| 117 | var b = f.querySelector('button'); b.disabled = true; m.textContent = 'Sending…'; |
| 118 | fetch('/api/request', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ request: t, site: location.hostname, website: f.website.value }) }) |
| 119 | .then(function (r) { if (!r.ok) throw 0; f.reset(); m.textContent = 'Thanks. We got your request.'; }) |
| 120 | .catch(function () { m.textContent = 'That didn’t go through. Please try again in a minute.'; }) |
| 121 | .then(function () { b.disabled = false; }); |
| 122 | }); |
| 123 | })(); |