From e9b803c50cbaaf304b82744934e204818134cfa8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Tue, 17 May 2022 01:11:42 +0200 Subject: [PATCH] Misskey CW toggle: reduce scope, fix CSS somewhat Button positioning isn't great, but not awful anymore. --- fediverse/misskey_cw_toggle.user.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/fediverse/misskey_cw_toggle.user.js b/fediverse/misskey_cw_toggle.user.js index fece9cc..bef0b28 100644 --- a/fediverse/misskey_cw_toggle.user.js +++ b/fediverse/misskey_cw_toggle.user.js @@ -1,7 +1,7 @@ // ==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 +// @version 2022.05.17.2 // @author tastytea // @copyright 2022, tastytea (https://tastytea.de/) // @license GPL-3.0-only @@ -20,7 +20,11 @@ let counter = 0; // Toggle the visibility of each status with CW. function toggle() { - for (let status of document.getElementsByClassName("cw")) { + const root = document.getElementsByClassName("_block _isolated note")[0]; + if (root === undefined) { + return; + } + for (let status of root.getElementsByClassName("cw")) { let button = status.getElementsByTagName("button")[0]; if (button === undefined) { continue; @@ -32,7 +36,7 @@ function toggle() { // 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]; + const root = document.getElementsByClassName("_block _isolated note")[0]; if (root === undefined) { console.error("No suitable parent-element found."); return; @@ -42,7 +46,7 @@ function add_button() { 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.setAttribute("style", "margin-top: 0.2em; margin-right: 0.5em;"); button.appendChild(document.createTextNode("Toggle all CWs")); root.insertBefore(button, root.firstChild); @@ -51,9 +55,14 @@ function add_button() { } function check() { + const root = document.getElementsByClassName("_block _isolated note")[0]; + if (root === undefined) { + return; + } + // 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) { + if (root.getElementsByClassName("cw").length > 0 + && root.getElementById("global-cw-toggle") === null) { add_button(); console.debug("Global CW toggle button added"); }