本帖最后由 lccccccc 于 2024-2-24 08:39 编辑
[JavaScript] 纯文本查看 复制代码 // ==UserScript==
// @name 52_评分可视化
// @version 2024-02-19
// @description 吾爱破解论坛评分可视化 By: lccccccc @52pojie.cn
// @author lccccccc @52pojie.cn
// @match http*://www.52pojie.cn*
// @icon https://down.52pojie.cn/Logo/%E5%90%BE%E7%88%B1%E7%A0%B4%E8%A7%A3.ico
// @grant none
// ==/UserScript==
var CSS_TEXT = `
div._52view-background_div{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 300;
background-color: #00000050;
}
div._52view-background_div>div.tipsBoxWait{
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
/*padding: 5px;*/
background-color: #fff;
border: 3px #0ff solid;
width: 80%;
max-height: 80%;
overflow: auto;
}
table._52view_table {
/*table-layout: fixed;*/
overflow: auto;
height: 100%; /* 设置固定高度 */
width: 100%;
}
table._52view_table>thead tr th {
position:sticky;
top:0;
}
table._52view_table>*>tr>td{
border: 1px #000 solid;
padding: 5px 2px;
text-align: center;
}
table._52view_table>thead>tr {
position: sticky;
top: 0; /* 首行永远固定在头部 */
background-color: #eaf4ff; /*设置表头背景色*/
}
p._52view_close {
z-index: 302;
position: absolute;
top: 5px;
right: 5px;
}
`;
function _52view_cancel(del_eve = 0) {
try{
document.getElementById("_52view_background_div").remove();
if(del_eve==1){
document.removeEventListener('keydown', KeyEvent);
}
} catch {}
}
function KeyEvent(e) {
if (e.keyCode == 27) {
_52view_cancel(1);
}
}
function isMouseInsideElement(event, element) {
try{
var rect = element.getBoundingClientRect();
return (
event.clientX >= rect.left &&
event.clientX <= rect.right &&
event.clientY >= rect.top &&
event.clientY <= rect.bottom
);
} catch {}
}
function _52view_(link) {
var background_div = document.createElement('div');
background_div.id = "_52view_background_div";
background_div.classList.add("_52view-background_div");
var tipsBoxWait = document.createElement('div');
tipsBoxWait.innerHTML = "<p><img src=\"https://static.52pojie.cn/static/image/common/loading.gif\" class=\"vm\" style=\"margin: 5px;\">请稍后 <span style=\"color: #f00;\">[<a href=\"javascript:;\" style=\"text-align: right; color: #f00;\" id=\"_52view_cancel\">取消</a>]</sapn></p>";
tipsBoxWait.classList.add("tipsBoxWait");
background_div.appendChild(tipsBoxWait);
document.body.appendChild(background_div);
document.getElementById('_52view_cancel').addEventListener('click', () => { _52view_cancel() });
var xhr = new XMLHttpRequest();
xhr.open('GET', link + '&infloat=yes&handlekey=viewratings&inajax=1&ajaxtarget=fwin_content_viewratings');
xhr.onload = function() {
if (xhr.status === 200) {
var parser0 = new DOMParser();
var doc0 = parser0.parseFromString(xhr.responseText, 'text/html');
var elements = doc0.getElementsByTagName('root');
// 按理来说只有一个root
var elementValue = elements[0].innerHTML;
var parser = new DOMParser();
var doc = parser.parseFromString(elementValue, 'text/html');
var tr = doc.querySelectorAll('table>tbody>tr');
var scores = {};
var items = new Set();
var users = new Set();
for (var i = 0; i < tr.length; i++) {
var td = tr[i].querySelectorAll('td');
var uname = td[1].querySelectorAll('a')[0].textContent;
items.add(td[0].textContent.split(/[-+]/)[0].trim())
users.add(uname);
if (!scores[uname]) {
scores[uname] = [[td[0].textContent],
td[1].querySelectorAll('a')[0].href,
td[2].textContent,
td[3].textContent]
} else {
scores[uname][0].push(td[0].textContent);
}
}
items = [...items];
users = [...users];
var table = document.createElement('table'), thead, tbody, theadTr, thead2;
table.style.cssText = "border: 1px #000 solid;";
table.classList.add('_52view_table');
table.appendChild(thead = document.createElement('thead'));
table.appendChild(tbody = document.createElement('tbody'));
thead.appendChild(theadTr = document.createElement('tr'));
var createChild = (tag, text) => {
var ele = document.createElement(tag);
ele.innerText = text;
return ele;
}
var createChildHTML = (tag, html) => {
var ele = document.createElement(tag);
ele.innerHTML = html;
return ele;
}
theadTr.appendChild(createChild('td', `用户 (${users.length})`));
var theadTr_Items_Nums = [], Sums = [];
for (var x = 0; x < items.length; x++) {
theadTr_Items_Nums.push(0);
}
for (var x2 = 0; x2 < users.length; x2++) {
Sums.push(0);
}
for (var i0 = 0; i0 < users.length; i0++) {
var tr0 = document.createElement('tr');
tr0.appendChild(createChildHTML('td', `<a href="${scores[users[i0]][1]}">${users[i0]}</a>`));
for (var j = 0; j < items.length; j++) {
var flag = false;
for (var k = 0; k < scores[users[i0]][0].length; k++) {
if (scores[users[i0]][0][k].match(items[j])) {
var score = scores[users[i0]][0][k].replace(items[j], '').trim().split(' ')[0].trim();
theadTr_Items_Nums[tr0.childNodes.length - 1] += parseInt(score);
tr0.appendChild(createChildHTML('td', `<p style="color: ${score > 0 ? '#f00' : '#888'}">${score}</p>`));
flag = true;
break;
}
}
if (!flag) {
tr0.appendChild(createChild('td', ''));
}
}
tr0.appendChild(createChild('td', scores[users[i0]][3]));
tr0.appendChild(createChild('td', scores[users[i0]][2]));
tbody.appendChild(tr0);
scores[users[i0]];
}
for (var z = 0; z < items.length; z++) {
theadTr.appendChild(createChildHTML('td', `${items[z]} <span style="color: ${theadTr_Items_Nums[z] > 0 ? '#f00' : '#888'};">${theadTr_Items_Nums[z] > 0 ? '+' : ''}${theadTr_Items_Nums[z]}</span>`));
}
theadTr.appendChild(createChild('td', '理由'));
var lastTdInTheadTr = createChild('td', '时间');
theadTr.appendChild(lastTdInTheadTr);
document.querySelectorAll("div._52view-background_div>div.tipsBoxWait>p")[0].remove()
var cancel = document.createElement('a');
cancel.innerText = '[X]';
cancel.href = 'javascript:;';
cancel.title = "关闭对话框";
cancel.style.cssText = "position: absolute; right: 5px; color: #f00;";
cancel.addEventListener('click', () => { _52view_cancel() });
lastTdInTheadTr.appendChild(cancel);
document.querySelectorAll("div._52view-background_div>div.tipsBoxWait")[0].appendChild(table);
document.addEventListener('keydown', KeyEvent);
document.querySelectorAll("div._52view-background_div")[0].addEventListener("click", (e) => {
if (!isMouseInsideElement(e, document.querySelectorAll("div._52view-background_div>div.tipsBoxWait")[0])) {
_52view_cancel();
}
});
}
else {
alert('油猴脚本 52_评分可视化: 出错了 (请求错误)');
}
}
xhr.onerror = function() {
background_div.remove();
alert('油猴脚本 52_评分可视化: 出错了 (处理错误 或 网络异常)');
}
xhr.send();
}
(function () {
'use strict';
var pageUrl = window.location.href;
if (pageUrl.match('52pojie.cn') != null) {
var style = document.createElement('style');
style.innerText = CSS_TEXT;
document.head.appendChild(style);
var all = document.querySelectorAll('p.ratc');
for (var i = 0; i < all.length; i++) {
const link = all[i].querySelectorAll('a')[0].href;
var new_a = document.createElement('a');
new_a.href = 'javascript:;';
new_a.innerText = '可视化所有评分';
new_a.classList.add('xi2');
new_a.addEventListener('click', () => {
_52view_(link);
})
all[i].appendChild(new_a);
}
}
})();
这次更新还把一些建议加上了:
- Esc关闭对话框
- 点击对话框以外的空白区域关闭对话框
我找了个论坛的帖子测试了一下,没问题,一切正常:
测试图片
好了,就是这些了。这些源代码仅供学习和交流,请勿他用。
|