Last active
August 29, 2015 14:24
-
-
Save br4nnigan/8fda9c6a3462e914ae8a to your computer and use it in GitHub Desktop.
create direct links to x-posted subs
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 reddit x-post | |
// @namespace reddit_xpost | |
// @description add x-post link | |
// @include https://www.reddit.com/* | |
// @version 1.1 | |
// @grant none | |
// ==/UserScript== | |
function makeXpostLinks () { | |
var | |
titles = document.querySelectorAll("a.title"), | |
xpost_re = /x-?post/i, | |
xpost_from_re = /(x-?post\w*\b)(\s{0,1}\S+)?\s([r/]+)([a-zA-Z0-9]+)/i, | |
debug = 0; | |
for (var i = 0, title, tagline, entry, userattrs, textNode, subreddit, add_after, xpost_from, xpost_from_a, sub; i < titles.length; i++) { | |
title = titles[i]; | |
if ( xpost_re.test(title.innerHTML) && !title.classList.contains("x-post") ){ | |
xpost_from = title.innerHTML.match(xpost_from_re); | |
if ( xpost_from && xpost_from.length >= 5 ){ | |
sub = xpost_from[4]; | |
entry = title.parentNode.parentNode; | |
if ( entry && entry.classList.contains("entry") ){ | |
textNode = document.createTextNode(" x-posted from "); | |
xpost_from_a = document.createElement("a"); | |
xpost_from_a.href = "https://www.reddit.com/r/"+sub; | |
xpost_from_a.className = "subreddit hover"; | |
xpost_from_a.innerHTML = "/r/"+sub; | |
userattrs = entry.querySelector(".userattrs"); | |
subreddit = entry.querySelector(".subreddit"); | |
tagline = entry.querySelector(".tagline"); | |
add_after = subreddit || userattrs || tagline && tagline.lastChild; | |
if ( add_after ){ | |
if ( add_after.nextSibling ){ | |
add_after.parentNode.insertBefore(textNode, add_after.nextSibling); | |
add_after.parentNode.insertBefore(xpost_from_a, textNode.nextSibling); | |
} else { | |
add_after.parentNode.appendChild(textNode); | |
add_after.parentNode.appendChild(xpost_from_a); | |
} | |
} else { | |
if (debug) console.error("add_after is null"); | |
} | |
title.classList.add("x-post"); | |
} else { | |
if (debug) console.error("entry is null"); | |
} | |
} | |
} | |
} | |
} | |
document.addEventListener("DOMContentLoaded", makeXpostLinks); | |
window.addEventListener("neverEndingLoad", makeXpostLinks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment