/*
* @title count up characters, words, lines at d.hatena.ne.jp/edit
* @description count up characters, words, lines at d.hatena.ne.jp/edit for http://q.hatena.ne.jp/1445242570
* @include http://d.hatena.ne.jp/*
* @license MIT License
* @javascript_url
*/
(function() {
if (location.hostname != "d.hatena.ne.jp") return;
function countup_text(txt) {
function round_number(n) {
return Math.round(n * 100) / 100;
}
var t, ret = []
if ((/[^\n]$/m).test(txt)) {
txt = txt + "\n";
}
// count char
t = txt.replace(/\s/gm, '');
ret.n_char = t.length;
// count line
t = txt.replace(/[^\n]/gm, '');
ret.n_line = t.length;
// count char / line
ret.n_char_line = ret.n_line != 0 ? round_number(parseFloat(ret.n_char) / ret.n_line) : 0
// count word
t = txt.replace(/\S/gm, '|@|');
t = t.replace(/\s+/gm, ' ');
t = t.replace(/\|@\|/gm, '');
ret.n_word = t.length;
// count word / line
ret.n_word_line = ret.n_line != 0 ? round_number(parseFloat(ret.n_word) / ret.n_line) : 0
return ret;
}
function analyze_text(e) {
var result = countup_text(e.value);
output = window.o1445242570;
output.n_char .innerHTML = result.n_char;
output.n_char_line.innerHTML = result.n_char_line;
output.n_line .innerHTML = result.n_line;
output.n_word .innerHTML = result.n_word;
output.n_word_line.innerHTML = result.n_word_line;
console.log("------------------------------------------------------------------------");
console.log("char: " + result.n_char);
console.log("char / line: " + result.n_char_line);
console.log("line: " + result.n_line);
console.log("word: " + result.n_word);
console.log("word / line: " + result.n_word_line);
}
function initialize() {
if (true /* ! window.o1445242570 */) {
var copy_attr = function(dest, src) {
for (var i in src) {
dest[i] = src[i];
}
}
// Listener
var e = document.getElementById("textarea-edit");
e.addEventListener("keyup", function(event) {
analyze_text(event.target);
}, false);
// Element
output = [];
window.o1445242570 = output;
var tbl = document.createElement("table");
copy_attr(tbl.style, {
"borderSpacing" : 0,
"borderCollapse" : 'collapse',
"position" : 'fixed',
"top" : 40,
"right" : 0,
"backgroundColor" : "white",
"zIndex" : 9999,
});
function create_row(tbl, label) {
var cell_style = {
"border" : "1px solid black",
"padding" : 3,
};
var tr, td;
tr = document.createElement("tr");
// label
td = document.createElement("td");
td.innerHTML = label;
copy_attr(td.style, cell_style);
tr.appendChild(td);
// value
td = document.createElement("td");
copy_attr(td.style, cell_style);
copy_attr(td.style, {
"textAlign" : "right",
"minWidth" : "3em",
});
tr.appendChild(td);
tbl.appendChild(tr);
return td;
}
// charset is EUC @ d.hatena.ne.jp
output.n_char = create_row(tbl, "文字数"); // 文字数
output.n_char_line = create_row(tbl, "文字数/行"); // 文字数/行
output.n_line = create_row(tbl, "行数"); // 行数
output.n_word = create_row(tbl, "英単語数"); // 英単語数
output.n_word_line = create_row(tbl, "英単語数/行"); // 英単語数/行
document.body.appendChild(tbl);
}
}
initialize();
var e = document.getElementById("textarea-edit");
analyze_text(e);
})();