/* BitAddy personal page / quick link: copy, QR codes, report. Tracking: only the site's anonymous visit counter and anonymous funnel steps (ev.js). */ (function () { "use strict"; /* funnel steps (ev.js): anonymous, per browser tab. The page's own name is never sent: these pages all count as one place. */ var KIND = (document.querySelector(".rep") || { dataset: {} }).dataset.kind || (document.querySelector(".reqstat") ? "request" : "other"); window.BA_EV_PATH = KIND === "page" ? "/(page)" : KIND === "link" ? "/(link)" : KIND === "request" ? "/(request)" : "/(other)"; window.baEv = window.baEv || function () { (window.baEv.q = window.baEv.q || []).push(arguments); }; (function () { var s = document.createElement("script"); s.src = "/_b/static/ev.js?v=1"; s.async = true; document.head.appendChild(s); })(); function ev(step, detail, onceKey) { try { window.baEv(step, detail, onceKey); } catch (e) {} } ev("upage", KIND); /* a Share button under the page's name (the phone's share menu when there is one, else Copy link) */ (function () { var head = document.querySelector(".ph"); if (!head || KIND === "other") return; var u = new URL(location.href); u.searchParams.delete("q"); var url = u.origin + u.pathname + u.search, row = document.createElement("div"), b = document.createElement("button"); row.className = "acts shr"; b.type = "button"; var canShare = window.baShare ? baShare.can : typeof navigator.share === "function"; b.textContent = canShare ? "Share this " + ({ page: "page", link: "link", request: "request" }[KIND]) : "Copy link"; b.addEventListener("click", function () { ev("u_share", canShare ? "share" : "copy"); if (canShare) navigator.share({ url: url, title: document.title }).catch(function () {}); else (navigator.clipboard ? navigator.clipboard.writeText(url) : Promise.reject()).then(function () { toast("Link copied"); }, function () { toast("Copy the address from the address bar"); }); }); row.appendChild(b); head.parentNode.insertBefore(row, head.nextSibling); })(); /* a payment request that is still open: look every 20 seconds and show "Paid" as soon as the payment is seen */ (function () { var st = document.querySelector('.reqstat[data-status="pending"]'); if (!st) return; var code = st.getAttribute("data-code"), n = 0; (function look() { if (++n > 180) return; /* an hour, then the visitor can reload */ setTimeout(function () { fetch("/_b/payreq/status?c=" + encodeURIComponent(code), { headers: { "X-BitAddy": "1" } }).then(function (r) { return r.json(); }).then(function (j) { if (j.status && j.status !== "pending") { if (j.status === "paid") ev("u_req_paid"); location.reload(); } else look(); }).catch(look); }, 20000); })(); })(); document.querySelectorAll('.foot a[href="/"], a.brand').forEach(function (a) { a.addEventListener("click", function () { ev("u_make", a.classList.contains("brand") ? "logo" : "make your own"); }); }); function toast(t) { var d = document.querySelector(".toast"); if (!d) { d = document.createElement("div"); d.className = "toast"; d.setAttribute("role", "status"); document.body.appendChild(d); } d.textContent = t; d.classList.add("on"); setTimeout(function () { d.classList.remove("on"); }, 2200); } document.querySelectorAll(".copy").forEach(function (b) { b.addEventListener("click", function () { var t = b.getAttribute("data-copy"); ev("u_copy", KIND, "u_copy"); (navigator.clipboard ? navigator.clipboard.writeText(t) : Promise.reject()).then(function () { toast("Address copied. Check the ending in your wallet."); }, function () { var r = document.createRange(); r.selectNodeContents(b.closest(".coin").querySelector(".addr")); var s = getSelection(); s.removeAllRanges(); s.addRange(r); toast("Selected: copy it now"); }); }); }); /* what a wallet app reads from the QR code: the address, and the amount too where that coin has a standard way to say it */ function units(amount, dec) { var p = String(amount).split("."), f = (p[1] || "").padEnd(dec, "0").slice(0, dec); return (p[0] + f).replace(/^0+(?=\d)/, ""); } function qrText(sec) { var addr = sec.getAttribute("data-addr"), uri = sec.getAttribute("data-uri"), amt = sec.getAttribute("data-amt"); if (!amt || !uri) return addr; return uri === "ethereum" ? "ethereum:" + addr + "?value=" + units(amt, 18) : uri + ":" + addr + "?amount=" + amt; } function drawQr(box, sec) { var addr = sec.getAttribute("data-addr"), amt = sec.getAttribute("data-amt"); var q = qrcode(0, "M"); q.addData(qrText(sec)); q.make(); box.innerHTML = q.createSvgTag({ cellSize: 6, margin: 2, scalable: true }); var cap = document.createElement("div"); var capLine = "ends in " + addr.slice(-6) + (amt ? " · asks for " + amt + " " + sec.getAttribute("data-coin") : ""); cap.textContent = "Scan with your wallet app · ends in " + addr.slice(-6) + (amt ? (sec.getAttribute("data-uri") ? " · asks for " + amt + " " + sec.getAttribute("data-coin") : " · type the amount yourself: " + amt + " " + sec.getAttribute("data-coin")) : ""); box.appendChild(cap); if (window.baShare) { /* the same QR code as a picture to print, post or send */ var sv = document.createElement("button"); sv.type = "button"; sv.className = "qrsave"; sv.textContent = "Save QR picture"; sv.addEventListener("click", function () { ev("u_qr_save", KIND); var name = (document.querySelector(".nm") || {}).textContent || "bitaddy.com"; var coinName = (sec.querySelector(".ch b") || {}).textContent || sec.getAttribute("data-coin"); baShare.saveQr(qrText(sec), coinName + " · " + name, capLine, name.replace(/^bitaddy\.com\//, "bitaddy-") + "-" + sec.getAttribute("data-coin")) .then(function () { toast("QR picture saved"); }, function () { toast("The picture couldn't be made"); }); }); box.appendChild(sv); } } document.querySelectorAll(".qrb").forEach(function (b) { b.addEventListener("click", function () { var sec = b.closest(".coin"), box = sec.querySelector(".qr"); if (!box.hidden) { box.hidden = true; b.textContent = "Show QR code"; return; } ev("u_qr", KIND, "u_qr"); if (!box.firstChild && window.qrcode) drawQr(box, sec); box.hidden = false; b.textContent = "Hide QR code"; }); }); /* "Ask for an amount": makes a link to this same page that shows the amount (free, nothing is stored) */ document.querySelectorAll(".amt").forEach(function (b) { b.addEventListener("click", function () { var sec = b.closest(".coin"), box = sec.querySelector(".amtbox"); if (box) { box.hidden = !box.hidden; return; } var coin = sec.getAttribute("data-coin"); box = document.createElement("div"); box.className = "amtbox"; var lab = document.createElement("label"); lab.textContent = "How much " + coin + " do you want to ask for?"; var inp = document.createElement("input"); inp.type = "text"; inp.inputMode = "decimal"; inp.placeholder = "e.g. 12.5"; inp.setAttribute("aria-label", "Amount in " + coin); var go = document.createElement("button"); go.type = "button"; go.className = "go"; go.textContent = "Make the link"; var row = document.createElement("div"); row.className = "row"; row.appendChild(inp); row.appendChild(go); var out = document.createElement("div"); out.className = "out"; out.hidden = true; var cp = document.createElement("button"); cp.type = "button"; cp.textContent = "Copy link"; cp.hidden = true; cp.style.marginTop = ".5rem"; box.appendChild(lab); box.appendChild(row); box.appendChild(out); box.appendChild(cp); sec.appendChild(box); go.onclick = function () { var v = inp.value.trim().replace(",", "."); if (!/^(0|[1-9]\d{0,11})(\.\d{1,8})?$/.test(v) || Number(v) <= 0) { toast("Type an amount like 12.5"); return; } var many = document.querySelectorAll(".coin").length > 1; var url = location.origin + location.pathname + "?a=" + v + (many ? "&c=" + coin : ""); out.textContent = url; out.hidden = false; cp.hidden = false; ev("u_amount", KIND, "u_amount"); cp.onclick = function () { (navigator.clipboard ? navigator.clipboard.writeText(url) : Promise.reject()).then(function () { toast("Link copied"); }, function () { toast("Select the link and copy it"); }); }; }; inp.focus(); }); }); /* a link that asks for an amount opens its QR code straight away */ document.querySelectorAll(".coin[data-amt]").forEach(function (sec) { var b = sec.querySelector(".qrb"); if (b && window.qrcode) b.click(); sec.scrollIntoView({ block: "nearest" }); }); var REASONS = [["scam", "Scam or fraud"], ["impersonation", "Pretends to be someone else"], ["wrong", "Wrong or stolen address"], ["abuse", "Offensive or harmful"], ["other", "Something else"]]; document.querySelectorAll(".rep").forEach(function (b) { b.addEventListener("click", function () { var d = document.createElement("div"); d.className = "dlg"; var box = document.createElement("div"); box.className = "dl"; box.setAttribute("role", "dialog"); box.setAttribute("aria-modal", "true"); var h = document.createElement("h2"); h.textContent = "Report this " + (b.dataset.kind === "page" ? "page" : "link"); box.appendChild(h); REASONS.forEach(function (r, i) { var l = document.createElement("label"); var x = document.createElement("input"); x.type = "radio"; x.name = "why"; x.value = r[0]; if (!i) x.checked = true; l.appendChild(x); l.appendChild(document.createTextNode(r[1])); box.appendChild(l); }); var ta = document.createElement("textarea"); ta.placeholder = "Anything we should know? (optional)"; ta.maxLength = 500; ta.setAttribute("aria-label", "Details"); box.appendChild(ta); var row = document.createElement("div"); row.className = "row"; var cancel = document.createElement("button"); cancel.type = "button"; cancel.textContent = "Cancel"; var go = document.createElement("button"); go.type = "button"; go.className = "go"; go.textContent = "Send report"; row.appendChild(cancel); row.appendChild(go); box.appendChild(row); d.appendChild(box); document.body.appendChild(d); function close() { d.remove(); } cancel.onclick = close; d.addEventListener("click", function (e) { if (e.target === d) close(); }); go.onclick = function () { go.disabled = true; fetch("/_b/report", { method: "POST", headers: { "Content-Type": "application/json", "X-BitAddy": "1" }, body: JSON.stringify({ kind: b.dataset.kind, target: b.dataset.target, reason: box.querySelector("input:checked").value, note: ta.value }) }) .then(function (r) { return r.json().then(function (j) { if (!r.ok) throw new Error(j.error || "Error"); }); }) .then(function () { ev("u_report", KIND); close(); toast("Thanks. We'll take a look."); }, function (e) { go.disabled = false; toast(e.message); }); }; go.focus(); }); }); })();