(function () { 'use strict'; function typoCssVarsForEl(typo, prefix, el) { if (!typo) return; if (typo.family) el.style.setProperty(prefix + 'font-family', "'" + typo.family + "', sans-serif"); if (typo.weight) el.style.setProperty(prefix + 'font-weight', typo.weight); if (typo.transform) el.style.setProperty(prefix + 'text-transform', typo.transform); if (typo.style) el.style.setProperty(prefix + 'font-style', typo.style); if (typo.decoration) el.style.setProperty(prefix + 'text-decoration', typo.decoration); var su = typo.sizeUnit || 'px'; if (typo.sizeDesktop !== '' && typo.sizeDesktop != null) el.style.setProperty(prefix + 'font-size-d', typo.sizeDesktop + su); if (typo.sizeTablet !== '' && typo.sizeTablet != null) el.style.setProperty(prefix + 'font-size-t', typo.sizeTablet + su); if (typo.sizeMobile !== '' && typo.sizeMobile != null) el.style.setProperty(prefix + 'font-size-m', typo.sizeMobile + su); var lhu = typo.lineHeightUnit || 'px'; if (typo.lineHeightDesktop !== '' && typo.lineHeightDesktop != null) el.style.setProperty(prefix + 'line-height-d', typo.lineHeightDesktop + lhu); if (typo.lineHeightTablet !== '' && typo.lineHeightTablet != null) el.style.setProperty(prefix + 'line-height-t', typo.lineHeightTablet + lhu); if (typo.lineHeightMobile !== '' && typo.lineHeightMobile != null) el.style.setProperty(prefix + 'line-height-m', typo.lineHeightMobile + lhu); var lsu = typo.letterSpacingUnit || 'px'; if (typo.letterSpacingDesktop !== '' && typo.letterSpacingDesktop != null) { el.style.setProperty(prefix + 'letter-spacing-d', typo.letterSpacingDesktop + lsu); el.style.setProperty(prefix + 'letter-spacing', typo.letterSpacingDesktop + lsu); } if (typo.letterSpacingTablet !== '' && typo.letterSpacingTablet != null) el.style.setProperty(prefix + 'letter-spacing-t', typo.letterSpacingTablet + lsu); if (typo.letterSpacingMobile !== '' && typo.letterSpacingMobile != null) el.style.setProperty(prefix + 'letter-spacing-m', typo.letterSpacingMobile + lsu); var wsu = typo.wordSpacingUnit || 'px'; if (typo.wordSpacingDesktop !== '' && typo.wordSpacingDesktop != null) { el.style.setProperty(prefix + 'word-spacing-d', typo.wordSpacingDesktop + wsu); el.style.setProperty(prefix + 'word-spacing', typo.wordSpacingDesktop + wsu); } if (typo.wordSpacingTablet !== '' && typo.wordSpacingTablet != null) el.style.setProperty(prefix + 'word-spacing-t', typo.wordSpacingTablet + wsu); if (typo.wordSpacingMobile !== '' && typo.wordSpacingMobile != null) el.style.setProperty(prefix + 'word-spacing-m', typo.wordSpacingMobile + wsu); } // ── Base64 helpers with Unicode support ────────────────────────────── function toBase64(str, urlSafe, lineBreaks) { var b64; try { // TextEncoder → Uint8Array → binary string → btoa handles Unicode cleanly var bytes = new TextEncoder().encode(str); var bin = ''; bytes.forEach(function(b) { bin += String.fromCharCode(b); }); b64 = btoa(bin); } catch(e) { return { error: 'Encoding error: ' + e.message }; } if (urlSafe) { b64 = b64.replace(/\+/g,'-').replace(/\//g,'_'); } if (lineBreaks) { var lines = []; for (var i = 0; i < b64.length; i += 76) { lines.push(b64.slice(i, i+76)); } b64 = lines.join('\n'); } return { result: b64 }; } function fromBase64(str, urlSafe) { try { var s = str.trim(); if (urlSafe) { s = s.replace(/-/g,'+').replace(/_/g,'/'); } s = s.replace(/\s/g, ''); var bin = atob(s); var bytes = new Uint8Array(bin.length); for (var i=0; i' + (o.title ? '

' + o.title + '

' : '') + (o.subtitle ? '

' + o.subtitle + '

' : '') + '
' + '' + '' + '
' + '' + '' + '
' + (o.showUrlSafe ? '' : '') + (o.showLineBreaks ? '' : '') + '
' + '
' + 'Base64 Output' + '' + '
' + '
' + 'Output will appear here…' + 'Copied!' + '
' + (o.showStats ? '
' + 'Input: 0 chars' + 'Output: 0 chars' + 'Ratio: —' + '
' : '') + ''; var wrapEl = root.querySelector('.bkbg-b64-wrap'); if (wrapEl) { typoCssVarsForEl(opts.titleTypo, '--bkbg-b64-title-', wrapEl); typoCssVarsForEl(opts.subtitleTypo, '--bkbg-b64-sub-', wrapEl); } var inEl = root.querySelector('#b64-input'); var inLabel = root.querySelector('#b64-in-label'); var outLabel = root.querySelector('#b64-out-label'); var outEl = root.querySelector('#b64-output'); var copyBtn = root.querySelector('#b64-copy'); var toast = root.querySelector('#b64-toast'); var urlsafeEl = root.querySelector('#b64-urlsafe'); var lbEl = root.querySelector('#b64-linebreaks'); var statIn = root.querySelector('#b64-stat-in'); var statOut = root.querySelector('#b64-stat-out'); var statRatio = root.querySelector('#b64-stat-ratio'); var tabs = root.querySelectorAll('.bkbg-b64-tab'); var currentOutput = ''; function setMode(m) { mode = m; tabs.forEach(function(t) { var act = t.getAttribute('data-mode') === mode; t.classList.toggle('active', act); t.style.background = act ? o.accentColor : ''; t.style.borderColor = act ? o.accentColor : ''; t.style.color = act ? '#fff' : o.labelColor; }); inLabel.textContent = mode === 'encode' ? 'Plain Text' : 'Base64 String'; outLabel.textContent = mode === 'encode' ? 'Base64 Output' : 'Decoded Text'; inEl.placeholder = mode === 'encode' ? 'Enter text to encode…' : 'Enter Base64 to decode…'; inEl.value = ''; outEl.innerHTML = 'Output will appear here…'; currentOutput = ''; if (statIn) statIn.textContent = 'Input: 0 chars'; if (statOut) statOut.textContent = 'Output: 0 chars'; if (statRatio) statRatio.textContent = 'Ratio: —'; } function convert() { var text = inEl.value; var urlSafe = urlsafeEl ? urlsafeEl.checked : false; var lb = lbEl ? lbEl.checked : false; var res; if (!text) { outEl.innerHTML = 'Output will appear here…Copied!'; currentOutput = ''; return; } if (mode === 'encode') { res = toBase64(text, urlSafe, lb); } else { res = fromBase64(text, urlSafe); } if (res.error) { outEl.innerHTML = 'Copied!' + '' + res.error + ''; currentOutput = ''; } else { var esc = res.result.replace(/&/g,'&').replace(//g,'>'); outEl.innerHTML = 'Copied!' + esc; currentOutput = res.result; toast = root.querySelector('#b64-toast'); if (o.showStats) { var inLen = text.length; var outLen = res.result.replace(/\n/g,'').length; var ratio = inLen > 0 ? (outLen / inLen).toFixed(2) + 'x' : '—'; if (statIn) statIn.textContent = 'Input: ' + inLen + ' chars'; if (statOut) statOut.textContent = 'Output: ' + outLen + ' chars'; if (statRatio) statRatio.textContent = 'Ratio: ' + ratio; } } } // bind tab clicks tabs.forEach(function(t) { t.addEventListener('click', function(){ setMode(t.getAttribute('data-mode')); }); }); // live conversion on input inEl.addEventListener('input', convert); if (urlsafeEl) urlsafeEl.addEventListener('change', convert); if (lbEl) lbEl.addEventListener('change', convert); // copy button copyBtn.addEventListener('click', function() { if (!currentOutput) return; navigator.clipboard.writeText(currentOutput).then(function() { var t2 = root.querySelector('#b64-toast'); if (t2) { t2.classList.add('show'); setTimeout(function(){ t2.classList.remove('show'); }, 1800); } }).catch(function(){}); }); // initial setMode(mode); if (o.defaultInput) { inEl.value = o.defaultInput; convert(); } }); })();