// ==UserScript== // @name A岛id过滤(只看po主) // @namespace adidfilter // @version 0.1 // @description 在右键菜单中使用该脚本过滤指定的ID,需要跨域权限访问三酱api // @match https://adnmb2.com/t/* // @require https://openuserjs.org/src/libs/sizzle/GM_config.js // @grant GM_getValue // @grant GM_setValue // @grant GM_xmlhttpRequest // @run-at context-menu // @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 url = document.URL.split('/')[4]; var thread = url.substring(0, url.indexOf('?')); thread = thread ? thread : url; var page = parseInt(url.substring(url.indexOf('=') + 1)); page = page != thread && page != 1 ? (page - 1) : 0; var pageCount; //总页面数量 var pageCur; //已经向服务器请求过的页面数量 var p = jQuery('.h-threads-item-reply'); 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('id', 'id-filter-select'); select.onchange = function () { render(jQuery('#id-filter-select option:selected').val() - 1); }; var button = document.createElement('button'); button.innerText = '加载更多'; button.onclick = function () { if (locker + 1 == runCount) { locker++; loadNewPage(30); } else alert('请稍等'); }; var nextPage = document.createElement('button'); 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); } }; adiv.appendChild(select); adiv.appendChild(button); adiv.appendChild(nextPage); //清除原内容 let i = 0; 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 = ''; } for (; i < 20; 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); 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; if (i == endflag - 1) finished(); 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 / 20); i++) { let u = document.createElement('option'); u.innerText = i; select.appendChild(u); } jQuery('#id-filter-select').val(total); render(total - 1); total = i - 1; } runCount++; } function dialog() { //偷懒 用的GM_config做ui GM_config.init({ 'id': 'settings', 'fields': { 'uid': { 'label': '饼干(用分号分隔)', 'type': 'text', 'default': defaultUid } }, 'events': { 'close': function () { uid = GM_config.get('uid').split(';'); 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 / 20); 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]); } } }); GM_config.set('uid', defaultUid); GM_config.save(); GM_config.open(); } //展示过滤后内容 function render(index) { index = index * 20; for (let i = 0; i < 20; i++) { let cont = p[i].getElementsByClassName('h-threads-content')[0]; let info = p[i].getElementsByClassName('h-threads-info')[0]; if (i + index < filtered.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 + '.jpg'); imgb.children[1].setAttribute('href', 'https://nmbimg.fastmirror.org/image/' + f.img + '.jpg'); imgb.children[1].children[0].setAttribute('data-src', 'https://nmbimg.fastmirror.org/thumb/' + f.img + '.jpg'); imgb.children[1].children[0].setAttribute('src', 'https://nmbimg.fastmirror.org/thumb/' + f.img + '.jpg'); p[i].children[1].insertBefore(imgb, info); } } else { let imgb = p[i].children[1].getElementsByClassName('h-threads-img-box')[0]; if (imgb) { p[i].children[1].removeChild(imgb); } cont.innerHTML = ''; info.children[1].innerText = '无内容-点击底部按钮加载更多'; } } //三酱的api initImageBox(); initContent(); } })(); //感谢阅读 欢迎改进( ´∀`)