pleroma_cw_toggle: Fixed bug I introduced with interactions-support.

This commit is contained in:
tastytea 2019-06-25 00:32:42 +02:00
parent 67811cb59b
commit c82e31b6f9
Signed by untrusted user: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 21 additions and 10 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, profile-pages and timelines. // @description Adds a button to toggle the visibility of all statuses with content warnings on status-pages, profile-pages and timelines.
// @version 2019.06.24.1 // @version 2019.06.25.1
// @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
@ -23,13 +23,12 @@ let interval;
let counter = 0; let counter = 0;
// Toggle the visibility of statuses with CW. // Toggle the visibility of statuses with CW.
function toggle() function toggle(parent)
{ {
const main = document.getElementsByClassName("main")[0]; let hyperlinks = parent.getElementsByClassName("cw-status-hider");
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 = main.getElementsByClassName("status-unhider"); hyperlinks = parent.getElementsByClassName("status-unhider");
} }
for (let hyperlink of hyperlinks) for (let hyperlink of hyperlinks)
@ -59,7 +58,6 @@ function add_button(parent)
button.setAttribute( button.setAttribute(
"style", "margin-left: 1em; margin-right: 0.5em; cursor: pointer;"); "style", "margin-left: 1em; margin-right: 0.5em; cursor: pointer;");
button.appendChild(document.createTextNode("Toggle all CWs")); button.appendChild(document.createTextNode("Toggle all CWs"));
button.addEventListener('click', toggle);
span.append(button); span.append(button);
const otherspans = parent.getElementsByTagName("span"); const otherspans = parent.getElementsByTagName("span");
@ -71,6 +69,7 @@ function add_button(parent)
{ {
parent.append(span); parent.append(span);
} }
return button;
} }
// Check if we need to add a button. // Check if we need to add a button.
@ -98,9 +97,18 @@ function check()
} }
const root = get_root_elements(main); const root = get_root_elements(main);
let parent;
if (RegExp("/main/(friends|public|all)").test(window.location.href))
{
parent = root[0].parentElement;
}
else
{
parent = main;
}
// if root element and a status was found, disable interval and add button. // if root element and a status was found, disable interval and add button.
if (root.length !== 0 if (root.length !== 0
&& main.getElementsByClassName("status-content").length > 0) && parent.getElementsByClassName("status-content").length > 0)
{ {
if (!is_timeline) if (!is_timeline)
{ {
@ -110,8 +118,8 @@ function check()
for (let element of root) for (let element of root)
{ {
// Only add button if one or more statuses have a CW. // Only add button if one or more statuses have a CW.
if (main.getElementsByClassName("cw-status-hider").length > 0 if (parent.getElementsByClassName("cw-status-hider").length > 0
|| main.getElementsByClassName("status-unhider").length > 0) || parent.getElementsByClassName("status-unhider").length > 0)
{ {
if (element.getElementsByClassName("global-cw-toggle") if (element.getElementsByClassName("global-cw-toggle")
.length > 0) // Skip if button is already there. .length > 0) // Skip if button is already there.
@ -119,7 +127,10 @@ function check()
continue; continue;
} }
add_button(element); // jshint -W083
add_button(element).addEventListener('click', function()
{ toggle(parent); });
// jshint +W083
} }
} }
} }