// ==UserScript==
// @name A岛id过滤(只看po主)
// @namespace adidfilter
// @version 0.2.4
// @description 在右键菜单中使用该脚本过滤指定的ID,需要跨域权限访问三酱api
// @match https://adnmb2.com/t/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @run-at context-menu
// @require https://cdn.bootcss.com/jquery-scrollTo/2.1.2/jquery.scrollTo.min.js
// @downloadURL none
// ==/UserScript==
(function () {
var uid = []; //需要过滤的id
var total = 0; //当前展示的过滤后页面数量
var endflag; //请求的最大数量
var runCount = 0; //用来区分首次运行
var locker = 0; //防止同时请求
var reply = []; //存放得到的页面数据
var replyPtr; //用来防止重复过滤
var filtered = []; //存放过滤后的回复
var gtot = 0; //当前已获得的页面计数
//页面信息
var url = document.URL.split('/')[4];
var thread = url.substring(0, url.indexOf('?'));
thread = thread ? thread : url;
var page = url.substring(url.indexOf('=') + 1);
page = url.indexOf('r=') != -1 ? 1 : parseInt(page);
gtot = page = page != thread && page != 1 ? (page - 1) : 0;
var pageCount; //总页面数量
var pageCur; //已经向服务器请求过的页面数量
var p = jQuery('.h-threads-item-reply');
p[0].setAttribute('id', 'top');
var defaultUid = jQuery('.h-threads-info-uid')[0].innerText.substring(3);
//ui
var imgbox = document.createElement('div');
imgbox.setAttribute('class', 'h-threads-img-box');
imgbox.innerHTML = `
`;
var adiv = document.createElement('div');
adiv.setAttribute('style', 'margin: 5px');
var select = document.createElement('select');
select.setAttribute('style', 'width: 4rem; height: 2rem; border: 1px solid rgba(0,0,0,.06);');
select.setAttribute('id', 'id-filter-select');
select.onchange = function () {
render(jQuery('#id-filter-select option:selected').val() - 1);
};
var button = document.createElement('button');
button.setAttribute('style', 'margin-left: 5px; color: #07d; background: #fff; height: 2rem; border: 1px solid rgba(0,0,0,.06);');
button.innerText = '加载更多';
button.onclick = function () {
if (locker + 1 == runCount) {
locker++;
loadNewPage(30);
} else
alert('请稍等');
};
var nextPage = document.createElement('button');
nextPage.setAttribute('style', 'margin-left: 5px; color: #07d; background: #fff; height: 2rem; border: 1px solid rgba(0,0,0,.06);');
nextPage.innerText = '下一页';
nextPage.onclick = function () {
let c = parseInt(jQuery('#id-filter-select').val());
if (c != total) {
jQuery('#id-filter-select').val(c + 1);
render(c);
$('html, body').animate({
scrollTop: $('#top').offset().top
}, 600);
}
};
adiv.appendChild(select);
adiv.appendChild(button);
adiv.appendChild(nextPage);
var divprg = document.createElement('div');
divprg.style.top = '3em';
divprg.style.width = '100%';
divprg.style.position = 'fixed';
divprg.style.textAlign = 'center';
var progress = document.createElement('progress');
progress.style.width = '20em';
progress.style.height = '2.5em';
progress.setAttribute('value', 0);
progress.setAttribute('max', 30);
divprg.appendChild(progress);
document.body.appendChild(divprg);
//清除原内容
let i = 0;
try {
for (; i < p.length; i++) {
let img = p[i].getElementsByClassName('h-threads-img-box')[0];
if (img)
img.innerHTML = '';
let po = p[i].getElementsByClassName('uk-text-primary uk-text-small')[0];
if (po)
p[i].getElementsByClassName('h-threads-info')[0].removeChild(po);
p[i].getElementsByClassName('h-threads-content')[0].innerHTML = '';
}
} catch (error) {
alert('过滤出现问题,刷新重试');
window.history.go(0);
}
for (; i < 19; i++) {
p[p.length - 1].after(p[0].cloneNode(true));
}
//首次请求
GM_xmlhttpRequest({
method: 'get',
headers: {
cookie: document.cookie
},
url: 'https://nmb.fastmirror.org/api/thread?id=' + thread + '&page=' + (page + 1),
onload: function (res) {
let con = JSON.parse(res.response);
reply[page] = con;
pageCount = Math.ceil(con.replyCount / 19);
progress.setAttribute('max', pageCount > 30 ? 30 : pageCount);
pageCur = page + 1;
if (pageCount > 1)
loadNewPage(30);
else
finished();
}
});
//shift:向后读取的页面数量
function loadNewPage(shift) {
let i;
endflag = Math.min(pageCount, pageCur + shift);
for (i = pageCur; i < endflag; i++)
fetch(i, i - pageCur);
pageCur = i;
}
//网络请求
function fetch(i, t) {
setTimeout(function () {
GM_xmlhttpRequest({
method: 'get',
headers: {
cookie: document.cookie
},
url: 'https://nmb.fastmirror.org/api/thread?id=' + thread + '&page=' + (i + 1),
onload: function (res) {
let con = JSON.parse(res.response);
reply[i] = con;
gtot++;
if (gtot == endflag - 1) finished();
if (!runCount)
progress.setAttribute('value', i + 1 - page);
else
button.innerText = '加载更多 (' + (i + 1) + '/' + endflag + ')';
}
});
}, 70 * t);
}
function finished() {
if (!runCount)
dialog();
else {
button.innerText = '加载更多 (' + endflag + '/' + endflag + ')';
p = jQuery('.h-threads-item-reply');
//过滤部分
let i;
for (i in reply) {
if (parseInt(i) <= replyPtr) continue;
for (let j = 0; j < reply[i].replys.length; j++)
for (let k = 0; k < uid.length; k++)
if (reply[i].replys[j].userid == uid[k]) {
filtered.push(reply[i].replys[j]);
break;
}
}
replyPtr = parseInt(i);
for (i = total + 1; i <= Math.ceil(filtered.length / 19); i++) {
let u = document.createElement('option');
u.innerText = i;
select.appendChild(u);
}
jQuery('#id-filter-select').val(total + 1);
render(total);
$('html, body').animate({
scrollTop: $('#top').offset().top
}, 600);
total = i - 1;
}
runCount++;
}
function dialog() {
let uidd = prompt("饼干(用分号分隔)", defaultUid);
if (!uidd) window.history.go(0);
uid = uidd.split(';');
document.body.removeChild(divprg);
p = jQuery('.h-threads-item-reply');
let i;
for (i in reply)
for (let j = 0; j < reply[i].replys.length; j++)
for (let k = 0; k < uid.length; k++)
if (reply[i].replys[j].userid == uid[k]) {
filtered.push(reply[i].replys[j]);
break;
}
replyPtr = parseInt(i);
render(0);
for (i = 0; i < Math.ceil(filtered.length / 19); i++) {
let u = document.createElement('option');
u.innerText = i + 1;
select.appendChild(u);
}
total = i;
jQuery('.uk-container')[0].insertBefore(adiv, jQuery('.uk-pagination.uk-pagination-left.h-pagination')[0]);
}
//展示过滤后内容
function render(index) {
index = index * 19;
for (let i = 0; i < 19; i++) {
let cont = p[i].getElementsByClassName('h-threads-content')[0];
let info = p[i].getElementsByClassName('h-threads-info')[0];
let length = filtered.length;
let imgb = p[i].children[1].getElementsByClassName('h-threads-img-box')[0];
if (imgb) {
p[i].children[1].removeChild(imgb);
}
if (i + index < length) {
let f = filtered[i + index];
info.children[0].innerText = f.title;
info.children[1].innerText = f.name;
info.children[2].innerText = f.now;
info.children[3].innerText = 'ID:' + f.userid;
info.children[4].children[0].setAttribute('href', '/f/值班室?r=' + f.id);
info.children[5].setAttribute('href', '/t/' + thread + '?r=' + f.id);
info.children[5].innerText = 'No.' + f.id;
cont.innerHTML = f.content;
if (f.img) {
let imgb = imgbox.cloneNode(true);
imgb.children[0].children[1].setAttribute('href', 'https://nmbimg.fastmirror.org/image/' + f.img + f.ext);
imgb.children[1].setAttribute('href', 'https://nmbimg.fastmirror.org/image/' + f.img + f.ext);
imgb.children[1].children[0].setAttribute('data-src', 'https://nmbimg.fastmirror.org/thumb/' + f.img + f.ext);
imgb.children[1].children[0].setAttribute('src', 'https://nmbimg.fastmirror.org/thumb/' + f.img + f.ext);
p[i].children[1].insertBefore(imgb, info);
}
} else {
let f = {
content: '',
id: '',
img: '',
name: '无内容-点击底部按钮加载更多',
now: '',
title: '无标题',
userid: ''
};
filtered.push(f);
cont.innerHTML = f.content;
info.children[0].innerText = f.title;
info.children[1].innerText = f.name;
info.children[2].innerText = f.now;
info.children[3].innerText = f.userid;
info.children[5].innerText = 'No.' + f.id;
}
}
//三酱的api
initImageBox();
initContent();
}
})();
//感谢阅读 欢迎改进( ´∀`)