Wrap spoilers in Codeforces tutorial in spoiler blocks
当前为
// ==UserScript==
// @name Codeforces_spoiler_tutorial
// @version 0.0.0
// @description Wrap spoilers in Codeforces tutorial in spoiler blocks
// @match https://codeforces.com/blog/entry/*
// @match http://codeforces.com/blog/entry/*
// @run-at document-start
// @namespace https://greasyfork.org/users/410786
// ==/UserScript==
function wrappedInSpoiler(node){
while(node!==null){
if(node.classList && node.classList.contains("spoiler")) return true;
node=node.parentNode;
}
return false;
}
function wrapInSpoiler(nodes, spoilerTitle){
if(!Array.isArray(nodes)) nodes=[nodes]
if(nodes.length==0) return
const spoilerElement=document.createElement("div")
spoilerElement.classList.add("spoiler")
spoilerElement.innerHTML='<b class="spoiler-title"></b><div class="spoiler-content" style="display: none;"></div>'
spoilerElement.firstElementChild.innerText=spoilerTitle
nodes[0].insertAdjacentElement('beforebegin', spoilerElement);
nodes.forEach(function(node){
spoilerElement.lastElementChild.appendChild(node) // "If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position"
})
}
new MutationObserver(function(mutationList, observer){
mutationList.forEach(function(mutation){
mutation.addedNodes.forEach(function(node){
if(node.classList!==undefined && node.classList.contains("problemTutorial")){
if(!wrappedInSpoiler(node)){
var spoilerTitle='Tutorial'
const problemcode=node.getAttribute("problemcode")
if(problemcode) spoilerTitle+=' - '+problemcode
wrapInSpoiler(node, spoilerTitle)
}
}
})
//observer.disconnect()
//console.log(mutation)
})
}).observe(document, { childList: true, subtree: true });