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: tastytea
GPG Key ID: CFC39497F1B26E07

View File

@ -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);
}
}
}