Last active
August 29, 2015 14:04
-
-
Save rlemon/d7d7a38a4429df94447d to your computer and use it in GitHub Desktop.
reddit links in so chat
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 Reddit links | |
// @author Robert Lemon | |
// @version 0.42.0 | |
// @namespace | |
// @description replaces /r/something with a reddit link. | |
// @include http://chat.stackoverflow.com/rooms/* | |
// ==/UserScript== | |
function exec(fn) { | |
var script = document.createElement('script'); | |
script.setAttribute('type', 'application/javascript'); | |
script.textContent = '(' + fn + ')();'; | |
document.body.appendChild(script); // run the script | |
} | |
exec(function() { | |
var re = /(\B\/r\/\w+)/gi; | |
function parseNode(node) { | |
if (node.classList && node.classList.contains('message') && !node.classList.contains('pending')) { | |
if (re.test(node.textContent)) { | |
node.innerHTML = node.innerHTML.replace(re, function(match) { | |
return ' <a href="http://reddit.com' + match + '">' + match + '</a>'; | |
}); | |
} | |
} | |
} | |
var chat = document.getElementById('chat'); | |
// get all existing messages | |
[].forEach.call(chat.querySelectorAll('.user-container .message'), parseNode); | |
// listen for new messages | |
new MutationObserver(function(records) { | |
records.forEach(function(record) { | |
[].forEach.call(record.addedNodes, parseNode); | |
}); | |
}).observe(chat, { | |
childList: true, | |
subtree: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment