吾爱代码块支持

吾爱代码块隐藏/显示切换+md复制按钮支持

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         吾爱代码块支持
// @namespace    wuai_copy
// @version      0.1.0
// @description  吾爱代码块隐藏/显示切换+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
// ==/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, '代码已复制到剪贴板');
			};
		}
	});
}
})();