goodreadsReply

Quote only the selected text on a Goodreads comment reply; allow user to use Markdown

  1. // ==UserScript==
  2. // @name goodreadsReply
  3. // @namespace http://pointerstop.ca/
  4. // @version 0.6.3
  5. // @description Quote only the selected text on a Goodreads comment reply; allow user to use Markdown
  6. // @author derek@pointerstop.ca
  7. // @match https://www.goodreads.com/topic/*
  8. // @match https://www.goodreads.com/review/show/*
  9. // @grant none
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/showdown/1.6.4/showdown.min.js
  11. // ==/UserScript==
  12. function makeHTML(comment){
  13. var options = {simplifiedAutoLink: true,
  14. excludeTrailingPunctuationFromURLs: true,
  15. literalMidWordUnderscores: true,
  16. strikethrough: true
  17. };
  18. var converter = new showdown.Converter(options),
  19. text = comment.val(),
  20. html = converter.makeHtml(text).replace(/<p>/g, '\n').replace(/<\/p>/g, '');
  21. comment.val(html);
  22. }
  23. jQuery(function($) {
  24. 'use strict';
  25. var preview = $("input:submit + span + a");
  26. var commentText = $('textarea');
  27. var clickHandler = preview.prop("onclick");
  28. preview.prop("onclick",null);
  29. $("input:submit").on('click', function(){
  30. makeHTML(commentText);
  31. });
  32. preview.on('click', function(){
  33. makeHTML(commentText);
  34. clickHandler();
  35. });
  36. $("#box #close").on('click',function(){ commentText.focus(); });
  37. // turn off all the old click handlers
  38. $("a[href='#comment_form']").prop("onclick", null);
  39.  
  40. // attach the new click handlers
  41. $("a[href='#comment_form']").on('click', function (e) {
  42. // disable the default handler
  43. e.preventDefault();
  44. // find the parent comment, the author's name, and the comment text
  45. var comment = $(e.target).parents("div.comment");
  46. var author = comment.find("span.commentAuthor a").text();
  47. var text = comment.find("div.reviewText");
  48. // use the selected text, if it exists, else the first 200 characters of the comment
  49. var selection = "";
  50. try {
  51. // if we're selecting a text node... get the selection
  52. if (window.getSelection().anchorNode.parentElement==text[0]){
  53. selection = window.getSelection().toString();
  54. }
  55. } catch(e) { // we don't care about errors
  56. }
  57. // if nothing has been selected, use GoodRead's normal default of the first 200 chars
  58. if (!selection) {
  59. selection = text.text().trim();
  60. if (selection.length > 200) {
  61. selection = selection.substring(0, 200) + '...';
  62. }
  63. }
  64. var quoteText = '<i>' + author + ' wrote: "' + selection + '"</i>\n\n';
  65.  
  66. // use 'text()' to get the value, but 'val()' to set it, because carriage
  67. // returns are stripped by val()
  68. commentText.val(commentText.val() + quoteText);
  69. commentText.focus();
  70. });
  71. });