userscripts/fediverse/misskey_cw_toggle.user.js

71 lines
2.2 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 2022.07.13.1
// @author tastytea
// @copyright 2022, 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;
// Toggle the visibility of each status with CW.
function toggle() {
const root = document.getElementsByClassName("_block note")[0];
if (root === undefined) {
return;
}
for (let status of root.getElementsByClassName("cw")) {
let button = status.getElementsByTagName("button")[0];
if (button === undefined) {
continue;
}
button.click();
}
}
// Add a “Toggle all CWs”-button.
function add_button() {
let root = document.getElementsByClassName("_block note")[0];
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 = document.getElementsByClassName("_block note")[0];
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.