Skip to content

Instantly share code, notes, and snippets.

@mxamber
Last active November 21, 2022 14:31
Show Gist options
  • Save mxamber/3d8227ed2cf22a12c79956fabefbf7dc to your computer and use it in GitHub Desktop.
Save mxamber/3d8227ed2cf22a12c79956fabefbf7dc to your computer and use it in GitHub Desktop.
Changes the "Publish" button back to "Toot", with "Send" for DMs and "Broadcast" for Public toots (all labels can be changed at the top). Works for glitch-soc only.
// ==UserScript==
// @name Toot Button Revival Advanced
// @namespace http://tampermonkey.net/
// @version 0.3
// @description here be toots
// @author mxamber
// @match https://eldritch.cafe/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
/*
* remember to change domain name as needed
*/
// labels to use for buttons, adjust as needed
const Labels = [
"Send", // DM
"<i class='fa fa-lock'></i> Toot", // Followers only
"Toot", // Unlisted
"Broadcast" // Public
];
/*
* function to find the button and change its text if it exists
*/
function updateButton() {
let button = document.querySelector("button.button.primary");
if(!button) { return; }
/*
* includes "only" (DM or followers)?
* if yes: includes "Mentioned" (DM) or not (followers only)?
* if no: includes "Unlisted" (Unlisted) or not (Public)?
*/
button.innerHTML = button.title.includes("only") ? (button.title.includes("Mentioned") ? Labels[0] : Labels[1]) : (button.title.includes("Unlisted") ? Labels[2] : Labels[3]);
}
/*
* change the text every time the title of the toot button is changed to reflect updated post privacy
*/
function observeCallback(mutationList, observer) {
for (const mutation of mutationList) {
// first false-ish value cancels if, order optimised for maximum efficiency
// non-attribute mutations will already drop half of the mutations
// only filtering for title changes will restrict to very few remaining
// only then do querySelector; most mutations won't even get to this stage
// && mutation.target == document.querySelector("button.button.primary")
if(mutation.type == "attributes" && mutation.attributeName == "title") {
// don't overwrite the "save changes" edit button
if(mutation.target.innerHTML != "Save changes") { updateButton(); }
} else {
// console.log(`Wrong mutation! ${mutation.type} ${mutation.target.tagName} #${mutation.target.id} .${mutation.target.classList}`);
}
}
}
/*
* change the text for the first time as soon as the button is created in the DOM
*/
function firstCallback(mutationList, firstObserver) {
if(document.querySelector("button.button.primary")) {
firstObserver.disconnect();
updateButton();
// only now observe the now-existing button for changes as described in callback above
const observer = new MutationObserver(observeCallback);
observer.observe(document.querySelector("button.button.primary"), { attributes: true});
}
}
const firstobserver = new MutationObserver(firstCallback);
firstobserver.observe(document.body, { subtree: true, childList: true });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment