forked from tastytea/userscripts
pleroma_cw_toggle: Made it work on timelines too.
This commit is contained in:
parent
504c9c8bf4
commit
02f78bc3cc
|
@ -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, profile-pages and timelines.
|
||||||
// @version 2019.06.07.4
|
// @version 2019.06.12.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
|
||||||
|
@ -12,6 +12,9 @@
|
||||||
// @grant none
|
// @grant none
|
||||||
// @match https://*/notice/*
|
// @match https://*/notice/*
|
||||||
// @match https://*/users/*
|
// @match https://*/users/*
|
||||||
|
// @match https://*/main/friends
|
||||||
|
// @match https://*/main/public
|
||||||
|
// @match https://*/main/all
|
||||||
// @run-at document-end
|
// @run-at document-end
|
||||||
// @inject-into content
|
// @inject-into content
|
||||||
// ==/UserScript==
|
// ==/UserScript==
|
||||||
|
@ -20,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)
|
||||||
|
@ -35,42 +37,75 @@ function toggle()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function add_button()
|
function get_root_elements()
|
||||||
{
|
{
|
||||||
++counter;
|
|
||||||
// If this is not Pleroma or we tried 10 times, disable interval.
|
|
||||||
if (counter > 10 || document.getElementById("app") === null)
|
|
||||||
{
|
|
||||||
clearInterval(interval);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const main = document.getElementsByClassName("main")[0];
|
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 = main.getElementsByClassName("conversation-heading")[0];
|
let root = main.getElementsByClassName("conversation-heading");
|
||||||
if (root === undefined)
|
if (root.length === 0)
|
||||||
{
|
{
|
||||||
root = main.getElementsByClassName("tabs")[0];
|
root = main.getElementsByClassName("tabs");
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
function add_button(parent)
|
||||||
|
{
|
||||||
|
const button = document.createElement("a");
|
||||||
|
button.setAttribute("class", "global-cw-toggle");
|
||||||
|
button.setAttribute("style", "margin-left: 1em; cursor: pointer;");
|
||||||
|
button.appendChild(document.createTextNode("Toggle all CWs"));
|
||||||
|
// button.setAttribute("href", "#");
|
||||||
|
button.addEventListener('click', function()
|
||||||
|
{ toggle(parent.parentElement); });
|
||||||
|
parent.append(button);
|
||||||
|
}
|
||||||
|
|
||||||
|
function check()
|
||||||
|
{
|
||||||
|
const re = new RegExp(
|
||||||
|
'(/main/(friends|public|all)|/users/[^/]+/(mentions|dms))#?$');
|
||||||
|
const is_timeline = re.test(window.location.href);
|
||||||
|
|
||||||
|
if (!is_timeline) // If we are on a timeline, don't stop checking.
|
||||||
|
{
|
||||||
|
++counter;
|
||||||
|
// If this is not Pleroma or we tried 10 times, disable interval.
|
||||||
|
if (counter > 10 || document.getElementById("app") === null)
|
||||||
|
{
|
||||||
|
clearInterval(interval);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const main = document.getElementsByClassName("main")[0];
|
||||||
|
const root = get_root_elements();
|
||||||
// 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 !== undefined
|
if (root.length !== 0
|
||||||
&& main.getElementsByClassName("status-content").length > 0)
|
&& main.getElementsByClassName("status-content").length > 0)
|
||||||
{
|
{
|
||||||
clearInterval(interval);
|
if (!is_timeline)
|
||||||
|
|
||||||
// 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");
|
clearInterval(interval);
|
||||||
button.setAttribute("style", "margin-left: 1em;");
|
}
|
||||||
button.appendChild(document.createTextNode("Toggle all CWs"));
|
|
||||||
button.setAttribute("href", "#");
|
for (let element of root)
|
||||||
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)
|
||||||
|
{
|
||||||
|
if (element.getElementsByClassName("global-cw-toggle")
|
||||||
|
.length > 0) // Skip if button is already there.
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_button(element);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interval = setInterval(add_button, 1000); // Try to add button every second.
|
interval = setInterval(check, 1000); // Try to add button every second.
|
||||||
|
|
Loading…
Reference in New Issue