Misskey CW toggle: reduce scope, fix CSS somewhat

Button positioning isn't great, but not awful anymore.
This commit is contained in:
tastytea 2022-05-17 01:11:42 +02:00
parent 6a2eadc05a
commit e9b803c50c
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
1 changed files with 15 additions and 6 deletions

View File

@ -1,7 +1,7 @@
// ==UserScript==
// @name Misskey CW toggle
// @description Adds a button to toggle the visibility of all notes with content warnings on note-pages.
// @version 2022.05.17.1
// @version 2022.05.17.2
// @author tastytea
// @copyright 2022, tastytea (https://tastytea.de/)
// @license GPL-3.0-only
@ -20,7 +20,11 @@ let counter = 0;
// Toggle the visibility of each status with CW.
function toggle() {
for (let status of document.getElementsByClassName("cw")) {
const root = document.getElementsByClassName("_block _isolated note")[0];
if (root === undefined) {
return;
}
for (let status of root.getElementsByClassName("cw")) {
let button = status.getElementsByTagName("button")[0];
if (button === undefined) {
continue;
@ -32,7 +36,7 @@ function toggle() {
// Add a “Toggle all CWs”-button.
function add_button() {
// If there is no element named “column-1”, use the footer.
let root = document.getElementsByClassName("_block _isolated note")[0];
const root = document.getElementsByClassName("_block _isolated note")[0];
if (root === undefined) {
console.error("No suitable parent-element found.");
return;
@ -42,7 +46,7 @@ function add_button() {
const button = document.createElement("button");
button.setAttribute("id", "global-cw-toggle");
button.setAttribute("class", "_button");
button.setAttribute("style", "float: right; margin: 0.2em 0.5em;");
button.setAttribute("style", "margin-top: 0.2em; margin-right: 0.5em;");
button.appendChild(document.createTextNode("Toggle all CWs"));
root.insertBefore(button, root.firstChild);
@ -51,9 +55,14 @@ function add_button() {
}
function check() {
const root = document.getElementsByClassName("_block _isolated note")[0];
if (root === undefined) {
return;
}
// If there is a “Show content” button, add our button, if we didn't do so before.
if (document.getElementsByClassName("cw").length > 0
&& document.getElementById("global-cw-toggle") === null) {
if (root.getElementsByClassName("cw").length > 0
&& root.getElementById("global-cw-toggle") === null) {
add_button();
console.debug("Global CW toggle button added");
}