Reddit Inline Comments Viewer

View inline Reddit threads from the front page or any subreddit.

As of 2015-12-15. See the latest version.

  1. // ==UserScript==
  2. // @name Reddit Inline Comments Viewer
  3. // @namespace http://reddit.com
  4. // @version 0.15
  5. // @description View inline Reddit threads from the front page or any subreddit.
  6. // @author jaszhix
  7. // @include http*://www.reddit.com/*
  8. // @exclude http*://www.reddit.com/*/*/comments/*
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
  10. // @run-at document-end
  11. // ==/UserScript==
  12. $(document).ready(function($){
  13. $('div>div>ul>li:nth-child(1)>a').each(function(i) {
  14. var post = $(this).parents().eq(3);
  15. var toggleButton = $('<li class="site-viewer-' + i + '"><a href="#">view thread</a><li>');
  16. toggleButton.insertAfter(post.find('ul>li:nth-child(5)'));
  17. var scrollToNextPost = function(){
  18. var nextI = ++i;
  19. $('.site-viewer-' + nextI).get(0).scrollIntoView();
  20. };
  21. var insertButton = function(){
  22. $('<button class="inline-next-post" style="position: fixed; top: 95%; left: 85%;">Next Post</button>').insertAfter(post.find('ul>li:nth-child(6)'));
  23. post.find('.inline-next-post').click(function() {
  24. scrollToNextPost();
  25. post.find('button').hide();
  26. });
  27. };
  28. var toggleButtonLink = $('.site-viewer-' + i + '>a');
  29. var closeThread = function(){
  30. toggleButtonLink.text('view thread');
  31. post.find('div.commentarea').hide();
  32. post.find('button').hide();
  33. };
  34. var viewThread = function(){
  35. toggleButtonLink.text('close thread');
  36. post.find('button').show();
  37. post.find('div.commentarea').show();
  38. };
  39. $('.site-viewer-' + i).click(function(e) {
  40. e.preventDefault();
  41. e.stopPropagation();
  42. if (toggleButtonLink.text() === 'close thread') {
  43. closeThread();
  44. } else {
  45. if (post.find('div.commentarea').length > 0) {
  46. viewThread();
  47. } else {
  48. toggleButtonLink.text('loading...');
  49. $('<iframe />').attr({
  50. 'src': $(this).attr('href'),
  51. 'frameborder': '0',
  52. 'width': window.innerWidth / 1.2,
  53. 'height': window.innerHeight
  54. }).appendTo(post);
  55. var iframe = post.find('iframe');
  56. iframe.hide();
  57. iframe.on('load', function() {
  58. toggleButtonLink.text('close thread');
  59. if ($('.inline-next-post').length > 0) {
  60. $('.inline-next-post').hide();
  61. }
  62. insertButton();
  63. iframe.contents().find('div.commentarea').appendTo(post);
  64. iframe.remove();
  65. $('<li class="inline-close-thread"><a href="#">close thread</a><li>').appendTo(post.find('div.commentarea'));
  66. $('.inline-close-thread').click(function(e){
  67. e.preventDefault();
  68. e.stopPropagation();
  69. closeThread();
  70. $('.site-viewer-' + i).get(0).scrollIntoView();
  71. });
  72. });
  73. }
  74. }
  75. }.bind(this));
  76. });
  77. });