Skip to content

Instantly share code, notes, and snippets.

@ccloli
Last active August 29, 2015 14:26
Show Gist options
  • Save ccloli/ec11912fd2c2ec7e1b7e to your computer and use it in GitHub Desktop.
Save ccloli/ec11912fd2c2ec7e1b7e to your computer and use it in GitHub Desktop.
tieba#3938066599 统计脚本
(function(){
var user = /*sessionStorage.getItem('himg-user') ? JSON.parse(sessionStorage.getItem('himg-user')).data : */[];
var frame = document.createElement('div');
var elem = document.getElementsByClassName('l_post');
var xhr = new XMLHttpRequest();
var page = (PageData && PageData.pager && PageData.pager.cur_page) || 1;
var totalPage = (PageData && PageData.pager && PageData.pager.total_page) || 1;
xhr.onload = function(){
frame.innerHTML = xhr.responseText;
totalPage = xhr.responseText.split('"total_page":')[1].split('}')[0].split(',')[0] - 0;
elem = frame.getElementsByClassName('l_post');
calculateMark();
}
if (PageData && PageData.thread && PageData.thread.thread_id == 3938066599 && page == 1) calculateMark();
else {
page = 1;
xhr.open('GET', '/p/3938066599?pn=' + page);
xhr.send();
console.log('正在载入第 ' + page + ' 页...');
}
function calculateMark(){
for (var i = 0; i < elem.length; i++) {
var ldata = JSON.parse(elem[i].dataset.field);
if (ldata.content.post_no == 1) {
if (user.length != 0) user = [];
continue;
}
var content = elem[i].textContent;
var select = null;
var is_repeat = user.some(function(e){return e.name == ldata.author.user_name && e.select != null;});
if (/[\[【][A-F]+[\]】]/.test(content)) {
select = [];
content.match(/[\[【]([A-F]+)[\]】]/)[1].split('').forEach(function(elem){
if (!select.some(function(e){return e == elem;})) {
select.push(elem);
}
});
}
user.push({
name: ldata.author.user_name,
floor: ldata.content.post_no,
select: select,
is_repeat: is_repeat
});
}
if (totalPage == page) {
var mark = [0, 0, 0, 0, 0, 0];
user.forEach(function(elem, index){
console.info('#' + elem.floor + ' (' + elem.name + '): ' + (elem.is_repeat && elem.select != null ? '重复投票' : elem.select == null ? '投票格式不正确或未投票' : elem.select.join('')));
if (elem.select != null && elem.is_repeat == false) elem.select.forEach(function(elem){
mark[elem.charCodeAt(0) - 65]++;
});
});
mark.forEach(function(elem, index){
console.log(String.fromCharCode(index + 65) + ': ' + elem);
});
}
else {
//sessionStorage.setItem('himg-user', JSON.stringify({data: user}));
//window.location.href = '/p/' + PageData.thread.thread_id + '?pn=' + (PageData.pager.cur_page + 1);
xhr.open('GET', '/p/3938066599?pn=' + (++page));
xhr.send();
console.log('正在载入第 ' + page + ' 页...');
}
frame.innerHTML = '';
}
})();
@ccloli
Copy link
Author

ccloli commented Aug 1, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment