第一个是合并了代码块折叠功能和隐藏置顶公告信息的代码,如下
// ==UserScript==
// @name 吾爱破解增强
// @namespace 52pojie_enhancements
// @version 1.0.0
// @description 吾爱代码块隐藏/显示切换+md复制按钮支持+网页净化
// @author
// @match https://www.52pojie.cn/*
// @grant none
// @run-at document-end
// @require http://cdn.bootcss.com/jquery/1.8.3/jquery.min.js
// @note 合并了代码块支持与网页净化功能
// @icon https://www.52pojie.cn/favicon.ico
// ==/UserScript==
(function() {
'use strict';
// 代码块功能实现
function setupCodeBlocks() {
// 处理代码块的隐藏/显示和复制功能
function processCodeBlocks() {
// 处理.viewsource元素
document.querySelectorAll(".viewsource").forEach((a) => {
const parent = a.parentElement;
if (parent.lastChild.className === "hideCode" || parent.lastChild.className === "showCode") {
return;
}
const toggleBtn = createToggleButton();
parent.appendChild(toggleBtn);
});
// 处理pre元素
document.querySelectorAll("pre").forEach((pre) => {
if (pre.firstChild.className === "hideCode" || pre.firstChild.className === "CopyMyCode" || pre.firstChild.className === "showCode") {
return;
}
const toggleBtn = createToggleButton();
const copyBtn = createCopyButton();
pre.insertBefore(toggleBtn, pre.firstChild);
pre.insertBefore(copyBtn, pre.firstChild);
});
}
// 创建隐藏/显示按钮
function createToggleButton() {
const btn = document.createElement('em');
btn.setAttribute("class", "hideCode");
btn.style = "cursor:pointer;font-size:12px;color:#369 !important;";
btn.innerHTML = " 隐藏代码";
btn.onclick = function() {
const codeContainer = this.className === "hideCode"
? this.parentElement.parentElement.lastChild
: this.parentElement.lastChild;
if (this.className === "hideCode") {
codeContainer.style.height = "0";
codeContainer.style.overflow = "hidden";
this.setAttribute("class", "showCode");
this.innerHTML = " 显示代码";
} else {
codeContainer.style.height = "";
codeContainer.style.overflow = "";
this.setAttribute("class", "hideCode");
this.innerHTML = " 隐藏代码";
}
};
return btn;
}
// 创建复制按钮
function createCopyButton() {
const btn = document.createElement('em');
btn.setAttribute("class", "CopyMyCode");
btn.style = "cursor:pointer;font-size:12px;color:#369 !important;";
btn.innerHTML = " 复制代码";
btn.onclick = function() {
const container = this.parentElement.lastChild;
const lines = container.childNodes;
const code = [];
for (let i = 0; i < lines.length; i++) {
code.push(lines[i].innerText || lines[i].textContent);
}
setCopy(code.join(''), '代码已复制到剪贴板');
};
return btn;
}
// 复制到剪贴板功能
function setCopy(text, message) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
// 显示提示信息
alert(message);
}
// 在滚动时处理代码块
window.onscroll = processCodeBlocks;
// 初始加载时处理一次
processCodeBlocks();
}
// 网页净化功能实现
function setupPageCleanup() {
// 等待元素加载的函数,增加超时处理
function waitForElement(selector, callback, timeout = 5000) {
let timer;
const interval = setInterval(() => {
const element = document.querySelector(selector);
if (element) {
clearInterval(interval);
clearTimeout(timer);
callback(element);
}
}, 100);
timer = setTimeout(() => {
clearInterval(interval);
console.log(`超时未找到元素: ${selector}`);
}, timeout);
}
// 隐藏不需要的元素
function hideElements() {
$("div.bm.bml.pbn, td.plc.plm, div.plc.dnch_eo_pt, div.dnch_eo_pt, dl.rate").hide();
document.querySelectorAll('[id*="stickthread"]').forEach(el => el.style.display = 'none');
}
// 执行净化
hideElements();
}
// 初始化所有功能
function init() {
setupCodeBlocks();
setupPageCleanup();
}
// 页面加载完成后初始化
window.addEventListener('load', init);
})();
下面是原版
// ==UserScript==
// @name 吾爱代码块支持
// @namespace wuai_copy
// @version 0.1.0
// @description 折叠吾爱破解代码块,52pojie代码块隐藏/显示切换+md复制按钮支持
// @author 涛之雨
// @match https://www.52pojie.cn/*
// @grant none
// @note 吾爱代码块隐藏/显示切换+md复制按钮支持
// @icon https://www.52pojie.cn/favicon.ico
// @home https://greasyfork.org/zh-CN/scripts/416512
// @downloadURL https://update.greasyfork.org/scripts/416512/%E5%90%BE%E7%88%B1%E4%BB%A3%E7%A0%81%E5%9D%97%E6%94%AF%E6%8C%81.user.js
// @updateURL https://update.greasyfork.org/scripts/416512/%E5%90%BE%E7%88%B1%E4%BB%A3%E7%A0%81%E5%9D%97%E6%94%AF%E6%8C%81.meta.js
// ==/UserScript==
(function() {
window.onscroll=function(){
document.querySelectorAll(".viewsource").forEach((a)=>{
var b=a.parentElement;
if(b.lastChild.className=="hideCode"||b.lastChild.className=="showCode"){
return;
}
var c = document.createElement('em');
c.setAttribute("class","hideCode");
c.style="cursor:pointer;font-size:12px;color:#369 !important;";
c.innerHTML=" 隐藏代码";
c.onclick=function(){
var a=this;
if(a.className=="hideCode"){
a.parentElement.parentElement.lastChild.style.height="0";
a.parentElement.parentElement.lastChild.style.overflow="hidden";
a.setAttribute("class","showCode");
a.innerHTML=" 显示代码";
}else if(a.className=="showCode"){
a.parentElement.parentElement.lastChild.style.height="";
a.parentElement.parentElement.lastChild.style.overflow=""
a.setAttribute("class","hideCode");
a.innerHTML=" 隐藏代码";
}
};
b.appendChild(c);
});
document.querySelectorAll("pre").forEach((a)=>{
if(a.firstChild.className=="hideCode"||a.firstChild.className=="CopyMyCode"||a.firstChild.className=="showCode"){
return;
}else{
var c = document.createElement('em');
c.setAttribute("class","hideCode");
c.style="cursor:pointer;font-size:12px;color:#369 !important;";
c.innerHTML=" 隐藏代码";
a.insertBefore(c,a.firstChild);
c.onclick=function(){
var a=this;
if(a.className=="hideCode"){
a.parentElement.lastChild.style.height="0";
a.parentElement.lastChild.style.overflow="hidden";
a.setAttribute("class","showCode");
a.innerHTML=" 显示代码";
}else if(a.className=="showCode"){
a.parentElement.lastChild.style.height="";
a.parentElement.lastChild.style.overflow="";
a.setAttribute("class","hideCode");
a.innerHTML=" 隐藏代码";
}
};
c = document.createElement('em');
c.setAttribute("class","CopyMyCode");
c.style="cursor:pointer;font-size:12px;color:#369 !important;";
c.innerHTML=" 复制代码";
a.insertBefore(c,a.firstChild);
c.onclick=function(){
var container = this.parentElement.lastChild;
var lines = container.childNodes;
var code = [];
for (var i = 0; i < lines.length; i++) {
code.push(lines[i].innerText || lines[i].textContent);
}
code = code.join('');
setCopy(code, '代码已复制到剪贴板');
};
}
});
}
})();
