Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2017 07:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/6292fe8e325d45058117bfb97a74ac7a to your computer and use it in GitHub Desktop.
// ==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