2019-06-07 00:22:03 +02:00
|
|
|
// ==UserScript==
|
|
|
|
// @name Pleroma CW toggle
|
2019-06-12 21:04:15 +02:00
|
|
|
// @description Adds a button to toggle the visibility of all statuses with content warnings on status-pages, profile-pages and timelines.
|
2019-06-13 20:54:39 +02:00
|
|
|
// @version 2019.06.13.1
|
2019-06-07 00:22:03 +02:00
|
|
|
// @author tastytea
|
|
|
|
// @copyright 2019, tastytea (https://tastytea.de/)
|
|
|
|
// @license GPL-3.0-only
|
|
|
|
// @namespace tastytea.de
|
|
|
|
// @homepageURL https://schlomp.space/tastytea/userscripts
|
|
|
|
// @supportURL https://schlomp.space/tastytea/userscripts/issues
|
|
|
|
// @downloadURL https://schlomp.space/tastytea/userscripts/raw/branch/main/fediverse/pleroma_cw_toggle.user.js
|
|
|
|
// @grant none
|
|
|
|
// @match https://*/notice/*
|
|
|
|
// @match https://*/users/*
|
2019-06-12 21:04:15 +02:00
|
|
|
// @match https://*/main/friends
|
|
|
|
// @match https://*/main/public
|
|
|
|
// @match https://*/main/all
|
2019-06-07 00:22:03 +02:00
|
|
|
// @run-at document-end
|
|
|
|
// @inject-into content
|
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
let interval;
|
2019-06-07 01:23:25 +02:00
|
|
|
let counter = 0;
|
2019-06-07 00:22:03 +02:00
|
|
|
|
|
|
|
// Toggle the visibility of statuses with CW.
|
2019-06-12 21:04:15 +02:00
|
|
|
function toggle(parent)
|
2019-06-07 00:22:03 +02:00
|
|
|
{
|
2019-06-12 21:04:15 +02:00
|
|
|
let hyperlinks = parent.getElementsByClassName("cw-status-hider");
|
2019-06-07 00:22:03 +02:00
|
|
|
if (hyperlinks.length === 0) // If no status is hidden, hide all.
|
|
|
|
{
|
2019-06-12 21:04:15 +02:00
|
|
|
hyperlinks = parent.getElementsByClassName("status-unhider");
|
2019-06-07 00:22:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let hyperlink of hyperlinks)
|
|
|
|
{
|
|
|
|
hyperlink.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:14:28 +02:00
|
|
|
// Returns all conversation-headings or profile-tabs.
|
2019-06-12 21:04:15 +02:00
|
|
|
function get_root_elements()
|
2019-06-07 00:22:03 +02:00
|
|
|
{
|
2019-06-07 01:04:55 +02:00
|
|
|
const main = document.getElementsByClassName("main")[0];
|
|
|
|
|
2019-06-07 00:22:03 +02:00
|
|
|
// If conversation-heading is not there, try profile-tabs.
|
2019-06-12 21:04:15 +02:00
|
|
|
let root = main.getElementsByClassName("conversation-heading");
|
|
|
|
if (root.length === 0)
|
|
|
|
{
|
|
|
|
root = main.getElementsByClassName("tabs");
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
function add_button(parent)
|
|
|
|
{
|
2019-06-12 21:10:24 +02:00
|
|
|
const span = document.createElement("span");
|
2019-06-12 21:04:15 +02:00
|
|
|
const button = document.createElement("a");
|
2019-06-13 20:54:39 +02:00
|
|
|
|
2019-06-12 21:04:15 +02:00
|
|
|
button.setAttribute("class", "global-cw-toggle");
|
2019-06-13 20:54:39 +02:00
|
|
|
button.setAttribute(
|
|
|
|
"style", "margin-left: 1em; margin-right: 0.5em; cursor: pointer;");
|
2019-06-12 21:04:15 +02:00
|
|
|
button.appendChild(document.createTextNode("Toggle all CWs"));
|
|
|
|
button.addEventListener('click', function()
|
|
|
|
{ toggle(parent.parentElement); });
|
2019-06-12 21:10:24 +02:00
|
|
|
span.append(button);
|
2019-06-13 20:54:39 +02:00
|
|
|
|
|
|
|
const otherspans = parent.getElementsByTagName("span");
|
|
|
|
if (otherspans.length > 1) // Place it left of “Collapse”.
|
|
|
|
{
|
|
|
|
parent.insertBefore(span, otherspans[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parent.append(span);
|
|
|
|
}
|
2019-06-12 21:04:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 21:14:28 +02:00
|
|
|
// Check if we need to add a button.
|
2019-06-12 21:04:15 +02:00
|
|
|
function check()
|
|
|
|
{
|
|
|
|
const re = new RegExp(
|
2019-06-12 22:20:04 +02:00
|
|
|
'(/main/(friends|public|all)|/users/[^/]+(/(mentions|dms))?)#?$');
|
2019-06-12 21:04:15 +02:00
|
|
|
const is_timeline = re.test(window.location.href);
|
|
|
|
|
|
|
|
if (!is_timeline) // If we are on a timeline, don't stop checking.
|
2019-06-07 00:22:03 +02:00
|
|
|
{
|
2019-06-12 21:04:15 +02:00
|
|
|
++counter;
|
|
|
|
// If this is not Pleroma or we tried 10 times, disable interval.
|
|
|
|
if (counter > 10 || document.getElementById("app") === null)
|
|
|
|
{
|
|
|
|
clearInterval(interval);
|
|
|
|
return;
|
|
|
|
}
|
2019-06-07 00:22:03 +02:00
|
|
|
}
|
|
|
|
|
2019-06-12 21:04:15 +02:00
|
|
|
const root = get_root_elements();
|
2019-06-07 01:04:55 +02:00
|
|
|
// if root element and a status was found, disable interval and add button.
|
2019-06-12 21:04:15 +02:00
|
|
|
if (root.length !== 0
|
2019-06-12 21:30:22 +02:00
|
|
|
&& root[0].parentElement
|
|
|
|
.getElementsByClassName("status-content").length > 0)
|
2019-06-07 00:22:03 +02:00
|
|
|
{
|
2019-06-12 21:04:15 +02:00
|
|
|
if (!is_timeline)
|
|
|
|
{
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
2019-06-07 00:22:03 +02:00
|
|
|
|
2019-06-12 21:04:15 +02:00
|
|
|
for (let element of root)
|
2019-06-07 01:04:55 +02:00
|
|
|
{
|
2019-06-12 21:30:22 +02:00
|
|
|
const parent = element.parentElement;
|
2019-06-12 21:04:15 +02:00
|
|
|
// Only add button if one or more statuses have a CW.
|
2019-06-12 21:30:22 +02:00
|
|
|
if (parent.getElementsByClassName("cw-status-hider").length > 0
|
|
|
|
|| parent.getElementsByClassName("status-unhider").length > 0)
|
2019-06-12 21:04:15 +02:00
|
|
|
{
|
|
|
|
if (element.getElementsByClassName("global-cw-toggle")
|
|
|
|
.length > 0) // Skip if button is already there.
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_button(element);
|
|
|
|
}
|
2019-06-07 01:04:55 +02:00
|
|
|
}
|
2019-06-07 00:22:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:04:15 +02:00
|
|
|
interval = setInterval(check, 1000); // Try to add button every second.
|