Created
March 1, 2017 07:00
Star
You must be signed in to star a gist
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 MetaFilter: scroll past deleted comments | |
// @namespace http://www.metafilter.com/user/57912 | |
// @version 0.1 | |
// @match http://www.metafilter.com/* | |
// @match http://ask.metafilter.com/* | |
// @match http://fanfare.metafilter.com/* | |
// @match http://projects.metafilter.com/* | |
// @match http://music.metafilter.com/* | |
// @match http://jobs.metafilter.com/* | |
// @match http://irl.metafilter.com/* | |
// @match http://metatalk.metafilter.com/* | |
// @copyright 2017 | |
// ==/UserScript== | |
window.addEventListener("load", function() { | |
if (window.location.hash !== "") { | |
var hash = window.location.hash.substr(1); | |
var targetId = parseFloat(hash); | |
var targetElements = document.getElementsByName(hash); | |
if (targetElements.length === 0 && !isNaN(targetId)) { | |
// find all comment anchors | |
var allAnchors = document.querySelectorAll("a:empty"); | |
var commentIds = []; | |
for (var i = 0; i < allAnchors.length; i++) { | |
var id = parseFloat(allAnchors[i].name); | |
if (!isNaN(id)) { | |
commentIds.push(id); | |
} | |
} | |
commentIds.sort(function(x,y) { return x-y; }); | |
// binary search for first id >= hash | |
var lo = 0, hi = commentIds.length; | |
while (lo < hi) { | |
var mid = Math.floor((lo + hi) / 2); | |
if (commentIds[mid] < targetId) { | |
lo = mid + 1; | |
} else if (commentIds[mid] > targetId) { | |
hi = mid; | |
} else { | |
lo = hi = mid; | |
} | |
} | |
var targetIndex = lo; | |
if (targetIndex >= commentIds.length) { | |
targetIndex = commentIds.length - 1; | |
} | |
window.location.hash = "#" + commentIds[targetIndex]; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment