What.CD Quick Quote

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

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

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