Last active
December 14, 2020 18:00
-
-
Save FelixWolf/c5a0f95e7c1e84ac73240fba9fe21001 to your computer and use it in GitHub Desktop.
Second Life Forums thread blocker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Thread blocker | |
// @namespace http://felix.softhyena.com/ | |
// @version 0.1 | |
// @description Block threads on the Second Life forums | |
// @author Chaser Zaks | |
// @match https://community.secondlife.com/forums/forum/* | |
// @grant none | |
// ==/UserScript== | |
(function(){ | |
let showBlocked = Boolean(parseInt(localStorage.getItem("showBlocked") || 0)); | |
document.body.dataset.showBlockedThreads = showBlocked; | |
//Create the stylesheet | |
const style = document.createElement("style"); | |
style.textContent = ` | |
body[data-show-blocked-threads="false"] ol.cTopicList > li[data-blocked="true"] { | |
display: none; | |
}; | |
`; | |
document.head.append(style); | |
//Create the toggle button | |
const ipsBreadcrumb = document.querySelector("nav.ipsBreadcrumb > ul"), | |
toggleLi = document.createElement("li"), | |
toggleA = document.createElement("a"), | |
toggleI = document.createElement("i"), | |
toggleSpan = document.createElement("span"); | |
//Append children | |
toggleLi.appendChild(toggleA); | |
toggleA.appendChild(toggleI); | |
toggleA.appendChild(toggleSpan); | |
//Setup the anchor | |
toggleA.setAttribute("class", ipsBreadcrumb.firstElementChild.firstElementChild.getAttribute("class")); | |
toggleA.setAttribute("href", "#"); | |
//Setup the icon | |
toggleI.setAttribute("aria-hidden", "true"); | |
toggleI.setAttribute("class", "fa fa-shield"); | |
//Setup the text | |
toggleSpan.textContent = (showBlocked ? "Hide" : "Show") + " blocked threads"; | |
ipsBreadcrumb.insertBefore(toggleLi, ipsBreadcrumb.firstElementChild); | |
toggleA.addEventListener("click", e=>{ | |
e.preventDefault(); | |
showBlocked = !showBlocked; | |
localStorage.setItem("showBlocked", Number(showBlocked).toString()); | |
document.body.dataset.showBlockedThreads = showBlocked; | |
toggleSpan.textContent = (showBlocked ? "Hide" : "Show") + " blocked threads"; | |
}); | |
//Process threads | |
const threadTable = document.querySelector("ol.cTopicList"); | |
if(!threadTable) return; //Escape early if we can't find a valid topic list | |
let blockedThreads = JSON.parse(localStorage.getItem("blockedThreads")) || []; | |
for(let thread of threadTable.children){ | |
let blocked = blockedThreads.indexOf(parseInt(thread.dataset.rowid)) != -1; | |
thread.dataset.blocked = blocked; | |
//Add button to thread | |
let buttonLocation = thread.querySelector("div.ipsDataItem_main > div.ipsDataItem_meta"), | |
blockA = document.createElement("a"), | |
blockI = document.createElement("i"), | |
blockSpan = document.createElement("span"); | |
blockA.appendChild(blockI); | |
blockA.appendChild(blockSpan); | |
blockA.setAttribute("href", "#"); | |
blockI.setAttribute("aria-hidden", "true"); | |
blockI.setAttribute("class", "fa fa-shield"); | |
blockI.innerHTML = " "; | |
blockSpan.textContent = (blocked ? "Unblock" : "Block") + " thread"; | |
//Add handler for button | |
blockA.addEventListener("click", e=>{ | |
e.preventDefault(); | |
let blockedThreads = JSON.parse(localStorage.getItem("blockedThreads")) || []; | |
//Resolve thread | |
let thread = e.target; | |
if(thread.tagName != "A") thread = thread.parentElement; | |
let blockSpan = thread.querySelector("span"); | |
thread = thread.parentElement.parentElement.parentElement; | |
let tid = parseInt(thread.dataset.rowid); | |
//Handle blocking | |
let blocked = blockedThreads.indexOf(tid) != -1; | |
if(blocked){ //Blocked, so unblock it | |
blockedThreads = blockedThreads.filter(item => item !== tid); | |
}else{ //Not blocked, so block it. | |
blockedThreads.push(tid); | |
} | |
blocked = !blocked; | |
//Update and save | |
thread.dataset.blocked = blocked; | |
blockSpan.textContent = (blocked ? "Unblock" : "Block") + " thread"; | |
localStorage.setItem("blockedThreads", JSON.stringify(blockedThreads)); | |
}); | |
buttonLocation.appendChild(blockA); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment