Skip to content

Instantly share code, notes, and snippets.

@mxamber
Created May 3, 2022 11:09
Show Gist options
  • Save mxamber/6585caf2e4b1667fea51d5c699386bdd to your computer and use it in GitHub Desktop.
Save mxamber/6585caf2e4b1667fea51d5c699386bdd to your computer and use it in GitHub Desktop.
Changes the tab title of Mastodon-based Fediverse instances in real time according to what's displayed (toot text)
// ==UserScript==
// @name Fedi Tab Title
// @version 0.1
// @grant none
// @description Dynamic tab title for Mastodon-based Fediverse instances.
// @author mxamber
// ==/UserScript==
/*
define function invoked periodically to refresh page title
parameter: page default title
*/
function setTitle(title) {
// if site doesn't have a link to Mastodon's Github, it's probably not a Mastodon instance
if(document.querySelector("a[href='https://github.com/mastodon/mastodon']") == null) {
return;
}
// if a toot is currently in focus (=expanded), scrape username and text from it
if(document.querySelector(".focusable.detailed-status__wrapper") != null) {
let username = document.querySelector(".detailed-status__display-name").innerText;
let text = document.querySelector(".focusable.detailed-status__wrapper .status__content").innerText;
// set webpag title like on birdsite: user on instance: "lorem ipsum"
document.title = username + " on " + title + ": \"" + text + "\"";
} else {
// if no tweet in focus, revert to default title
document.title = title;
}
}
/* actual code executed once on page load begins here */
// initial title of the page = default name
var sitename = document.title;
// call setTitle every second and pass default name as parameter
setInterval(function() {
setTitle(sitename);
}, 1000);
/*
Mastodon frontend is based on ReactJS (or something like that)
Clicking on a toot, notifications, etc doesn't load a different page, the page is just rebuilt in real time
Due to this, the setTitle function has to be called periodically, because the script itself only runs once
Hence the setInterval
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment