From 9b0164ac64c7bda04d89b6f5d7da432eefceabde Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 7 Jun 2019 01:04:55 +0200 Subject: [PATCH] Only add button if one or more statuses have a CW. --- fediverse/pleroma_cw_toggle.user.js | 35 ++++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/fediverse/pleroma_cw_toggle.user.js b/fediverse/pleroma_cw_toggle.user.js index eef3792..2286aae 100644 --- a/fediverse/pleroma_cw_toggle.user.js +++ b/fediverse/pleroma_cw_toggle.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Pleroma CW toggle // @description Adds a button to toggle the visibility of all statuses with content warnings on status-pages and profile-pages. -// @version 2019.06.07.1 +// @version 2019.06.07.2 // @author tastytea // @copyright 2019, tastytea (https://tastytea.de/) // @license GPL-3.0-only @@ -21,10 +21,11 @@ let interval; // Toggle the visibility of statuses with CW. function toggle() { - let hyperlinks = document.getElementsByClassName("cw-status-hider"); + const main = document.getElementsByClassName("main")[0]; + let hyperlinks = main.getElementsByClassName("cw-status-hider"); if (hyperlinks.length === 0) // If no status is hidden, hide all. { - hyperlinks = document.getElementsByClassName("status-unhider"); + hyperlinks = main.getElementsByClassName("status-unhider"); } for (let hyperlink of hyperlinks) @@ -35,24 +36,32 @@ function toggle() function add_button() { + const main = document.getElementsByClassName("main")[0]; + // If conversation-heading is not there, try profile-tabs. - let root = document.getElementsByClassName("conversation-heading")[0]; + let root = main.getElementsByClassName("conversation-heading")[0]; if (root === undefined) { - root = document.getElementsByClassName("tabs")[0]; + root = main.getElementsByClassName("tabs")[0]; } - // if root element was found, disable interval and add button. - if (root !== undefined) + // if root element and a status was found, disable interval and add button. + if (root !== undefined + && main.getElementsByClassName("status-content").length > 0) { clearInterval(interval); - const button = document.createElement("a"); - button.setAttribute("style", "margin-left: 1em;"); - button.appendChild(document.createTextNode("Toggle all CWs")); - button.setAttribute("href", "#"); - button.onclick = toggle; - root.append(button); + // Only add button if one or more statuses have a CW. + if (main.getElementsByClassName("cw-status-hider").length > 0 + || main.getElementsByClassName("status-unhider").length > 0) + { + const button = document.createElement("a"); + button.setAttribute("style", "margin-left: 1em;"); + button.appendChild(document.createTextNode("Toggle all CWs")); + button.setAttribute("href", "#"); + button.onclick = toggle; + root.append(button); + } } }