【伟哥自用】某堂fix「复制代码」
// ==UserScript==
// @name 【伟哥自用】某堂fix「复制代码」
// @namespace http://tampermonkey.net/
// @version 0.1
// @description fix 'copycode' button
// @author 朱焱伟
// @match https://sehuatang.org/forum.php?mod=viewthread&tid=*
// @match https://sehuatang.org/thread*
// @icon https://www.google.com/s2/favicons?sz=64&domain=sehuatang.org
// @grant unsafeWindow
// @grant GM_registerMenuCommand
// @grant GM_setClipboard
// ==/UserScript==
(function() {
'use strict';
let isBlockcodeExist = ()=>{
return document.querySelector(".blockcode>em")?true:false;
}
function getBlockcodeContent (){
if(!isBlockcodeExist())
return "";
let liElemArr = [...document.querySelector(".blockcode>div>ol")?.querySelectorAll("li")];
/* https://stackoverflow.com/questions/7459704/in-javascript-what-is-the-best-way-to-convert-a-nodelist-to-an-array */
let liContentArr = liElemArr.map(function(li){
return li.textContent;
})
return liContentArr.join("\n");
}
function addCopycodeMenu(){/* add GM menu */
if(isBlockcodeExist()){
let id = GM_registerMenuCommand ("点我「复制代码」", function(){
let to_copy = getBlockcodeContent();
GM_setClipboard(to_copy);
});
}
}
function fixCopycode(){/* rewrite 「copycode」 button's onlick func*/
if(isBlockcodeExist()){
document.querySelector(".blockcode>em").onclick = ()=>{
if (!window.__cfRLUnblockHandlers) return false;
GM_setClipboard(getBlockcodeContent());
}
document.querySelector(".blockcode>em").textContent="伟哥の复制代码"
}
}
fixCopycode();
addCopycodeMenu();
// Your code here...
})();
/**
* There is something wrong with 98tang's 'copycode' button, this script is to fix it.
* The 'copycode' button does not exist in most pages, but if it does, this script will override its onlick event,
*/