Quick Quote

Only selected text is quoted as long as it's quoted in the proper quote box

当前为 2014-07-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Quick Quote
  3. // @namespace What.CD
  4. // @description Only selected text is quoted as long as it's quoted in the proper quote box
  5. // @include https://*what.cd/inbox.php?action=viewconv&id=*
  6. // @include https://*what.cd/forums.php?*action=viewthread&threadid=*
  7. // @include https://*what.cd/torrents.php?id=*
  8. // @include https://*what.cd/colleges.php?id=*
  9. // @include https://*what.cd/artist.php*
  10. // @include https://*what.cd/requests.php?action*
  11. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
  12. // @version 1.0.1
  13. // ==/UserScript==
  14.  
  15. {
  16. function addJQuery(callback) {
  17. var script = document.createElement("script");
  18. script.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
  19. script.addEventListener('load', function() {
  20. var script = document.createElement("script");
  21. script.textContent = "(" + callback.toString() + ")();";
  22. document.body.appendChild(script);
  23. }, false);
  24. document.body.appendChild(script);
  25. }
  26.  
  27. function main() {
  28. var orig_quote = window.Quote;
  29. window.Quote = function(args){
  30. return false;
  31. };
  32. $('a[onclick^="Quote("]').click(function(event) {
  33. //Get required arguments
  34. var arguments = jQuery(this).attr("onclick");
  35. var username = arguments.replace(/Quote\(\'[0-9]*\', \'/,"").replace(/\'.*/,"");
  36. var postID = arguments.replace(/Quote\(\'/,"").replace(/\'.*/,"");
  37. var textArea = jQuery('#quickpost');
  38. var addNewLines = false;
  39. var link = true;
  40. var target = '';
  41. var requrl = '';
  42. var elem = getSelectionContainerElement(); // find what element contains the selection and only quote if it's the container for the Quote button you clicked on
  43. var selText = window.getSelection().toString();
  44. if (url.path == "inbox") {
  45. requrl = 'inbox.php?action=get_post&post=' + postID;
  46. } else {
  47. requrl = 'comments.php?action=get&postid=' + postID;
  48. }
  49. if (url.path == "artist") {
  50. // artist comment
  51. target = 'a';
  52. } else if (url.path == "torrents") {
  53. // torrent comment
  54. target = 't';
  55. } else if (url.path == "collages") {
  56. // collage comment
  57. target = 'c';
  58. } else if (url.path == "requests") {
  59. // request comment
  60. target = 'r';
  61. } else {
  62. // forum post
  63. requrl = 'forums.php?action=get_post&post=' + postID;
  64. }
  65. target += postID;
  66. /*
  67. if(selText.length != 0 && (elem.id == "content"+postID || (elem && elem.id == "" && elem.nodeName != "BLOCKQUOTE" && elem.parentNode.id == "content"+postID))){
  68. // the target and requrl code got moved above so that the links work in more than just the forums
  69. var target = '';
  70. var requrl = '';
  71. ajax.get(requrl, function(response) {
  72. debugger;
  73. var selText = window.getSelection().toString();
  74. var selection = window.getSelection();
  75. if ($('#quickpost').raw().value !== '') {
  76. $('#quickpost').raw().value = $('#quickpost').raw().value + "\n\n";
  77. }
  78. str = html_entity_decode(response);
  79. $('#quickpost').raw().value = $('#quickpost').raw().value + "[quote=" + username + "|" + target + "]" + str + "[/quote]";
  80. resize('quickpost');
  81. });
  82. */
  83. if(selText.length != 0 && (elem.id == "content"+postID || (elem && elem.id == "" && elem.nodeName != "BLOCKQUOTE" && elem.parentNode.id == "content"+postID))){
  84. if (textArea.val() !== '') addNewLines = true;
  85. textArea.val(textArea.val() + (addNewLines ? "\n\n" : "") + "[quote="+username + "|" + target + "]" + selText.trim() + "[/quote]");
  86. resize('quickpost');
  87. //...Otherwise proceed with a regular full-post quote
  88. } else {
  89. orig_quote(postID, username, true);
  90. }
  91. textArea[0].scrollIntoView();
  92.  
  93. return false;
  94. });
  95.  
  96. function getSelectionContainerElement() {
  97. var range, sel, container;
  98. if (document.selection && document.selection.createRange) {
  99. // IE case
  100. range = document.selection.createRange();
  101. return range.parentElement();
  102. } else if (window.getSelection) {
  103. sel = window.getSelection();
  104. if (sel.getRangeAt) {
  105. if (sel.rangeCount > 0) {
  106. range = sel.getRangeAt(0);
  107. }
  108. } else {
  109. // Old WebKit selection object has no getRangeAt, so
  110. // create a range from other selection properties
  111. range = document.createRange();
  112. range.setStart(sel.anchorNode, sel.anchorOffset);
  113. range.setEnd(sel.focusNode, sel.focusOffset);
  114.  
  115. // Handle the case when the selection was selected backwards (from the end to the start in the document)
  116. if (range.collapsed !== sel.isCollapsed) {
  117. range.setStart(sel.focusNode, sel.focusOffset);
  118. range.setEnd(sel.anchorNode, sel.anchorOffset);
  119. }
  120. }
  121.  
  122. if (range) {
  123. /*** This function is generic and unedited except for the part between here and the next comment. Remove this section to always return the actual getSelectionContainerElement. ***/
  124. if (range.startContainer.parentNode.nodeName == "BLOCKQUOTE" || range.endContainer.parentNode.nodeName == "BLOCKQUOTE") {
  125. container = range.commonAncestorContainer.parentNode; // we want to ensure we don't return the true parent div so that later checks will fail
  126. }
  127. else
  128. /*** edited section ends here ***/
  129. container = range.commonAncestorContainer;
  130.  
  131. // Check if the container is a text node and return its parent if so
  132. return container.nodeType === 3 ? container.parentNode : container;
  133. }
  134. }
  135. }
  136. /*function doQuickQuote(obj) {
  137. // can't use $ in here because it's called after we've called jQuery.noConflict
  138. var addNewLines = false;
  139. var selText = document.getSelection().toString();
  140. var commentDiv = jQuery(obj).parent().parent().parent().parent().find('td.body > div').eq(0);
  141. var ajaxResponse;
  142. postID = commentDiv.attr("id").match(/content(\d*)/)[1];
  143. poster = jQuery(obj).parent().find('strong > a').eq(0).text();
  144. //console.log(poster);
  145. jQuery.ajax({
  146. url: "?action=get_post&post=" + postID,
  147. type: 'GET',
  148. async: false,
  149. success: function(response) {
  150. ajaxResponse = html_entity_decode(response); //not sure if this needs to happen but Quote() in comments.js does it, so why not be safe?
  151. }
  152. });
  153. elem = getSelectionContainerElement();
  154. if (selText == "" || selText == null || elem != commentDiv[0]) {
  155. selText = ajaxResponse;
  156. }
  157. textArea = jQuery('#quickpost');
  158. if (textArea.val() !== '')
  159. addNewLines = true;
  160. textArea.val(textArea.val() + (addNewLines ? "\n\n" : "") + "[quote="+poster+"|"+postID+"]" + selText.trim() + "[/quote]");
  161. }*/
  162. jQuery.noConflict();
  163. }
  164. // load jQuery and execute the main function
  165. if( /opera/i.test(navigator.userAgent)) {
  166. console.log("What.CD Quick Quote: If this script is not working in Opera, make sure the filename ends in user.js");
  167. addJQuery(main);
  168. }
  169. else if( ! /firefox/i.test(navigator.userAgent) ) { // chrome and safari
  170. addJQuery(main);
  171. }
  172. else {
  173. this.$ = this.jQuery = jQuery.noConflict(true);
  174. main();
  175. }
  176. }