FTB comment spiffifier

Make commenting on FTB more better

  1. // ==UserScript==
  2. // @name FTB comment spiffifier
  3. // @namespace http://niko.cat/
  4. // @version 0.1
  5. // @description Make commenting on FTB more better
  6. // @include http://freethoughtblogs.com/*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
  8. // ==/UserScript==
  9.  
  10.  
  11. /*
  12. <li class="comment byuser comment-author-nerdofredhead odd alt thread-odd thread-alt depth-1" id="comment-860508">
  13. <article itemprop="comment" itemscope="itemscope" itemtype="http://schema.org/UserComments">
  14. <header class="comment-header">
  15. <p class="comment-author" itemprop="creator" itemscope="itemscope" itemtype="http://schema.org/Person">
  16. <img alt='' src='http://0.gravatar.com/avatar/47f9d14fc29ffe78436ab7c50d58dc08?s=48&amp;d=identicon&amp;r=R' class='avatar avatar-48 photo' height='48' width='48'/><span itemprop="name">Nerd of Redhead, Dances OM Trolls</span> <span class="says">says</span> </p>
  17. <p class="comment-meta">
  18. <time itemprop="commentTime" datetime="2014-10-02T09:33:13+00:00"><a href="http://freethoughtblogs.com/pharyngula/2014/10/02/my-morning-mail-bag-so-far/comment-page-1/#comment-860508" itemprop="url">2 October 2014 at 9:33 am</a></time> </p>
  19. </header>
  20. <div class="comment-content" itemprop="commentText">
  21. <blockquote><p>does Reddit have any responsibility to build a healthy community? I argue they do not. </p></blockquote>
  22. <p>Then you argue from a position of moral bankruptcy, and can and will be criticized for that moral failure. There is no excuse for an unhealthy community.</p>
  23. </div>
  24. </article>
  25. </li>
  26. */
  27.  
  28.  
  29. // http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
  30. $.fn.selectRange = function(start, end) {
  31. if (!end) end = start;
  32. return this.each(function() {
  33. if (this.setSelectionRange) {
  34. this.focus();
  35. this.setSelectionRange(start, end);
  36. } else if (this.createTextRange) {
  37. var range = this.createTextRange();
  38. range.collapse(true);
  39. range.moveEnd('character', end);
  40. range.moveStart('character', start);
  41. range.select();
  42. }
  43. });
  44. };
  45.  
  46.  
  47. var hoverbox = $('<span id="hoverbox">' +
  48. '\xa0|\xa0<a class="reply-link" href="#"><b>Reply</b></a>' +
  49. '</span>');
  50. var template = $('<div>' +
  51. '<a><span class="comment-author-link"></span>@<span class="comment-number-link"></span>:</a>\n' +
  52. '<blockquote></blockquote>' +
  53. '</div>');
  54.  
  55. jQuery(document).on('mouseenter', 'li.comment', function hover() {
  56. jQuery('#hoverbox').remove();
  57. jQuery(this).closest('li.comment').find('time').first().after(hoverbox);
  58. }).on('mouseleave', 'li.comment', function unhover() {
  59. jQuery('#hoverbox').remove();
  60. }).on('click', '#hoverbox a.reply-link', function(event) {
  61. event.preventDefault();
  62. var comment = $(this).closest('li.comment');
  63. var id = comment.attr('id');
  64. var link = comment.find('time a').attr('href');
  65. var author = comment.find('.comment-author span[itemprop="name"]').text();
  66. var label = comment[0].getAttribute('value');
  67. if (label === null) label = comment.index() + 1; // sigh
  68. var content = comment.find('.comment-content');
  69. var fake = template.clone();
  70. fake.find('a').attr('href', '#' + id).end()
  71. .find('span.comment-author-link').text(author).end()
  72. .find('span.comment-number-link').text(label).end()
  73. .find('blockquote').attr('cite', link).append(content.clone().contents()).end();
  74. var textarea = $('#comment');
  75. var text = textarea.val();
  76. if (text.length > 0) text += '\n';
  77. text += fake.html() + '\n';
  78. textarea.val(text);
  79. window.location.hash = '#comment';
  80. textarea.selectRange(textarea.val().length).scrollTop(textarea[0].scrollHeight);
  81. });