userscripts/fediverse/misskey_cw_toggle.user.js

82 lines
2.4 KiB
JavaScript

// ==UserScript==
// @name Misskey CW toggle
// @description Adds a button to toggle the visibility of all notes with content warnings on note-pages.
// @version 2023.1.26.2
// @author tastytea
// @copyright 2022-2023, tastytea (https://tastytea.de/)
// @license GPL-3.0-only
// @namespace tastytea.de
// @homepageURL https://schlomp.space/tastytea/userscripts
// @supportURL https://schlomp.space/tastytea/userscripts/issues
// @downloadURL https://schlomp.space/tastytea/userscripts/raw/branch/main/fediverse/misskey_cw_toggle.user.js
// @grant none
// @match https://*/notes/*
// @run-at document-end
// @inject-into content
// ==/UserScript==
let interval;
let counter = 0;
function get_root() {
const root = document.getElementsByClassName("lxwezrsl note")[0]; // MK 13
if (root === undefined) {
root = document.getElementsByClassName("_block note")[0]; // MK 12
}
return root;
}
// Toggle the visibility of each status with CW.
function toggle() {
const root = get_root()
if (root === undefined) {
return;
}
const cw_classes = ["cw", "xpfPt"];
for (let classname of cw_classes) {
for (let status of root.getElementsByClassName(classname)) {
let button = status.getElementsByTagName("button")[0];
if (button === undefined) {
continue;
}
button.click();
}
}
}
// Add a “Toggle all CWs”-button.
function add_button() {
const root = get_root()
if (root === undefined) {
console.error("No suitable parent-element found.");
return;
}
const button = document.createElement("button");
button.setAttribute("id", "global-cw-toggle");
button.setAttribute("class", "_button");
button.setAttribute("style", "margin-top: 0.2em; margin-right: 0.5em;");
button.appendChild(document.createTextNode("Toggle all CWs"));
root.insertBefore(button, root.firstChild);
button.onclick = toggle;
}
function check() {
const root = get_root()
if (root === undefined) {
return;
}
// If there is a “Show content” button, add our button, if we didn't do so before.
if (root.getElementsByClassName("cw").length > 0
&& document.getElementById("global-cw-toggle") === null) {
add_button();
console.debug("Global CW toggle button added");
}
}
interval = setInterval(check, 1000); // Try to add button every second.