Created
May 3, 2022 11:09
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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