Created
November 22, 2015 17:07
-
-
Save shu8/6efa5cb9f58313c2c001 to your computer and use it in GitHub Desktop.
View posts inline
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 Display linked posts inline! | |
// @namespace http://stackexchange.com/users/4337810/ | |
// @version 1.0 | |
// @description Adds an arrow to links in posts/comments to expand and show the contents of the post | |
// @author ᔕᖺᘎᕊ (http://stackexchange.com/users/4337810/) | |
// @match *://*.stackexchange.com/questions/* | |
// @match *://*.stackoverflow.com/questions/* | |
// @match *://*.superuser.com/questions/* | |
// @match *://*.serverfault.com/questions/* | |
// @match *://*.askubuntu.com/questions/* | |
// @match *://*.stackapps.com/questions/* | |
// @match *://*.mathoverflow.net/questions/* | |
// @grant none | |
// ==/UserScript== | |
function getIdFromUrl(url) { | |
if (url.indexOf('/a/') > -1) { //eg. http://meta.stackexchange.com/a/26764/260841 | |
return url.split('/a/')[1].split('/')[0]; | |
} else if (url.indexOf('/q/') > -1) { //eg. http://meta.stackexchange.com/q/26756/260841 | |
return url.split('/q/')[1].split('/')[0]; | |
} else if (url.indexOf('/questions/') > -1) { | |
if (url.indexOf('#') > -1) { //then it's probably an answer, eg. http://meta.stackexchange.com/questions/26756/how-do-i-use-a-small-font-size-in-questions-and-answers/26764#26764 | |
/* | |
var x = url.split('/'); | |
var ids = x[x.length-1].split('#'); | |
if(ids[0] == ids[1]) return ids[0]; | |
*/ | |
return url.split('#')[1]; | |
} else { //then it's a question | |
return url.split('/questions/')[1].split('/')[0]; | |
} | |
} | |
} | |
function getBody(postId, callback) { | |
$.get("http://" + $(location).attr('hostname') + "/posts/" + postId + "/body", function (d) { | |
callback(d); | |
}); | |
} | |
$('.post-text a, .comments .comment-copy a').each(function () { | |
var url = $(this).attr('href'); | |
if (url && url.indexOf($(location).attr('hostname')) > -1 && url.indexOf('/questions/') > -1 && url.indexOf('#comment') == -1) { | |
$(this).css('color', '#0033ff'); | |
$(this).before('<a class="expander-arrow-small-hide expand-post-shub"></a>'); | |
} | |
}); | |
$(document).on('click', 'a.expand-post-shub', function () { | |
if ($(this).hasClass('expander-arrow-small-show')) { | |
$(this).removeClass('expander-arrow-small-show'); | |
$(this).addClass('expander-arrow-small-hide'); | |
$('.loaded-body-shub').remove(); | |
} else if ($(this).hasClass('expander-arrow-small-hide')) { | |
$(this).removeClass('expander-arrow-small-hide'); | |
$(this).addClass('expander-arrow-small-show'); | |
$that = $(this); | |
id = getIdFromUrl($(this).next().attr('href')); | |
getBody(id, function (d) { | |
var div = "<div class='loaded-body-shub' style='background-color: #ffffcc;'>" + d + "</div>"; | |
$that.next().after(div); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment