(function () { 'use strict'; document.documentElement.classList.add('js'); var signedIn = document.body.getAttribute('data-signed-in') === '1'; function on(sel, ev, fn) { document.addEventListener(ev, function (e) { var t = e.target && e.target.closest ? e.target.closest(sel) : null; if (t) fn(t, e); }); } function compact(n) { if (n < 1000) return String(n); if (n < 10000) return (n / 1000).toFixed(1).replace(/\.0$/, '') + 'k'; if (n < 1000000) return Math.round(n / 1000) + 'k'; return (n / 1000000).toFixed(1).replace(/\.0$/, '') + 'M'; } // Mobile navigation on('[data-nav-toggle]', 'click', function (btn) { var nav = document.getElementById('main-nav'); var open = nav.classList.toggle('is-open'); btn.setAttribute('aria-expanded', open ? 'true' : 'false'); }); // Close the account menu when clicking elsewhere or pressing Escape document.addEventListener('click', function (e) { document.querySelectorAll('details.menu[open]').forEach(function (d) { if (!d.contains(e.target)) d.removeAttribute('open'); }); }); document.addEventListener('keydown', function (e) { if (e.key === 'Escape') document.querySelectorAll('details.menu[open]').forEach(function (d) { d.removeAttribute('open'); }); }); // Likes on('[data-like]', 'click', function (btn) { if (!signedIn) { location.href = '/signin?next=' + encodeURIComponent(location.pathname + location.search); return; } if (btn.classList.contains('is-busy')) return; var liked = btn.classList.contains('is-liked'); btn.classList.add('is-busy'); fetch('/api/skills/' + encodeURIComponent(btn.getAttribute('data-like')) + '/like', { method: liked ? 'DELETE' : 'POST', headers: { accept: 'application/json' }, credentials: 'same-origin' }) .then(function (r) { return r.json().then(function (j) { return { ok: r.ok, body: j }; }); }) .then(function (res) { if (!res.ok) { btn.setAttribute('title', res.body.message || 'That did not work.'); return; } document.querySelectorAll('[data-like="' + btn.getAttribute('data-like') + '"]').forEach(function (b) { b.classList.toggle('is-liked', !!res.body.liked); b.setAttribute('aria-pressed', res.body.liked ? 'true' : 'false'); b.setAttribute('title', res.body.liked ? 'Remove your like' : 'Like this skill'); var count = b.querySelector('[data-like-count]'); if (count) count.textContent = compact(res.body.likes); }); }) .catch(function () { btn.setAttribute('title', 'Could not reach Skillfarm. Try again.'); }) .then(function () { btn.classList.remove('is-busy'); }); }); // Copy buttons function copyText(text, done) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text).then(function () { done(true); }, function () { done(false); }); } else { var ta = document.createElement('textarea'); ta.value = text; ta.style.position = 'fixed'; ta.style.opacity = '0'; document.body.appendChild(ta); ta.select(); var ok = false; try { ok = document.execCommand('copy'); } catch (e) { ok = false; } document.body.removeChild(ta); done(ok); } } on('[data-copy]', 'click', function (btn) { var label = btn.querySelector('span'); var before = label ? label.textContent : ''; copyText(btn.getAttribute('data-copy'), function (ok) { if (!label) return; label.textContent = ok ? 'Copied' : 'Press Ctrl+C'; setTimeout(function () { label.textContent = before; }, 1600); }); }); // Share a skill: the device's share sheet where there is one, otherwise copy the link on('[data-share]', 'click', function (btn) { var url = btn.getAttribute('data-share-url'); var label = btn.querySelector('span'); var before = label ? label.textContent : ''; function copied(ok) { if (!label) return; label.textContent = ok ? 'Link copied' : 'Press Ctrl+C'; setTimeout(function () { label.textContent = before; }, 1800); } if (navigator.share) { navigator.share({ title: btn.getAttribute('data-share-title'), text: btn.getAttribute('data-share-text'), url: url }) .catch(function (e) { if (!e || e.name !== 'AbortError') copyText(url, copied); }); } else { copyText(url, copied); } }); // A shared repo is read one skill per request. Keep asking for the next one until none are left, then show the result. document.querySelectorAll('[data-repo-share]').forEach(function (box) { var url = box.getAttribute('data-repo-share'); var status = box.querySelector('[data-repo-share-status]'); var fails = 0; function say(t) { if (status) status.textContent = t; } function step() { fetch(url, { method: 'POST', headers: { accept: 'application/json' }, credentials: 'same-origin' }) .then(function (r) { return r.json().then(function (j) { return { ok: r.ok, body: j }; }); }) .then(function (res) { if (!res.ok) throw new Error('step'); fails = 0; if (res.body.left > 0) { say('Skillfarm found ' + res.body.found + (res.body.found === 1 ? ' skill' : ' skills') + ' and is reading them. ' + res.body.left + ' to go.'); step(); } else { location.reload(); } }) .catch(function () { fails += 1; if (fails < 4) setTimeout(step, 1500 * fails); else say('Reading paused. The rest are read within a few minutes, and you can close this page. Reload it to check.'); }); } step(); }); // Send to agent. The visitor's agent is remembered in this browser only. var AGENT_KEY = 'sf-agent'; function savedAgent() { try { return localStorage.getItem(AGENT_KEY) || ''; } catch (e) { return ''; } } function saveAgent(a) { try { localStorage.setItem(AGENT_KEY, a); } catch (e) { /* private window */ } } function pickedOption(box) { var pick = box.querySelector('[data-send-pick]'); return pick.options[pick.selectedIndex]; } function showSend(box, agent) { var pick = box.querySelector('[data-send-pick]'); for (var i = 0; i < pick.options.length; i++) if (pick.options[i].value === agent) pick.selectedIndex = i; var opt = pickedOption(box); var href = opt.getAttribute('data-href'); box.querySelector('[data-send-go]').setAttribute('href', href || '#'); box.querySelector('[data-send-label]').textContent = (href ? 'Send to ' : 'Copy for ') + opt.textContent; box.querySelector('[data-send-note]').textContent = opt.getAttribute('data-note'); } function showAgent(agent) { if (!agent) return; document.querySelectorAll('[data-send]').forEach(function (box) { showSend(box, agent); }); document.querySelectorAll('[data-agent-tabs] [data-tab="' + agent + '"]').forEach(function (tab) { openTab(tab); }); } document.addEventListener('change', function (e) { var pick = e.target; if (!pick.matches || !pick.matches('[data-send-pick]')) return; saveAgent(pick.value); showAgent(pick.value); }); on('[data-send-go]', 'click', function (go, e) { var box = go.closest('[data-send]'); var opt = pickedOption(box); var note = box.querySelector('[data-send-note]'); saveAgent(opt.value); if (!opt.getAttribute('data-href')) e.preventDefault(); copyText(opt.getAttribute('data-prompt'), function (ok) { note.textContent = ok ? opt.getAttribute('data-after') : 'Could not copy the prompt here. Use the Copy button next to it instead.'; }); }); // Tab sets function openTab(tab) { var set = tab.closest('[data-tabset]'); var key = tab.getAttribute('data-tab'); set.querySelectorAll('[data-tab]').forEach(function (t) { var active = t === tab; t.classList.toggle('is-active', active); t.setAttribute('aria-selected', active ? 'true' : 'false'); }); set.querySelectorAll('[data-panel]').forEach(function (p) { p.hidden = p.getAttribute('data-panel') !== key; }); } on('[data-tabset] [data-tab]', 'click', function (tab) { openTab(tab); // Choosing an agent's tab is choosing your agent. if (tab.closest('[data-agent-tabs]')) { saveAgent(tab.getAttribute('data-tab')); showAgent(tab.getAttribute('data-tab')); } }); showAgent(savedAgent()); // Share form: switch between a GitHub link and pasted text document.querySelectorAll('[data-share-form]').forEach(function (form) { function sync() { var picked = form.querySelector('input[name="mode"]:checked'); var mode = picked ? picked.value : 'paste'; form.querySelectorAll('[data-mode]').forEach(function (el) { var show = el.getAttribute('data-mode') === mode; el.hidden = !show; el.querySelectorAll('input, textarea').forEach(function (i) { i.disabled = !show; }); }); } form.querySelectorAll('input[name="mode"]').forEach(function (r) { r.addEventListener('change', sync); }); sync(); form.addEventListener('submit', function () { var b = form.querySelector('button[type="submit"]'); if (b) { b.classList.add('is-disabled'); b.textContent = b.getAttribute('data-busy-label') || 'Submitting…'; } }); }); // Load a local SKILL.md into the paste box document.addEventListener('change', function (e) { var input = e.target; if (!input.matches || !input.matches('[data-skill-file]')) return; var file = input.files && input.files[0]; if (!file) return; if (file.size > 200000) { alertInline(input, 'That file is larger than 200 KB.'); return; } var reader = new FileReader(); reader.onload = function () { var form = input.closest('form'); var area = form && form.querySelector('textarea[name="skill_md"]'); if (area) area.value = String(reader.result); }; reader.readAsText(file); }); function alertInline(el, text) { var note = el.parentNode.querySelector('.file-note'); if (!note) { note = document.createElement('small'); note.className = 'file-note'; el.parentNode.appendChild(note); } note.textContent = ' ' + text; } // Avatar: crop to a square and resize in the browser, then upload both sizes document.querySelectorAll('[data-avatar]').forEach(function (box) { var input = box.querySelector('[data-avatar-input]'); var status = box.querySelector('[data-avatar-status]'); if (!input) return; function say(t) { if (status) status.textContent = t; } function draw(img, size) { var canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; var w = img.naturalWidth || img.width, h = img.naturalHeight || img.height; var side = Math.min(w, h); var ctx = canvas.getContext('2d'); ctx.imageSmoothingQuality = 'high'; ctx.drawImage(img, (w - side) / 2, (h - side) / 2, side, side, 0, 0, size, size); return canvas; } function toBlob(canvas, type) { return new Promise(function (resolve) { canvas.toBlob(function (b) { resolve(b); }, type, 0.86); }); } function encode(canvas) { return toBlob(canvas, 'image/webp').then(function (b) { if (b && b.type === 'image/webp') return b; return toBlob(canvas, 'image/jpeg'); }); } input.addEventListener('change', function () { var file = input.files && input.files[0]; if (!file) return; if (!/^image\/(png|jpeg|webp)$/.test(file.type)) { say('Choose a PNG, JPEG or WebP image.'); return; } if (file.size > 12 * 1024 * 1024) { say('That image is larger than 12 MB.'); return; } say('Preparing…'); var url = URL.createObjectURL(file); var img = new Image(); img.onload = function () { Promise.all([encode(draw(img, 512)), encode(draw(img, 128))]) .then(function (blobs) { URL.revokeObjectURL(url); if (!blobs[0] || !blobs[1]) throw new Error('encode'); var ext = blobs[0].type === 'image/webp' ? 'webp' : 'jpg'; var fd = new FormData(); fd.append('avatar', blobs[0], 'avatar.' + ext); fd.append('avatar_small', blobs[1], 'avatar-128.' + ext); say('Uploading…'); return fetch('/api/me/avatar', { method: 'POST', body: fd, headers: { accept: 'application/json' }, credentials: 'same-origin' }); }) .then(function (r) { return r.json().then(function (j) { return { ok: r.ok, body: j }; }); }) .then(function (res) { if (!res.ok) { say(res.body.message || 'The upload did not work.'); return; } say('Saved.'); var preview = box.querySelector('[data-avatar-preview]'); if (preview && res.body.url) { var fresh = document.createElement('img'); fresh.className = 'avatar'; fresh.width = 128; fresh.height = 128; fresh.alt = ''; fresh.src = res.body.url; preview.innerHTML = ''; preview.appendChild(fresh); } else { location.reload(); } }) .catch(function () { say('The upload did not work. Try a different image.'); }); }; img.onerror = function () { URL.revokeObjectURL(url); say('That file could not be read as an image.'); }; img.src = url; }); }); // Confirm before destructive forms document.addEventListener('submit', function (e) { var form = e.target; if (form.matches && form.matches('form[data-confirm]') && !window.confirm(form.getAttribute('data-confirm'))) e.preventDefault(); }); })();