Last active
November 10, 2018 02:56
-
-
Save devinacker/92211403b3674487218a07bafcfb0907 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Mastodon timeline thing | |
| // @description keep timeline from automatically scrolling under mouse cursor | |
| // @author @revenant@mastodon.social | |
| // @version 0.1 | |
| // @match 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) { | |
| // console.log("scroll height updated"); | |
| scrollable.scrollTop += node.scrollHeight; | |
| }); | |
| } | |
| }); | |
| }; | |
| // watch for timelines being shown/hidden | |
| new MutationObserver(function(recs) { | |
| recs.forEach(function (rec) { | |
| rec.addedNodes.forEach(function (node) { | |
| //console.log(node.className); | |
| if (node.className == "column") { | |
| var itemlist = node.getElementsByClassName("item-list")[0]; | |
| //console.log(itemlist); | |
| if (itemlist == null) return; | |
| //console.log("column added"); | |
| itemlist._statusObserver = new MutationObserver(columnModified); | |
| // monitor a timeline if the user puts the mouse over it | |
| itemlist.addEventListener("mouseenter", function() { | |
| //console.log("mouse entered scrollable"); | |
| itemlist._statusObserver.observe(itemlist, { childList: true }); | |
| }); | |
| // stop monitoring that timeline when the cursor leaves | |
| itemlist.addEventListener("mouseleave", function() { | |
| //console.log("mouse exited scrollable"); | |
| itemlist._statusObserver.disconnect(); | |
| }); | |
| } | |
| }); | |
| rec.removedNodes.forEach(function (node) { | |
| if (node.className == "column") { | |
| var itemlist = node.getElementsByClassName("item-list")[0]; | |
| if (itemlist == null) return; | |
| console.log("column removed"); | |
| delete node._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