// ==UserScript==
// @name 52 Enhance
// @namespace http://tampermonkey.net/
// @version 0.3.0
// @description Minimalistic enhancements for 52pojie.
// @author PRO
// @match https://www.52pojie.cn/*
// @icon http://52pojie.cn/favicon.ico
// @license gpl-3.0
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @require http://greasyfork.icu/scripts/470224-tampermonkey-config/code/Tampermonkey%20Config.js?version=1228955
// ==/UserScript==
(function() {
'use strict';
let idPrefix = "52-enhance-";
let config_desc = {
"css-fix": { // 动态透明度;图标上光标不显示为 pointer
name: "CSS 修复",
value: true,
input: "current",
processor: "not",
formatter: "boolean",
},
"hide": { // 为旧版代码块添加“隐藏代码”的按钮;一键隐藏所有置顶帖;添加隐藏回复的按钮
name: "*一键隐藏",
value: true,
input: "current",
processor: "not",
formatter: "boolean",
},
"get-to-top": { // 双击导航栏回到顶部;修改回到顶部按钮行为为原生
name: "*回到顶部",
value: true,
input: "current",
processor: "not",
formatter: "boolean",
},
"emoji-fix": { // 修复 Emoji 显示
name: "*修复 Emoji",
value: true,
input: "current",
processor: "not",
formatter: "boolean",
},
"hide-signature": { // 隐藏所有签名档
name: "隐藏签名档",
value: false,
input: "current",
processor: "not",
formatter: "boolean",
},
"hide-warning": { // 隐藏所有提醒
name: "隐藏提醒",
value: false,
input: "current",
processor: "not",
formatter: "boolean",
},
};
let config = GM_config(config_desc, false);
// Css fix
function cssFix(enable) {
let current = document.getElementById(idPrefix + "css-fix");
if (enable && !current) {
let css = document.createElement("style");
css.id = idPrefix + "css-fix";
css.textContent = `
#jz52top {
opacity: 0.2;
transition: opacity 0.2s ease-in-out;
}
#jz52top:hover {
opacity: 0.8;
}
@media (any-hover: none) {
#jz52top {
opacity: 0.8;
}
#jz52top:hover {
opacity: 0.8;
}
}
.authicn {
cursor: initial;
}`;
document.head.appendChild(css);
} else if (!enable && current) {
current.remove();
}
}
cssFix(config["css-fix"]);
// Hide
if (config["hide"]) {
// Hide code
function toggleCode() {
let code = this.parentNode.parentNode.lastChild;
if (code.style.display == "none") {
code.style.display = "";
this.textContent = " 隐藏代码";
} else {
code.style.display = "none";
this.textContent = " 显示代码";
}
}
document.querySelectorAll("em.viewsource").forEach(ele => {
let hide_code = document.createElement("em");
hide_code.setAttribute("style", ele.getAttribute("style"));
hide_code.setAttribute("num", ele.getAttribute("num"));
hide_code.textContent = " 隐藏代码";
hide_code.addEventListener("click", toggleCode);
ele.parentNode.appendChild(hide_code);
});
// Hide all top threads
let display = Boolean(document.querySelectorAll("tbody[id^='stickthread_']").length);
let table = document.getElementById("threadlisttableid");
if (display && table) {
function hideAll() {
document.querySelectorAll("tbody[id^='stickthread_']").forEach(ele => {
let close = ele.querySelector("a.closeprev");
if (close) close.click();
});
}
let tooltip = document.querySelector("div#threadlist > div.th > table > tbody > tr > th > div.tf");
let show_top = tooltip.querySelector("span#clearstickthread");
show_top.removeAttribute("style");
show_top.insertAdjacentHTML("beforeend", " ");
let hide_all = document.createElement("a");
hide_all.href = "javascript:;";
hide_all.className = "xi2";
hide_all.textContent = "隐藏置顶";
hide_all.title = "隐藏置顶";
hide_all.addEventListener("click", hideAll);
show_top.insertAdjacentElement("beforeend", hide_all);
}
// Hide reply
function toggleReplyHeader() {
let reply = this.parentNode.parentNode.parentNode.nextSibling;
if (reply.style.display == "none") {
reply.style.display = "";
} else {
reply.style.display = "none";
}
}
let headers = document.querySelectorAll("div.pti > div.authi");
headers.forEach(header => {
let pipe = document.createElement("span");
pipe.classList.add("pipe");
pipe.textContent = "|";
let toggle = document.createElement("a");
toggle.href = "javascript:void(0);";
toggle.textContent = "隐藏/显示";
toggle.addEventListener("click", toggleReplyHeader);
if (header.querySelector("span.none")) {
pipe.classList.add("show");
toggle.classList.add("show");
}
header.insertAdjacentElement("beforeend", pipe);
header.insertAdjacentElement("beforeend", toggle);
});
function toggleReplyFooter() {
let reply = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild.lastChild.lastElementChild;
if (reply.style.display == "none") {
reply.style.display = "";
} else {
reply.style.display = "none";
}
}
let footers = document.querySelectorAll("td.plc >div.po.hin > div.pob.cl > p");
footers.forEach(footer => {
let toggle = document.createElement("a");
toggle.href = "javascript:void(0);";
toggle.textContent = "隐藏/显示";
toggle.addEventListener("click", toggleReplyFooter);
footer.insertAdjacentElement("beforeend", toggle);
});
}
// Get to top
if (config["get-to-top"]) {
// Double click navbar to get to top
document.getElementById("nv").addEventListener("dblclick", e => {
window.scrollTo({ top: 0, behavior: "smooth" });
});
// Change get to top button behavior (use vanilla solution)
document.getElementById("goTopBtn").onclick = e => {
window.scrollTo({ top: 0, behavior: "smooth" });
};
}
// Emoji fix
if (config["emoji-fix"]) {
let temp = document.createElement("span");
function fixEmoji(html) { // Replace patterns like `👍` with represented emoji
return html.replace(/&(amp;)*#(\d+);/g, (match, p1, p2) => {
temp.innerHTML = `&#${p2};`;
// console.log(`${match} -> ${temp.textContent}`); // DEBUG
return temp.textContent;
});
}
function fix(node) {
// Select text nodes
let walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
let textNode;
while (textNode = walker.nextNode()) {
textNode.nodeValue = fixEmoji(textNode.nodeValue);
}
}
let replies = document.querySelectorAll("table.plhin td.plc div.pct > div.pcb > div.t_fsz");
replies.forEach(fix);
let ratings = document.querySelectorAll("tbody.ratl_l > tr");
ratings.forEach(rating => fix(rating.lastElementChild));
}
// Hide signatures
function hideSignature(enable) {
let current = document.getElementById(idPrefix + "hide-signature");
if (enable && !current) {
let css = document.createElement("style");
css.id = idPrefix + "hide-signature";
css.textContent = "div.sign { display: none; }";
document.head.appendChild(css);
} else if (!enable && current) {
current.remove();
}
}
hideSignature(config["hide-signature"]);
// Hide warnings
function hideWarning(enable) {
let current = document.getElementById(idPrefix + "hide-warning");
if (enable && !current) {
let css = document.createElement("style");
css.id = idPrefix + "hide-warning";
css.textContent = "div[class^=vw50_kfc_p] { display: none; }";
document.head.appendChild(css);
} else if (!enable && current) {
current.remove();
}
}
hideWarning(config["hide-warning"]);
// Listen to config changes
let callbacks = {
"css-fix": cssFix,
"hide-signature": hideSignature,
"hide-warning": hideWarning
};
window.addEventListener(GM_config_event, e => {
if (e.detail.type == "set") {
let callback = callbacks[e.detail.prop];
if (callback && (e.detail.before != e.detail.after)) {
callback(e.detail.after);
}
}
});
})();