Greasy Fork is available in English.

AO3: Comment Formatting Options

Adds interface to comments to easily insert formatting options in HTML

  1. // ==UserScript==
  2. // @name AO3: Comment Formatting Options
  3. // @namespace adustyspectacle
  4. // @description Adds interface to comments to easily insert formatting options in HTML
  5. // @include http://*archiveofourown.org/*
  6. // @include https://*archiveofourown.org/*
  7. // @version 1.2
  8. // @history 1.2 - updated to use searchParams to make life easier
  9. // @history 1.1 - Updated included urls
  10. // @history 1.0 - Initial release
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function($) {
  15. //
  16. var ICONIFY = true;
  17. //
  18.  
  19. if (ICONIFY) {
  20. var font_awesome_icons = document.createElement('script');
  21. font_awesome_icons.setAttribute('src', 'https://use.fontawesome.com/ed555db3cc.js');
  22. document.getElementsByTagName('head')[0].appendChild(font_awesome_icons);
  23.  
  24. var fa_icons_css = document.createElement('style');
  25. fa_icons_css.setAttribute('type', 'text/css');
  26. fa_icons_css.innerHTML = `
  27. .comment-formatting {
  28. font-family: FontAwesome, sans-serif;
  29. }
  30. `
  31. document.getElementsByTagName('head')[0].appendChild(fa_icons_css);
  32. }
  33.  
  34. // note: commentId must be in the form of #comment_content_for_COMMENTID
  35. function addCommentFormattingUI(commentId) {
  36. var postCommentField = $(commentId);
  37. var commentUniqueId = postCommentField[0].id.slice(20);
  38. var commentFormatting = document.createElement("ul");
  39.  
  40. var commentFormattingOptions = {
  41. bold_text: [["Bold", "&#xf032"], ["<strong>", "</strong>"]],
  42. italic_text: [["Italic", "&#xf033"], ["<em>", "</em>"]],
  43. underline_text: [["Underline", "&#xf0cd"], ["<u>", "</u>"]],
  44. strike_text: [["Strikethrough", "&#xf0cc"], ["<s>", "</s>"]],
  45. insert_link: [["Insert Link", "&#xf0c1"], ['<a href="">', "</a>"]],
  46. insert_image: [["Insert Image", "&#xf03e"], ['<img src="">']],
  47. blockquote_text: [["Blockquote", "&#xf10d"], ["<blockquote>", "</blockquote>"]]
  48. }
  49.  
  50. commentFormatting.setAttribute("id", "comment_formatting_for_" + commentUniqueId);
  51. commentFormatting.setAttribute("class", "actions comment-formatting");
  52. commentFormatting.setAttribute("style", "float: left; text-align: left;");
  53. commentFormatting.innerHTML = "<h4>Formatting Options:</h4>";
  54. postCommentField.before(commentFormatting);
  55.  
  56. for (let key in commentFormattingOptions) {
  57. var commentFormattingOptionItem = document.createElement("li");
  58. var commentFormattingOptionLink = document.createElement("a");
  59. let commentFormattingOptionItemId = key + "_" + commentUniqueId;
  60.  
  61. commentFormattingOptionItem.setAttribute("id", commentFormattingOptionItemId);
  62. commentFormattingOptionItem.setAttribute("class", key);
  63. commentFormattingOptionItem.setAttribute("title", commentFormattingOptions[key][0][0]);
  64.  
  65. if (ICONIFY) commentFormattingOptionLink.innerHTML = commentFormattingOptions[key][0][1];
  66. else commentFormattingOptionLink.innerHTML = commentFormattingOptions[key][0][0];
  67.  
  68. commentFormattingOptionItem.appendChild(commentFormattingOptionLink);
  69. commentFormatting.appendChild(commentFormattingOptionItem);
  70. $("#" + commentFormattingOptionItemId).on('click', 'a', function() {
  71. var caretPos = $(commentId)[0].selectionStart;
  72. var caretEnd = $(commentId)[0].selectionEnd;
  73. var textAreaTxt = $(commentId).val();
  74. if (caretPos == caretEnd) {
  75. var formatToAdd = commentFormattingOptions[key][1].join("");
  76. } else {
  77. var textAreaHighlight = textAreaTxt.slice(caretPos, caretEnd);
  78. var formatToAdd = commentFormattingOptions[key][1].join(textAreaHighlight);
  79. }
  80. $(commentId).val(textAreaTxt.substring(0, caretPos) + formatToAdd + textAreaTxt.substring(caretEnd) );
  81.  
  82. $(commentId).focus();
  83. $(commentId)[0].selectionStart = caretPos + txtToAdd.length
  84. $(commentId)[0].selectionEnd = caretPos + txtToAdd.length
  85. });
  86. }
  87.  
  88. }
  89. // For the Add Comment area found in works/tags
  90. if ( $("#add_comment textarea").length ) {
  91. var add_comment_box_id = "#" + $("#add_comment textarea")[0].id;
  92. addCommentFormattingUI(add_comment_box_id);
  93. }
  94. // This whole bit is for Reply Comments stuff, because AJAX
  95. $( document ).ajaxSuccess(function( event, xhr, settings ) {
  96. // This is for replying to comments
  97. if (settings.url.indexOf("add_comment_reply") !== -1) {
  98. var params = (new URL(settings.url)).searchParams;
  99. var commentReplyId = params.get("id");
  100. var commentReplyIdSelector = $("#comment_content_for_" + commentReplyId).selector;
  101.  
  102. addCommentFormattingUI(commentReplyIdSelector);
  103. }
  104. // This is for inbox comments
  105. else if (settings.url.indexOf("inbox/reply?") !== -1) {
  106. var params = (new URL(settings.url)).searchParams;
  107. var commentReplyId = params.get("comment_id");
  108. var commentReplyIdSelector = $("#comment_content_for_" + commentReplyId).selector;
  109.  
  110. addCommentFormattingUI(commentReplyIdSelector);
  111. }
  112.  
  113. // Commented out for now since it still doesn't work properly.
  114. // // This is for when editing a comment.
  115. // else if (settings.url.indexOf("comments/") !== -1 && settings.url.indexOf("/edit") !== -1) {
  116. // var sliceStart = settings.url.indexOf("comments/") + 9;
  117. // var sliceEnd = settings.url.indexOf("/edit");
  118. // var commentEditId = settings.url.slice(sliceStart, sliceEnd);
  119. // var commentEditIdSelector = "#" + $("#comment_" + commentEditId + " textarea")[0].id;
  120. // addCommentFormattingUI(commentEditIdSelector);
  121. // }
  122. });
  123. })(jQuery);