Greasy Fork

Greasy Fork is available in English.

长佩专用 - 自动翻页 只显示匿名

自动翻页 只显示匿名

当前为 2019-05-17 提交的版本,查看 最新版本

// ==UserScript==
// @name         长佩专用 - 自动翻页 只显示匿名
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  自动翻页 只显示匿名
// @author       You
// @match        *://allcp.net/forum.php?mod=viewthread&tid=*
// @grant        none
// ==/UserScript==

(function() {
	'use strict';

if (document.URL.length<60) {
	var url = document.URL + "&extra=&page="
	var pageNext = 2;
}else{
	var url = document.URL
	var pageNext = parseInt(url.split("=").reverse()[0]) + 1
}

// 初始化
// 1.1 添加点击的按钮
var btnRight = document.createElement("div");
btnRight.style.cssText = `position:fixed;bottom:140px;right:20px;
padding:7px;width:40px;background:rgb(174,230,255);
font:14px/1.5 '';color:#444;text-align:center;`
btnRight.innerText = "点击显示匿名"
document.body.appendChild(btnRight);

//设置是否隐藏匿名
var onlyAnous = false;

//1.2 原有评论处理
if (onlyAnous) {
	hiddenAnous(document.querySelectorAll("table.plhin"))
}

//2.1 当滚动时检测是否到底
document.onmousewheel = function (eve) {
	var e = eve || window.event;
	if (document.body.clientHeight < e.pageY + 2000) {
		newPage(pageNext);
	}
}

//2.2 获取下一页
function newPage(pageNext) {

	url = url.slice(0, url.lastIndexOf("=") + 1) + pageNext;

	var xhr = new XMLHttpRequest();
	xhr.open("get", url, false); 	// 禁止异步执行
	xhr.onreadystatechange = function () {
		console.log("xhr.ready", xhr.readyState, xhr.status)
		if (xhr.readyState == 4 && xhr.status == 200) {
			callback(xhr.responseText);
		}
	}
	xhr.send("");

}

//2.3 处理下一页的response
function callback(rsp) {
	var d = document.createElement("div");
	d.innerHTML = rsp;

	var nextComment = d.querySelectorAll("table.plhin")
	var ele = document.createElement("div");
	ele.style.cssText = "diaply:flex;justify-content:space-between;width:960px;margin:0 auto;"
	ele.innerHTML= `<div style="width:900px;margin:0 auto;border-bottom:1px dashed #999;text-align:center;padding:10px 0;">本页链接 >>>>>>> <a href="${url}">${url}</a></div>`

	for (var i = 0; i < nextComment.length; i++) {
		nextComment[i].style.width = "980px";
		ele.appendChild(nextComment[i]);
	}
	hiddenAnous(nextComment);
	document.body.appendChild(ele);
	pageNext++;
}

//0. 点击按钮切换显示状态
btnRight.onclick = function(){
	console.log( "onclick",onlyAnous,btnRight.innerText);  
	var aComment = document.querySelectorAll("table.plhin");
	// onlyAnous = false  innertxt = 点击显示匿名
	if (onlyAnous) {
		for (var i = 0; i < aComment.length; i++) {
			aComment[i].style.display = "";
			onlyAnous = false;
			btnRight.innerText = "点击显示匿名";
		}
	}else{
		hiddenAnous(aComment);
		onlyAnous = true;
		btnRight.innerText = "点击显示全部";

	}
}


function hiddenAnous(arr){
	for (var i = 0; i < arr.length; i++) {
		var auth = arr[i].querySelector("div.authi").innerText.split("发表于")[0].trim();
		if (auth != "匿名青花鱼") {
			arr[i].style.display = "none"
		}
	}
}


})();