| 1 |
// Topic name generator for the ntfy docs |
| 2 |
// |
| 3 |
// A tiny helper that lives on the "Publishing" page. The user types a memorable |
| 4 |
// prefix (e.g. "backups"), and the widget appends a random, hard-to-guess suffix |
| 5 |
// (e.g. "backups-x7Kp2mQ9"). The result is a valid, unguessable topic name. |
| 6 |
// |
| 7 |
// Topic names on the server must match ^[-_A-Za-z0-9]{1,64}$ (see server.go), so as |
| 8 |
// the user types we strip anything that isn't allowed (spaces, slashes, punctuation, |
| 9 |
// emoji, ...) live and cap the whole thing at 64 characters. The random suffix is |
| 10 |
// generated once on load and can be re-rolled with the "Regenerate suffix" button. |
| 11 |
(function () { |
| 12 |
// Allowed topic characters per the server regex ^[-_A-Za-z0-9]{1,64}$ |
| 13 |
const ALLOWED = /[^-_A-Za-z0-9]/g; |
| 14 |
const MAX_LEN = 64; |
| 15 |
|
| 16 |
// Suffix alphabet: full base62 (letters + digits). We deliberately keep look-alikes |
| 17 |
// (0/O, l/1) for maximum entropy -- this is a generated suffix, not something typed by |
| 18 |
// hand. Hyphen/underscore are excluded so the "-" separator stays visually clear. |
| 19 |
const SUFFIX_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 20 |
const SUFFIX_LEN = 10; |
| 21 |
|
| 22 |
// randomSuffix returns a cryptographically random string from SUFFIX_ALPHABET. |
| 23 |
// It uses rejection sampling to avoid the modulo bias that a plain `byte % 62` would |
| 24 |
// introduce (256 is not a multiple of 62), keeping every character equally likely. |
| 25 |
function randomSuffix() { |
| 26 |
const n = SUFFIX_ALPHABET.length; |
| 27 |
const limit = Math.floor(256 / n) * n; // largest multiple of n that fits in a byte |
| 28 |
const buf = new Uint8Array(1); |
| 29 |
let out = ""; |
| 30 |
while (out.length < SUFFIX_LEN) { |
| 31 |
crypto.getRandomValues(buf); |
| 32 |
if (buf[0] < limit) { |
| 33 |
out += SUFFIX_ALPHABET[buf[0] % n]; |
| 34 |
} |
| 35 |
} |
| 36 |
return out; |
| 37 |
} |
| 38 |
|
| 39 |
// sanitize strips everything that isn't a valid topic character. |
| 40 |
function sanitize(value) { |
| 41 |
return value.replace(ALLOWED, ""); |
| 42 |
} |
| 43 |
|
| 44 |
function initTopicGenerator() { |
| 45 |
const root = document.getElementById("tg-widget"); |
| 46 |
if (!root) return; |
| 47 |
|
| 48 |
const input = root.querySelector("#tg-input"); |
| 49 |
const outputName = root.querySelector("#tg-output-name"); |
| 50 |
const outputUrl = root.querySelector("#tg-output-url"); |
| 51 |
const reroll = root.querySelector("#tg-reroll"); |
| 52 |
|
| 53 |
let suffix = randomSuffix(); |
| 54 |
|
| 55 |
// update recomputes the live preview from the (sanitized) input + current suffix. |
| 56 |
function update() { |
| 57 |
// Sanitize in place so the user sees disallowed characters disappear as they type. |
| 58 |
const cleaned = sanitize(input.value); |
| 59 |
if (cleaned !== input.value) { |
| 60 |
const pos = input.selectionStart - (input.value.length - cleaned.length); |
| 61 |
// Reassigning .value and setSelectionRange make the browser scroll the field into |
| 62 |
// view (there is no preventScroll option for setSelectionRange), which jumps the |
| 63 |
// whole page. Capture the scroll position and restore it afterwards. |
| 64 |
const scrollX = window.scrollX; |
| 65 |
const scrollY = window.scrollY; |
| 66 |
input.value = cleaned; |
| 67 |
// Best-effort caret restore so removing a bad char doesn't jump the cursor to the end. |
| 68 |
try { input.setSelectionRange(pos, pos); } catch { /* ignore */ } |
| 69 |
window.scrollTo(scrollX, scrollY); |
| 70 |
} |
| 71 |
|
| 72 |
// Compose "<prefix>-<suffix>", capped at the 64-char topic limit. With no prefix, |
| 73 |
// fall back to just the random suffix so the output is always a valid topic. |
| 74 |
let topic; |
| 75 |
if (cleaned === "") { |
| 76 |
topic = suffix; |
| 77 |
} else { |
| 78 |
const maxPrefix = MAX_LEN - suffix.length - 1; // room for "-" + suffix |
| 79 |
const prefix = cleaned.slice(0, Math.max(0, maxPrefix)); |
| 80 |
topic = prefix === "" ? suffix : prefix + "-" + suffix; |
| 81 |
} |
| 82 |
|
| 83 |
outputName.textContent = topic; |
| 84 |
outputUrl.textContent = "https://ntfy.sh/" + topic; |
| 85 |
} |
| 86 |
|
| 87 |
input.addEventListener("input", update); |
| 88 |
reroll.addEventListener("click", function () { |
| 89 |
suffix = randomSuffix(); |
| 90 |
update(); |
| 91 |
input.focus(); |
| 92 |
}); |
| 93 |
|
| 94 |
// Copy buttons: copy the target output and briefly swap the clipboard icon for a checkmark, |
| 95 |
// mirroring the config generator's copy button behavior. |
| 96 |
const copyIcon = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><rect x=\"9\" y=\"9\" width=\"13\" height=\"13\" rx=\"2\" ry=\"2\"></rect><path d=\"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1\"></path></svg>"; |
| 97 |
const checkIcon = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><polyline points=\"20 6 9 17 4 12\"></polyline></svg>"; |
| 98 |
root.querySelectorAll(".tg-btn-copy").forEach(function (btn) { |
| 99 |
btn.addEventListener("click", function () { |
| 100 |
const target = root.querySelector("#" + btn.dataset.copy); |
| 101 |
if (!target || !target.textContent) return; |
| 102 |
navigator.clipboard.writeText(target.textContent).then(function () { |
| 103 |
btn.innerHTML = checkIcon; |
| 104 |
btn.style.color = "var(--md-primary-fg-color)"; |
| 105 |
setTimeout(function () { |
| 106 |
btn.innerHTML = copyIcon; |
| 107 |
btn.style.color = ""; |
| 108 |
}, 2000); |
| 109 |
}); |
| 110 |
}); |
| 111 |
}); |
| 112 |
|
| 113 |
update(); |
| 114 |
} |
| 115 |
|
| 116 |
if (document.readyState === "loading") { |
| 117 |
document.addEventListener("DOMContentLoaded", initTopicGenerator); |
| 118 |
} else { |
| 119 |
initTopicGenerator(); |
| 120 |
} |
| 121 |
})(); |