| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var STOP_WORDS = { the:1,a:1,an:1,and:1,or:1,but:1,in:1,on:1,at:1,to:1,for:1,of:1,with:1,by:1,is:1,it:1,its:1,was:1,are:1,be:1,this:1,that:1,as:1,i:1,we:1,you:1,he:1,she:1,they:1,my:1,his:1,her:1,our:1,your:1,their:1,not:1,no:1,so:1,if:1,from:1,into:1,do:1,did:1,had:1,has:1,have:1,will:1,can:1,may:1,all:1,been:1,were:1 }; |
| 5 |
|
| 6 |
function analyze(text) { |
| 7 |
var trimmed = text.trim(); |
| 8 |
var words = trimmed ? trimmed.split(/\s+/).filter(function (w) { return w.replace(/[^a-zA-Z]/g,'').length > 0; }) : []; |
| 9 |
var sentences = trimmed ? trimmed.split(/[.!?]+/).filter(function (s) { return s.trim().length > 0; }) : []; |
| 10 |
var paragraphs = trimmed ? trimmed.split(/\n\s*\n/).filter(function (p) { return p.trim().length > 0; }) : []; |
| 11 |
var chars = trimmed.length; |
| 12 |
var charsNoSp = trimmed.replace(/\s/g, '').length; |
| 13 |
var cleanWords = words.map(function (w) { return w.replace(/[^a-zA-Z']/g,'').toLowerCase(); }).filter(function(w){ return w.length > 0; }); |
| 14 |
var avgWordLen = cleanWords.length ? (cleanWords.map(function(w){ return w.length; }).reduce(function(a,b){return a+b;},0) / cleanWords.length) : 0; |
| 15 |
var avgSentLen = sentences.length ? (words.length / sentences.length) : 0; |
| 16 |
var readingS = words.length / 200 * 60; |
| 17 |
var uniqueSet = {}; |
| 18 |
cleanWords.forEach(function (w) { uniqueSet[w] = true; }); |
| 19 |
var uniqueWords = Object.keys(uniqueSet).length; |
| 20 |
var vocabRich = cleanWords.length ? Math.round((uniqueWords / cleanWords.length) * 100) : 0; |
| 21 |
var longestWord = cleanWords.reduce(function (a,w) { return w.length > a.length ? w : a; }, ''); |
| 22 |
|
| 23 |
// Frequency (skip stop words) |
| 24 |
var freq = {}; |
| 25 |
cleanWords.forEach(function (w) { |
| 26 |
if (w.length > 1 && !STOP_WORDS[w]) freq[w] = (freq[w]||0) + 1; |
| 27 |
}); |
| 28 |
var freqList = Object.keys(freq) |
| 29 |
.map(function (k) { return { word:k, count:freq[k] }; }) |
| 30 |
.sort(function (a,b) { return b.count - a.count; }); |
| 31 |
|
| 32 |
return { words:words.length, sentences:sentences.length, paragraphs:paragraphs.length, |
| 33 |
chars:chars, charsNoSp:charsNoSp, avgWordLen:avgWordLen, avgSentLen:avgSentLen, |
| 34 |
readingS:readingS, uniqueWords:uniqueWords, vocabRich:vocabRich, |
| 35 |
longestWord:longestWord, freqList:freqList }; |
| 36 |
} |
| 37 |
|
| 38 |
function fmtRead(secs) { |
| 39 |
if (secs < 60) return Math.ceil(secs) + 's'; |
| 40 |
return Math.ceil(secs / 60) + ' min'; |
| 41 |
} |
| 42 |
|
| 43 |
function buildBlock(root) { |
| 44 |
var o; |
| 45 |
try { o = JSON.parse(root.getAttribute('data-opts')); } catch (e) { return; } |
| 46 |
|
| 47 |
// CSS vars |
| 48 |
root.style.setProperty('--ta-accent', o.accentColor || '#8b5cf6'); |
| 49 |
root.style.setProperty('--ta-bar-color', o.barColor || '#8b5cf6'); |
| 50 |
root.style.setProperty('--ta-stat-bg', o.statBg || '#f5f3ff'); |
| 51 |
root.style.setProperty('--ta-stat-num-color', o.statNumColor || '#6d28d9'); |
| 52 |
root.style.setProperty('--ta-title-color', o.titleColor || '#111827'); |
| 53 |
root.style.setProperty('--ta-subtitle-color', o.subtitleColor || '#6b7280'); |
| 54 |
root.style.setProperty('--ta-label-color', o.labelColor || '#374151'); |
| 55 |
root.style.setProperty('--ta-input-bg', o.inputBg || '#f8fafc'); |
| 56 |
root.style.setProperty('--ta-max-width', (o.contentMaxWidth||760)+'px'); |
| 57 |
|
| 58 |
var freqTopN = o.freqTopN || 8; |
| 59 |
|
| 60 |
// Build wrap |
| 61 |
var wrap = document.createElement('div'); |
| 62 |
wrap.className = 'bkbg-ta-wrap'; |
| 63 |
wrap.style.background = o.sectionBg || '#faf5ff'; |
| 64 |
root.appendChild(wrap); |
| 65 |
|
| 66 |
if (o.showTitle && o.title) { |
| 67 |
var h = document.createElement('h3'); |
| 68 |
h.className = 'bkbg-ta-title'; |
| 69 |
h.textContent = o.title; |
| 70 |
wrap.appendChild(h); |
| 71 |
} |
| 72 |
if (o.showSubtitle && o.subtitle) { |
| 73 |
var sub = document.createElement('p'); |
| 74 |
sub.className = 'bkbg-ta-subtitle'; |
| 75 |
sub.textContent = o.subtitle; |
| 76 |
wrap.appendChild(sub); |
| 77 |
} |
| 78 |
|
| 79 |
// Copy button |
| 80 |
var copyRow = document.createElement('div'); |
| 81 |
copyRow.className = 'bkbg-ta-copy-row'; |
| 82 |
var copyBtn = document.createElement('button'); |
| 83 |
copyBtn.className = 'bkbg-ta-copy-btn'; |
| 84 |
copyBtn.textContent = '📋 Copy Text'; |
| 85 |
copyBtn.addEventListener('click', function () { |
| 86 |
navigator.clipboard.writeText(textarea.value).then(function () { |
| 87 |
copyBtn.textContent = '✓ Copied!'; |
| 88 |
setTimeout(function () { copyBtn.textContent = '📋 Copy Text'; }, 1500); |
| 89 |
}); |
| 90 |
}); |
| 91 |
copyRow.appendChild(copyBtn); |
| 92 |
wrap.appendChild(copyRow); |
| 93 |
|
| 94 |
// Textarea |
| 95 |
var textarea = document.createElement('textarea'); |
| 96 |
textarea.className = 'bkbg-ta-input'; |
| 97 |
textarea.rows = 7; |
| 98 |
textarea.placeholder = o.placeholder || 'Paste your text here…'; |
| 99 |
textarea.value = o.defaultText || ''; |
| 100 |
wrap.appendChild(textarea); |
| 101 |
|
| 102 |
// Results zone |
| 103 |
var results = document.createElement('div'); |
| 104 |
wrap.appendChild(results); |
| 105 |
|
| 106 |
// ── Render results ── |
| 107 |
var timer = null; |
| 108 |
function render() { |
| 109 |
var text = textarea.value; |
| 110 |
var res = analyze(text); |
| 111 |
results.innerHTML = ''; |
| 112 |
|
| 113 |
if (!text.trim()) { |
| 114 |
var empty = document.createElement('div'); |
| 115 |
empty.className = 'bkbg-ta-empty'; |
| 116 |
empty.textContent = '✏️ Paste or type text above to see analysis'; |
| 117 |
results.appendChild(empty); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
// Basic stats |
| 122 |
if (o.showBasicStats) { |
| 123 |
var bHead = document.createElement('div'); |
| 124 |
bHead.className = 'bkbg-ta-section-head'; |
| 125 |
bHead.textContent = '📊 Basic Statistics'; |
| 126 |
results.appendChild(bHead); |
| 127 |
|
| 128 |
var bGrid = document.createElement('div'); |
| 129 |
bGrid.className = 'bkbg-ta-stats-grid'; |
| 130 |
[ |
| 131 |
{ label:'Words', val: res.words }, |
| 132 |
{ label:'Characters', val: res.chars }, |
| 133 |
{ label:'No Spaces', val: res.charsNoSp }, |
| 134 |
{ label:'Sentences', val: res.sentences }, |
| 135 |
{ label:'Paragraphs', val: res.paragraphs }, |
| 136 |
{ label:'Read Time', val: fmtRead(res.readingS) } |
| 137 |
].forEach(function (s) { |
| 138 |
var card = document.createElement('div'); |
| 139 |
card.className = 'bkbg-ta-stat'; |
| 140 |
var num = document.createElement('div'); |
| 141 |
num.className = 'bkbg-ta-stat-num'; |
| 142 |
num.textContent = s.val; |
| 143 |
var lbl = document.createElement('div'); |
| 144 |
lbl.className = 'bkbg-ta-stat-label'; |
| 145 |
lbl.textContent = s.label; |
| 146 |
card.appendChild(num); |
| 147 |
card.appendChild(lbl); |
| 148 |
bGrid.appendChild(card); |
| 149 |
}); |
| 150 |
results.appendChild(bGrid); |
| 151 |
} |
| 152 |
|
| 153 |
// Vocab stats |
| 154 |
if (o.showVocabStats) { |
| 155 |
var vHead = document.createElement('div'); |
| 156 |
vHead.className = 'bkbg-ta-section-head'; |
| 157 |
vHead.textContent = '📚 Vocabulary Insights'; |
| 158 |
results.appendChild(vHead); |
| 159 |
|
| 160 |
var vGrid = document.createElement('div'); |
| 161 |
vGrid.className = 'bkbg-ta-stats-grid'; |
| 162 |
var vItems = [ |
| 163 |
{ label:'Unique Words', val: res.uniqueWords }, |
| 164 |
{ label:'Vocabulary %', val: res.vocabRich + '%' }, |
| 165 |
{ label:'Avg Word Len', val: res.avgWordLen.toFixed(1) + ' ch' }, |
| 166 |
{ label:'Avg Sent Len', val: res.avgSentLen.toFixed(1) + ' wds' } |
| 167 |
]; |
| 168 |
if (o.showLongestWord && res.longestWord) { |
| 169 |
vItems.push({ label:'Longest Word', val: res.longestWord }); |
| 170 |
} |
| 171 |
vItems.forEach(function (s) { |
| 172 |
var card = document.createElement('div'); |
| 173 |
card.className = 'bkbg-ta-stat'; |
| 174 |
var num = document.createElement('div'); |
| 175 |
num.className = 'bkbg-ta-stat-num'; |
| 176 |
num.style.fontSize = s.label === 'Longest Word' ? '16px' : ''; |
| 177 |
num.textContent = s.val; |
| 178 |
var lbl = document.createElement('div'); |
| 179 |
lbl.className = 'bkbg-ta-stat-label'; |
| 180 |
lbl.textContent = s.label; |
| 181 |
card.appendChild(num); |
| 182 |
card.appendChild(lbl); |
| 183 |
vGrid.appendChild(card); |
| 184 |
}); |
| 185 |
results.appendChild(vGrid); |
| 186 |
} |
| 187 |
|
| 188 |
// Frequency chart |
| 189 |
if (o.showFrequency && res.freqList.length > 0) { |
| 190 |
var fHead = document.createElement('div'); |
| 191 |
fHead.className = 'bkbg-ta-section-head'; |
| 192 |
fHead.textContent = '🔠 Top Words (stop words excluded)'; |
| 193 |
results.appendChild(fHead); |
| 194 |
|
| 195 |
var fList = document.createElement('div'); |
| 196 |
fList.className = 'bkbg-ta-freq-list'; |
| 197 |
|
| 198 |
var topItems = res.freqList.slice(0, freqTopN); |
| 199 |
var maxFreq = topItems.length > 0 ? topItems[0].count : 1; |
| 200 |
|
| 201 |
topItems.forEach(function (item) { |
| 202 |
var row = document.createElement('div'); |
| 203 |
row.className = 'bkbg-ta-freq-row'; |
| 204 |
|
| 205 |
var word = document.createElement('div'); |
| 206 |
word.className = 'bkbg-ta-freq-word'; |
| 207 |
word.textContent = item.word; |
| 208 |
word.title = item.word; |
| 209 |
|
| 210 |
var track = document.createElement('div'); |
| 211 |
track.className = 'bkbg-ta-freq-track'; |
| 212 |
var bar = document.createElement('div'); |
| 213 |
bar.className = 'bkbg-ta-freq-bar'; |
| 214 |
bar.style.width = Math.round((item.count / maxFreq) * 100) + '%'; |
| 215 |
track.appendChild(bar); |
| 216 |
|
| 217 |
var cnt = document.createElement('div'); |
| 218 |
cnt.className = 'bkbg-ta-freq-count'; |
| 219 |
cnt.textContent = item.count; |
| 220 |
|
| 221 |
row.appendChild(word); |
| 222 |
row.appendChild(track); |
| 223 |
row.appendChild(cnt); |
| 224 |
fList.appendChild(row); |
| 225 |
}); |
| 226 |
results.appendChild(fList); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
// Debounced live update |
| 231 |
textarea.addEventListener('input', function () { |
| 232 |
clearTimeout(timer); |
| 233 |
timer = setTimeout(render, 150); |
| 234 |
}); |
| 235 |
|
| 236 |
render(); |
| 237 |
} |
| 238 |
|
| 239 |
document.querySelectorAll('.bkbg-ta-app').forEach(buildBlock); |
| 240 |
})(); |
| 241 |
|