Created
August 1, 2016 15:26
-
-
Save anonymous/1967bdba0afa4c5e3fce7ee5e98fbd09 to your computer and use it in GitHub Desktop.
Shtetl-Link: userscript for Scott Aaronson's blog
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 Shtetl-Link | |
// @namespace shtetl | |
// @description Turn comment references into clickable links. Additionally, apply a CSS class to Scott's comments. Best used with <https://userstyles.org/styles/121144/shtetl-optimized-for-wide-screens> | |
// @include http://www.scottaaronson.com/blog/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
// Public domain 2016 | |
var cmtList = [ | |
document.getElementById('content') | |
]; | |
function linkify(node) { | |
var m; | |
while (m = /#(\d+)/.exec(node.data)) { | |
var preNode = document.createTextNode(node.data.substr(0, m.index)); | |
node.parentNode.insertBefore(preNode, node); | |
var linkNode = document.createElement('a'); | |
if (cmtList[m[1]]) | |
linkNode.href = '#' + cmtList[m[1]].id; | |
linkNode.textContent = m[0]; | |
node.parentNode.insertBefore(linkNode, node); | |
node.data = node.data.substr(m.index + m[0].length); | |
} | |
} | |
Array.prototype.forEach.call(document.querySelectorAll('.commentlist > li'), function(cmtNode) { | |
// var author = cmtNode.querySelector('cite').textContent; | |
var authorLink = cmtNode.querySelector('cite > a'); | |
var m = /^Comment #(\d+)/.exec(cmtNode.querySelector('.commentmetadata').textContent); | |
cmtList[m[1]] = cmtNode; | |
var scottLinks = { | |
'http://www.scottaaronson.com/': true, | |
'http://www.blogger.com/profile/13726794': true, | |
}; | |
// cmtNode.classList.add('cmt-by-' + author); | |
if (authorLink && scottLinks[authorLink.href]) { | |
cmtNode.classList.add('cmt-the-blogger'); | |
} | |
var nodes = cmtNode.querySelectorAll('.commentmetadata ~ :not(a), .commentmetadata ~ :not(a) :not(a)'); | |
for (var i = 0; i < nodes.length; ++i) { | |
var child = nodes[i].childNodes[0]; | |
while (child) { | |
if (child && child.nodeType == document.TEXT_NODE) { | |
linkify(child); | |
} | |
child = child.nextSibling; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment