Skip to content

Instantly share code, notes, and snippets.

@devinacker
Last active November 10, 2018 02:56
Show Gist options
  • Save devinacker/92211403b3674487218a07bafcfb0907 to your computer and use it in GitHub Desktop.
Save devinacker/92211403b3674487218a07bafcfb0907 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Mastodon timeline thing
// @description keep timeline from automatically scrolling under mouse cursor
// @author @[email protected]
// @version 0.1
// @include https://mastodon.social/*
// @run-at document-idle
// @namespace revenant.mastodon
// ==/UserScript==
(function () {
"use strict";
// watch for added/removed child nodes for selected nodes and all child nodes
var observerInit = { childList: true, subtree: true };
// callback for when a status (or something else) is added to a timeline
var columnModified = function(recs) {
recs.forEach(function (rec) {
// keep the timeline scrolled down to the same spot
var scrollable = rec.target.closest(".scrollable");
if (scrollable !== null && scrollable.scrollTop === 0) {
rec.addedNodes.forEach(function(node) {
scrollable.scrollTop += node.scrollHeight;
});
}
});
};
// watch for timelines being shown/hidden
new MutationObserver(function(recs) {
recs.forEach(function (rec) {
rec.addedNodes.forEach(function (node) {
if (node.className == "column") {
var itemlist = node.getElementsByClassName("item-list")[0];
if (itemlist == null) return;
itemlist._statusObserver = new MutationObserver(columnModified);
// monitor a timeline if the user puts the mouse over it
itemlist.addEventListener("mouseenter", function() {
itemlist._statusObserver.observe(itemlist, { childList: true });
});
// stop monitoring that timeline when the cursor leaves
itemlist.addEventListener("mouseleave", function() {
itemlist._statusObserver.disconnect();
});
}
});
rec.removedNodes.forEach(function (node) {
if (node.className == "column") {
var itemlist = node.getElementsByClassName("item-list")[0];
if (itemlist == null) return;
delete itemlist._statusObserver;
}
});
});
}).observe(document.getElementsByClassName("columns-area")[0], observerInit);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment