From 6a2eadc05af8d0cee440a0fd7a1bf136f34cc61c Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 17 May 2022 00:15:20 +0200 Subject: [PATCH] Add Misskey CW toggle --- fediverse/misskey_cw_toggle.user.js | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 fediverse/misskey_cw_toggle.user.js diff --git a/fediverse/misskey_cw_toggle.user.js b/fediverse/misskey_cw_toggle.user.js new file mode 100644 index 0000000..fece9cc --- /dev/null +++ b/fediverse/misskey_cw_toggle.user.js @@ -0,0 +1,62 @@ +// ==UserScript== +// @name Misskey CW toggle +// @description Adds a button to toggle the visibility of all notes with content warnings on note-pages. +// @version 2022.05.17.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://*/notice/* +// @run-at document-end +// @inject-into content +// ==/UserScript== + +let interval; +let counter = 0; + +// Toggle the visibility of each status with CW. +function toggle() { + for (let status of document.getElementsByClassName("cw")) { + let button = status.getElementsByTagName("button")[0]; + if (button === undefined) { + continue; + } + button.click(); + } +} + +// Add a “Toggle all CWs”-button. +function add_button() { + // If there is no element named “column-1”, use the footer. + let root = document.getElementsByClassName("_block _isolated 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", "float: right; margin: 0.2em 0.5em;"); + button.appendChild(document.createTextNode("Toggle all CWs")); + + root.insertBefore(button, root.firstChild); + + button.onclick = toggle; +} + +function check() { + // If there is a “Show content” button, add our button, if we didn't do so before. + if (document.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.