| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function uuidV4() { |
| 5 |
if (typeof crypto !== 'undefined' && crypto.randomUUID) { |
| 6 |
return crypto.randomUUID(); |
| 7 |
} |
| 8 |
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
| 9 |
var r = Math.random() * 16 | 0; |
| 10 |
var v = c === 'x' ? r : (r & 0x3 | 0x8); |
| 11 |
return v.toString(16); |
| 12 |
}); |
| 13 |
} |
| 14 |
|
| 15 |
function uuidV1() { |
| 16 |
var now = Date.now(); |
| 17 |
var timeHex = now.toString(16).padStart(12, '0'); |
| 18 |
var hi = timeHex.slice(0, 8); |
| 19 |
var lo = timeHex.slice(8, 12); |
| 20 |
var rand1 = Math.floor(Math.random() * 0x3fff | 0x8000).toString(16); |
| 21 |
var rand2 = Array.from({ length: 12 }, function () { return Math.floor(Math.random() * 16).toString(16); }).join(''); |
| 22 |
return lo + '-' + hi.slice(4, 8) + '-1' + hi.slice(1, 4) + '-' + rand1 + '-' + rand2; |
| 23 |
} |
| 24 |
|
| 25 |
function rgbFromHex(hex) { |
| 26 |
var c = hex.replace('#', ''); |
| 27 |
return { |
| 28 |
r: parseInt(c.slice(0, 2), 16), |
| 29 |
g: parseInt(c.slice(2, 4), 16), |
| 30 |
b: parseInt(c.slice(4, 6), 16) |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
function luminance(hex) { |
| 35 |
var rgb = rgbFromHex(hex); |
| 36 |
return 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b; |
| 37 |
} |
| 38 |
|
| 39 |
function textOn(bg) { return luminance(bg) > 140 ? '#0f172a' : '#f8fafc'; } |
| 40 |
|
| 41 |
function copyText(text, btn) { |
| 42 |
navigator.clipboard && navigator.clipboard.writeText(text).catch(function () { |
| 43 |
var ta = document.createElement('textarea'); |
| 44 |
ta.value = text; |
| 45 |
document.body.appendChild(ta); |
| 46 |
ta.select(); |
| 47 |
document.execCommand('copy'); |
| 48 |
document.body.removeChild(ta); |
| 49 |
}); |
| 50 |
var orig = btn.textContent; |
| 51 |
btn.textContent = 'Copied!'; |
| 52 |
setTimeout(function () { btn.textContent = orig; }, 1500); |
| 53 |
} |
| 54 |
|
| 55 |
function initBlock(root) { |
| 56 |
var opts; |
| 57 |
try { opts = JSON.parse(root.getAttribute('data-opts')); } catch (e) { return; } |
| 58 |
|
| 59 |
var a = opts; |
| 60 |
var version = a.defaultVersion || 'v4'; |
| 61 |
var count = parseInt(a.defaultCount) || 5; |
| 62 |
var uppercase = !!a.showUppercase; |
| 63 |
var history = []; |
| 64 |
var uuids = []; |
| 65 |
|
| 66 |
// Build UI |
| 67 |
root.innerHTML = ''; |
| 68 |
|
| 69 |
var wrap = document.createElement('div'); |
| 70 |
wrap.className = 'bkbg-uuid-wrap'; |
| 71 |
wrap.style.cssText = 'background:' + a.sectionBg + ';max-width:' + a.contentMaxWidth + 'px;margin:0 auto;'; |
| 72 |
root.appendChild(wrap); |
| 73 |
|
| 74 |
if (a.showTitle) { |
| 75 |
var h = document.createElement('div'); |
| 76 |
h.className = 'bkbg-uuid-title'; |
| 77 |
h.style.color = a.titleColor || ''; |
| 78 |
h.textContent = a.title; |
| 79 |
wrap.appendChild(h); |
| 80 |
} |
| 81 |
|
| 82 |
if (a.showSubtitle) { |
| 83 |
var s = document.createElement('div'); |
| 84 |
s.className = 'bkbg-uuid-subtitle'; |
| 85 |
s.style.color = a.subtitleColor; |
| 86 |
s.textContent = a.subtitle; |
| 87 |
wrap.appendChild(s); |
| 88 |
} |
| 89 |
|
| 90 |
var card = document.createElement('div'); |
| 91 |
card.className = 'bkbg-uuid-card'; |
| 92 |
card.style.background = a.cardBg; |
| 93 |
wrap.appendChild(card); |
| 94 |
|
| 95 |
// Version tabs |
| 96 |
var tabs = document.createElement('div'); |
| 97 |
tabs.className = 'bkbg-uuid-tabs'; |
| 98 |
card.appendChild(tabs); |
| 99 |
|
| 100 |
function renderTabBtn(ver) { |
| 101 |
var btn = document.createElement('button'); |
| 102 |
btn.className = 'bkbg-uuid-tab-btn'; |
| 103 |
btn.textContent = 'UUID ' + ver.toUpperCase(); |
| 104 |
updateTabStyle(btn, ver === version); |
| 105 |
btn.addEventListener('click', function () { |
| 106 |
version = ver; |
| 107 |
tabs.querySelectorAll('.bkbg-uuid-tab-btn').forEach(function (b, i) { |
| 108 |
updateTabStyle(b, ['v4', 'v1'][i] === version); |
| 109 |
}); |
| 110 |
}); |
| 111 |
return btn; |
| 112 |
} |
| 113 |
|
| 114 |
function updateTabStyle(btn, active) { |
| 115 |
btn.style.cssText = 'background:' + (active ? a.accentColor : '#e5e7eb') + ';color:' + (active ? '#fff' : '#374151') + ';'; |
| 116 |
} |
| 117 |
|
| 118 |
tabs.appendChild(renderTabBtn('v4')); |
| 119 |
tabs.appendChild(renderTabBtn('v1')); |
| 120 |
|
| 121 |
// Count buttons |
| 122 |
var countWrap = document.createElement('div'); |
| 123 |
countWrap.className = 'bkbg-uuid-count-btns'; |
| 124 |
card.appendChild(countWrap); |
| 125 |
|
| 126 |
[1, 5, 10, 25, 50].forEach(function (n) { |
| 127 |
var btn = document.createElement('button'); |
| 128 |
btn.className = 'bkbg-uuid-count-btn'; |
| 129 |
btn.textContent = n; |
| 130 |
updateCountStyle(btn, n === count); |
| 131 |
btn.addEventListener('click', function () { |
| 132 |
count = n; |
| 133 |
countWrap.querySelectorAll('.bkbg-uuid-count-btn').forEach(function (b, i) { |
| 134 |
updateCountStyle(b, [1, 5, 10, 25, 50][i] === count); |
| 135 |
}); |
| 136 |
}); |
| 137 |
countWrap.appendChild(btn); |
| 138 |
}); |
| 139 |
|
| 140 |
function updateCountStyle(btn, active) { |
| 141 |
btn.style.cssText = 'background:' + (active ? a.accentColor : '#e5e7eb') + ';color:' + (active ? '#fff' : '#374151') + ';border-color:' + (active ? a.accentColor : 'transparent') + ';'; |
| 142 |
} |
| 143 |
|
| 144 |
// Options row |
| 145 |
var optRow = document.createElement('div'); |
| 146 |
optRow.className = 'bkbg-uuid-options'; |
| 147 |
card.appendChild(optRow); |
| 148 |
|
| 149 |
var ucLabel = document.createElement('label'); |
| 150 |
ucLabel.className = 'bkbg-uuid-opt-label'; |
| 151 |
ucLabel.style.color = a.labelColor; |
| 152 |
var ucCheck = document.createElement('input'); |
| 153 |
ucCheck.type = 'checkbox'; |
| 154 |
ucCheck.checked = uppercase; |
| 155 |
ucLabel.appendChild(ucCheck); |
| 156 |
ucLabel.appendChild(document.createTextNode(' Uppercase')); |
| 157 |
ucCheck.addEventListener('change', function () { uppercase = ucCheck.checked; }); |
| 158 |
optRow.appendChild(ucLabel); |
| 159 |
|
| 160 |
// UUID list container |
| 161 |
var listEl = document.createElement('div'); |
| 162 |
listEl.className = 'bkbg-uuid-list'; |
| 163 |
card.appendChild(listEl); |
| 164 |
|
| 165 |
// Actions |
| 166 |
var actionsRow = document.createElement('div'); |
| 167 |
actionsRow.className = 'bkbg-uuid-actions'; |
| 168 |
card.appendChild(actionsRow); |
| 169 |
|
| 170 |
var genBtn = document.createElement('button'); |
| 171 |
genBtn.className = 'bkbg-uuid-btn'; |
| 172 |
genBtn.style.cssText = 'background:' + a.accentColor + ';color:#fff;'; |
| 173 |
genBtn.textContent = 'Generate'; |
| 174 |
actionsRow.appendChild(genBtn); |
| 175 |
|
| 176 |
var copyAllBtn = document.createElement('button'); |
| 177 |
copyAllBtn.className = 'bkbg-uuid-btn bkbg-uuid-btn-outline'; |
| 178 |
copyAllBtn.style.cssText = 'color:' + a.accentColor + ';border-color:' + a.accentColor + ';'; |
| 179 |
copyAllBtn.textContent = 'Copy All'; |
| 180 |
actionsRow.appendChild(copyAllBtn); |
| 181 |
|
| 182 |
var copyMsg = document.createElement('span'); |
| 183 |
copyMsg.className = 'bkbg-uuid-copy-msg'; |
| 184 |
copyMsg.style.color = a.accentColor; |
| 185 |
copyMsg.textContent = 'Copied to clipboard!'; |
| 186 |
actionsRow.appendChild(copyMsg); |
| 187 |
|
| 188 |
function showCopyMsg() { |
| 189 |
copyMsg.classList.add('bkbg-uuid-show'); |
| 190 |
setTimeout(function () { copyMsg.classList.remove('bkbg-uuid-show'); }, 2000); |
| 191 |
} |
| 192 |
|
| 193 |
function generate() { |
| 194 |
uuids = []; |
| 195 |
for (var i = 0; i < count; i++) { |
| 196 |
uuids.push(version === 'v1' ? uuidV1() : uuidV4()); |
| 197 |
} |
| 198 |
renderList(); |
| 199 |
if (a.showHistory) { |
| 200 |
history = uuids.concat(history).slice(0, 20); |
| 201 |
renderHistory(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
function renderList() { |
| 206 |
listEl.innerHTML = ''; |
| 207 |
uuids.forEach(function (uuid) { |
| 208 |
var display = uppercase ? uuid.toUpperCase() : uuid; |
| 209 |
var item = document.createElement('div'); |
| 210 |
item.className = 'bkbg-uuid-item'; |
| 211 |
item.style.cssText = 'background:' + a.uuidBg + ';color:' + a.uuidColor + ';'; |
| 212 |
|
| 213 |
var val = document.createElement('span'); |
| 214 |
val.className = 'bkbg-uuid-val'; |
| 215 |
val.textContent = display; |
| 216 |
item.appendChild(val); |
| 217 |
|
| 218 |
var copyBtn = document.createElement('button'); |
| 219 |
copyBtn.className = 'bkbg-uuid-copy-one'; |
| 220 |
copyBtn.style.cssText = 'color:' + a.uuidColor + ';border-color:' + a.uuidColor + ';background:rgba(0,0,0,0.2);'; |
| 221 |
copyBtn.textContent = 'Copy'; |
| 222 |
copyBtn.addEventListener('click', function () { copyText(display, copyBtn); }); |
| 223 |
item.appendChild(copyBtn); |
| 224 |
|
| 225 |
listEl.appendChild(item); |
| 226 |
}); |
| 227 |
} |
| 228 |
|
| 229 |
genBtn.addEventListener('click', generate); |
| 230 |
|
| 231 |
copyAllBtn.addEventListener('click', function () { |
| 232 |
if (uuids.length === 0) { generate(); } |
| 233 |
var text = uuids.map(function (u) { return uppercase ? u.toUpperCase() : u; }).join('\n'); |
| 234 |
copyText(text, copyAllBtn); |
| 235 |
showCopyMsg(); |
| 236 |
}); |
| 237 |
|
| 238 |
// History |
| 239 |
var histEl = null; |
| 240 |
if (a.showHistory) { |
| 241 |
histEl = document.createElement('div'); |
| 242 |
histEl.className = 'bkbg-uuid-history'; |
| 243 |
histEl.style.background = a.cardBg; |
| 244 |
var histTitle = document.createElement('div'); |
| 245 |
histTitle.className = 'bkbg-uuid-history-title'; |
| 246 |
histTitle.style.color = a.labelColor; |
| 247 |
histTitle.textContent = 'History'; |
| 248 |
histEl.appendChild(histTitle); |
| 249 |
var histList = document.createElement('ul'); |
| 250 |
histList.className = 'bkbg-uuid-history-list'; |
| 251 |
histEl.appendChild(histList); |
| 252 |
wrap.appendChild(histEl); |
| 253 |
} |
| 254 |
|
| 255 |
function renderHistory() { |
| 256 |
if (!histEl) return; |
| 257 |
var histList = histEl.querySelector('.bkbg-uuid-history-list'); |
| 258 |
histList.innerHTML = ''; |
| 259 |
history.forEach(function (u) { |
| 260 |
var li = document.createElement('li'); |
| 261 |
li.className = 'bkbg-uuid-history-item'; |
| 262 |
li.style.color = a.labelColor; |
| 263 |
li.textContent = uppercase ? u.toUpperCase() : u; |
| 264 |
histList.appendChild(li); |
| 265 |
}); |
| 266 |
} |
| 267 |
|
| 268 |
// Generate on load |
| 269 |
generate(); |
| 270 |
} |
| 271 |
|
| 272 |
function init() { |
| 273 |
document.querySelectorAll('.bkbg-uuid-app').forEach(initBlock); |
| 274 |
} |
| 275 |
|
| 276 |
if (document.readyState === 'loading') { |
| 277 |
document.addEventListener('DOMContentLoaded', init); |
| 278 |
} else { |
| 279 |
init(); |
| 280 |
} |
| 281 |
})(); |
| 282 |
|