/*
* @title 匿名質問だけの BA 数を集計
* @description 匿名質問だけの BA 数を集計
* @include http://q.hatena.ne.jp/anonymousanswerlist
* @license MIT License
*/
(() => {
if (location.href !== 'http://q.hatena.ne.jp/anonymousanswerlist') {
if (confirm("use at http://q.hatena.ne.jp/anonymousanswerlist.\n\nJUMP ?")) {
location.href = 'http://q.hatena.ne.jp/anonymousanswerlist';
}
return;
}
function copy_attr(dest, src) {
for (var k in src) {
dest[k] = src[k];
}
}
function create_and_append(tag, parent, opt, style) {
var e = document.createElement(tag);
copy_attr(e, opt);
copy_attr(e.style, style);
if (parent) {
parent.appendChild(e);
}
return e;
}
var n = {q: 0, judge: 0, ba: 0};
var work_area = create_and_append("div", document.body);
var counter = create_and_append("div", work_area, {id: "work_counter", innerHTML: ' '});
var work_frame = create_and_append("iframe", work_area);
work_area.scrollIntoView(false);
work_frame.onload = function() {
console.log("page = " + page);
var w_ = this.contentWindow, d_ = w_.document;
Array.from(d_.querySelectorAll("li.answerlist")).forEach(function(e) {
n.q += 1;
var ba_badge = e.querySelector('img[src="/images/badge-bestanswer-s.gif"]');
if (ba_badge) {
n.judge += 1;
var ans_link = e.querySelector('.answer-detail .section a');
if (ans_link.href == ba_badge.parentNode.href) {
n.ba += 1;
}
}
});
// 「次の20件>」がなくなるまで続ける
var e_next = d_.querySelector('a[rel="next"]');
if (e_next) {
page = page + 1;
load_answerlist(page);
} else {
console.log(n);
document.body.removeChild(work_area);
var s = [
"回答数 : " + n.q,
"BA判定数 : " + n.judge,
"BA数 : " + n.ba,
"BA率 : " + (Math.floor(n.ba * 1000.0 / n.q) / 10.0) + " %"
].join("<br>");
create_and_append("div", document.body, {innerHTML: s}, {border: "1px solid gray", position: "absolute", padding: "0.5ex", marginLeft: "1em",});
}
};
function load_answerlist(page) {
var c = document.getElementById("work_counter");
c.innerHTML = "Loading ... : page " + page;
var url = "http://q.hatena.ne.jp/anonymousanswerlist?page=" + page;
work_frame.src = url;
}
var page = 1;
load_answerlist(page);
})();