AO3: Comment Formatting Options

Adds interface to comments to easily insert formatting options in HTML

当前为 2017-10-07 提交的版本,查看 最新版本

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