powdertoy.co.uk Confirm Discarding A Long Post

Shows a confirmation before accidentally discarding a long forum post.

  1. // ==UserScript==
  2. // @name powdertoy.co.uk Confirm Discarding A Long Post
  3. // @namespace http://boxmein.net/
  4. // @version 1.2.1
  5. // @description Shows a confirmation before accidentally discarding a long forum post.
  6. // @match http://powdertoy.co.uk/Discussions/Thread/View.html*
  7. // @match http://powdertoy.co.uk/Discussions/Thread/Create.html*
  8. // @match http://powdertoy.co.uk/Conversations/View.html*
  9. // @match http://powdertoy.co.uk/Conversations/New.html*
  10. // @match http://powdertoy.co.uk/Groups/Thread/View.html*
  11. // @match http://powdertoy.co.uk/Groups/Thread/Create.html*
  12. // @copyright 2015 boxmein / apache2 license
  13. // ==/UserScript==
  14.  
  15. // http://wiki.greasespot.net/Content_Script_Injection
  16. function contentEval(source) {
  17. if ('function' == typeof source) {
  18. source = '(' + source + ')();';
  19. }
  20.  
  21. var script = document.createElement('script');
  22. script.setAttribute("type", "application/javascript");
  23. script.textContent = source;
  24.  
  25. document.body.appendChild(script);
  26. document.body.removeChild(script);
  27. }
  28.  
  29.  
  30. contentEval(function() {
  31. $ = ($||window.$||jQuery||window.jQuery);
  32.  
  33. //
  34. // Configlumaration
  35. //
  36.  
  37. // should I show debug logs?
  38. // Default: false
  39. const DEBUG = false;
  40.  
  41. // do I ignore when you leave the page by submitting the topic?
  42. // Default: true
  43. const IGNORE_SUBMIT = true;
  44.  
  45. // do I copy the text to the clipboard just to be safe before you leave?
  46. // Default: false
  47. const COPY_TO_CLIPBOARD = false;
  48.  
  49. // how long does the post have to be in characters to be considered long?
  50. // Default: 400
  51. const POST_THRESHOLD = 400;
  52.  
  53. // what do I tell the user to confirm if they want to leave?
  54. // Default: "You sure you wanna leave? Seems you have a really tall post typed up."
  55. const CONFIRM_MESSAGE = "You sure you wanna leave? Seems you have a really tall post typed up.";
  56.  
  57. //
  58. // Actual Code
  59. // Configuration variables end here. Only edit if you know what you're doing :D
  60. //
  61.  
  62. // what kinds of edit boxes do we know about?
  63. const possibleEditBoxes = {
  64. '/Discussions/Thread/Create.html': ['.EditPlain:first'],
  65. '/Discussions/Thread/View.html': ['#AddReplyMessage'],
  66. '/Conversations/View.html': ['textarea[name="Conversation_Reply"]:first'],
  67. '/Groups/Thread/View.html': ['#AddReplyMessage'],
  68. '/Groups/Thread/Create.html': ['textarea[name="Thread_Message']
  69. };
  70.  
  71. // what kinds of post buttons do we know about?
  72. const possiblePostBtns = {
  73. '/Discussions/Thread/Create.html': ['.btn[name="Thread_Post"]:first'],
  74. '/Discussions/Thread/View.html': ['#AddReplyPost'],
  75. '/Conversations/View.html': ['.PostForm .btn[value="Send"]:first'],
  76. '/Groups/Thread/View.html': ['#AddReplyPost'],
  77. '/Groups/Thread/Create.html': ['.SubmitF input[name="Thread_Post"]:first']
  78. };
  79.  
  80. if (DEBUG) debugger;
  81.  
  82. if (DEBUG) console.log('attaching to onbeforeunload to confirm discarding >400-long posts');
  83. var iTotallyActuallyMeanToPost = false;
  84. var pathname = window.location.pathname;
  85.  
  86. // try all the selectors in order until one matches
  87. function findElement(possible_selectors) {
  88. for (var i = 0, ps = possible_selectors; i < ps.length; i ++) {
  89. var $temp = $(ps[i]);
  90. if ($temp.length !== 0 && $temp[0]) {
  91. return $temp[0];
  92. }
  93. }
  94. }
  95.  
  96. window.onbeforeunload = function() {
  97.  
  98. if (DEBUG) console.log('beforeunload!');
  99.  
  100. if (!possibleEditBoxes[pathname]) {
  101. console.error('we don\'t know if there\'s an edit box we care about on this page!');
  102. if (DEBUG) console.log ('(i looked for ' + pathname + ' in possibleEditBoxes, which only has [' + Object.keys(possibleEditBoxes).join(', ') + '])');
  103. return;
  104. }
  105.  
  106. var editBoxes = possibleEditBoxes[pathname];
  107. var el = findElement(editBoxes);
  108. if (el === undefined) {
  109. console.error('did not fid a post edit box!');
  110. if (DEBUG) console.log ('(i looked for ' + editBoxes.join(', ') + ')');
  111. }
  112. else if (el.value.length > POST_THRESHOLD &&
  113. !iTotallyActuallyMeanToPost) {
  114. if (DEBUG) console.log('post too long, gonna check with the user');
  115. if (COPY_TO_CLIPBOARD) {
  116. if (DEBUG) console.log('copying the text to clipboard...');
  117. el.select();
  118. document.execCommand("copy");
  119. }
  120.  
  121. return CONFIRM_MESSAGE;
  122. }
  123.  
  124. };
  125.  
  126. // attaching to post button to disable the onbeforeunload thing happening
  127. // when you actually mean to post
  128. if (IGNORE_SUBMIT) {
  129.  
  130. if (!possiblePostBtns[pathname]) {
  131. console.error("we don't really know if there's a post button on this page! :(");
  132. if (DEBUG) console.log ('(i looked for ' + pathname + ' in possiblePostBtns, which only has [' + Object.keys(possiblePostBtns).join(', ') + '])');
  133. return;
  134. }
  135.  
  136. var postBtns = possiblePostBtns[pathname];
  137. var $postBtn = findElement(postBtns);
  138.  
  139. if ($postBtn === undefined) {
  140. console.error('did not find a post button in this page! :(');
  141. if (DEBUG) console.log ('(i looked for ' + postBtns.join(', ') + ')');
  142. return;
  143. }
  144.  
  145. $($postBtn).click(function() {
  146. if (DEBUG) console.log('user clicked the post button! letting them past');
  147. iTotallyActuallyMeanToPost = true;
  148. });
  149.  
  150. }
  151. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址