Only add button if one or more statuses have a CW.

This commit is contained in:
tastytea 2019-06-07 01:04:55 +02:00
parent 1e50d557c1
commit 9b0164ac64
Signed by untrusted user: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 22 additions and 13 deletions

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name Pleroma CW toggle // @name Pleroma CW toggle
// @description Adds a button to toggle the visibility of all statuses with content warnings on status-pages and profile-pages. // @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 // @author tastytea
// @copyright 2019, tastytea (https://tastytea.de/) // @copyright 2019, tastytea (https://tastytea.de/)
// @license GPL-3.0-only // @license GPL-3.0-only
@ -21,10 +21,11 @@ let interval;
// Toggle the visibility of statuses with CW. // Toggle the visibility of statuses with CW.
function toggle() 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. 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) for (let hyperlink of hyperlinks)
@ -35,24 +36,32 @@ function toggle()
function add_button() function add_button()
{ {
const main = document.getElementsByClassName("main")[0];
// If conversation-heading is not there, try profile-tabs. // 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) 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 element and a status was found, disable interval and add button.
if (root !== undefined) if (root !== undefined
&& main.getElementsByClassName("status-content").length > 0)
{ {
clearInterval(interval); clearInterval(interval);
const button = document.createElement("a"); // Only add button if one or more statuses have a CW.
button.setAttribute("style", "margin-left: 1em;"); if (main.getElementsByClassName("cw-status-hider").length > 0
button.appendChild(document.createTextNode("Toggle all CWs")); || main.getElementsByClassName("status-unhider").length > 0)
button.setAttribute("href", "#"); {
button.onclick = toggle; const button = document.createElement("a");
root.append(button); button.setAttribute("style", "margin-left: 1em;");
button.appendChild(document.createTextNode("Toggle all CWs"));
button.setAttribute("href", "#");
button.onclick = toggle;
root.append(button);
}
} }
} }