sig easy copy

easy copying on sig

当前为 2015-04-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name sig easy copy
  3. // @author Nick Hall
  4. // @namespace http://soitgo.es
  5. // @include https://soitgo.es/
  6. // @include https://soitgo.es/?*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
  8. // @version 1.1.2
  9. // @description easy copying on sig
  10. // ==/UserScript==
  11. $(document).ready(function()
  12. {
  13. easycopy.init();
  14. });
  15.  
  16. var easycopy = {
  17. init: function()
  18. {
  19. $('#links .date').append('<span class="easycopy"><a href="#" class="ezc-links">x</a> <a href="#" class="ezc-thumbs">+</a></span>');
  20. $('.easycopy a.ezc-links').click(easycopy.handleClick);
  21. $('#links').before('<div id="ezc-textcontainer" style="height: 100px; clear: both; margin: 0 20px 20px 20px; padding: 0 !important;"><textarea id="easycopytext" style="width: 922px; height: 98px; padding: 0 !important; margin: 0 !important;">Click the x to the right of each link to get started.\n</textarea></div><div id="ezc-loading" style="position: fixed; bottom: 0; right: 0;">Loading...</div>');
  22. $('.easycopy a.ezc-thumbs').click(easycopy.thumbsClick);
  23. $('#ezc-loading').hide();
  24. $('#easycopytext').focus(function() {$(this).select();});
  25.  
  26. //Comment this out if you don't want the top link I guess
  27. $('body').append('<div id="toTheTop" style="position: fixed; left: 5px; bottom: 5px;"><a href="#">top</a></div>');
  28. $('#toTheTop').click(function(e)
  29. {
  30. e.preventDefault();
  31. window.scrollTo(0, 0);
  32. });
  33.  
  34. // Another div for status messages
  35. $('body').append('<div id="ezc-status" style="position: fixed; right: 5px; top: 5px; background-color: #191919 !important; color: white !important; padding: 5px !important;">This should never be seen</div>');
  36. $('#ezc-status').hide();
  37.  
  38. MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
  39. var observer = new MutationObserver(function(mutations)
  40. {
  41. mutations.forEach(function(mutation)
  42. {
  43. if (typeof mutation.addedNodes == "object")
  44. {
  45. $(mutation.addedNodes).find('.date').each(function()
  46. {
  47. $(this).append(' <span class="easycopy"><a href="#" class="ezc-links">x</a> <a href="#" class="ezc-thumbs">+</a></span>');
  48. $(this).find('.ezc-links').click(easycopy.handleClick);
  49. });
  50. }
  51. });
  52. });
  53.  
  54. // define what element should be observed by the observer
  55. observer.observe(document, {
  56. subtree: true,
  57. childList: true
  58. });
  59.  
  60. // Set up moving the text area on scroll
  61. var textareaInitialPosition = $('#easycopytext').offset().top - 10;
  62. $(window).scroll(function()
  63. {
  64. if ($(window).scrollTop() > textareaInitialPosition)
  65. {
  66. $('#easycopytext').css('position', 'fixed');
  67. $('#easycopytext').css('top', '10px');
  68. }
  69. else
  70. {
  71. $('#easycopytext').css('position', '');
  72. $('#easycopytext').css('top', '');
  73. }
  74. });
  75.  
  76. // Throw in some custom CSS for voting links
  77. $('head').append('<style type="text/css">\
  78. .ezc-flood {\
  79. color: orange !important;\
  80. }\
  81. .ezc-voted {\
  82. color: green !important;\
  83. }\
  84. .ezc-alreadyvoted {\
  85. color: red !important;\
  86. }\
  87. .easycopy button {\
  88. margin: 0 !important;\
  89. }\
  90. </style>');
  91.  
  92. // Show/hide the link box if we want
  93. $('#middlebar a:last').after('<button id="ezc-linkToggle">Link Box</button>');
  94. $('#ezc-linkToggle').click(function(e)
  95. {
  96. $('#ezc-textcontainer').toggle('fast');
  97. });
  98. },
  99.  
  100. handleClick: function(event)
  101. {
  102. event.preventDefault();
  103. var url = $(this).parent().parent().prev().find('.title').parent().attr('href'); // This was being really finnicky
  104. $(this).parent().parent().prev().find('.title').css("cssText", "color: #FF0000 !important;");
  105. easycopy.enqueue();
  106. $.get(url, easycopy.populateURLBox);
  107. $(this).remove();
  108. },
  109.  
  110. thumbsClick: function(event)
  111. {
  112. event.preventDefault();
  113. var linkID = $(this).parent().parent().prev().find('.title').parent().attr('href').split(':');
  114. var currentLink = $(this);
  115. if (currentLink.hasClass('ezc-alreadyvoted') || currentLink.hasClass('ezc-voted'))
  116. {
  117. easycopy.displayMessage("Already voted");
  118. return; // Don't pester the server if we've already voted
  119. }
  120. easycopy.enqueue('vote');
  121. $.ajax('ajax.php?i=link&thank=' + linkID[1]).done(function (msg)
  122. {
  123. currentLink.removeClass('ezc-flood');
  124. currentLink.removeClass('ezc-voted');
  125. currentLink.removeClass('ezc-alreadyvoted');
  126. if (msg == 'flood')
  127. {
  128. currentLink.addClass('ezc-flood');
  129. easycopy.displayMessage("Please wait");
  130. console.log("FLOOD!!!");
  131. }
  132. else if (msg)
  133. {
  134. easycopy.displayMessage("Vote successful");
  135. currentLink.addClass('ezc-voted');
  136. console.log("VOTED");
  137. }
  138. else
  139. {
  140. easycopy.displayMessage("Already voted");
  141. currentLink.addClass('ezc-alreadyvoted');
  142. console.log("Already voted");
  143. }
  144. easycopy.dequeue();
  145. }
  146. );
  147. },
  148.  
  149. populateURLBox: function(data)
  150. {
  151. var linkData = "";
  152. $(data).find('#links_mega a').each(function() { linkData += $(this).attr('href') + '\n' });
  153. $('#easycopytext').val($('#easycopytext').val() + linkData);
  154. easycopy.dequeue();
  155. },
  156.  
  157. loadingCount: 0,
  158.  
  159. enqueue: function(type)
  160. {
  161. easycopy.loadingCount++;
  162. if (type !== 'vote')
  163. {
  164. $('#easycopytext').prop('disabled', true);
  165. $('#easycopytext').css('user-select', 'none');
  166. }
  167. $('#ezc-loading').show('fast');
  168. },
  169.  
  170. dequeue: function(type)
  171. {
  172. easycopy.loadingCount--;
  173. if (easycopy.loadingCount === 0)
  174. {
  175. if (type !== 'vote')
  176. {
  177. $('#easycopytext').prop('disabled', false);
  178. $('#easycopytext').css('user-select', 'all');
  179. }
  180. $('#ezc-loading').hide('slow');
  181. }
  182. },
  183.  
  184. displayMessage: function(message)
  185. {
  186. $('#ezc-status').text(message);
  187. $('#ezc-status').show('fast').delay(3000).fadeOut();
  188. }
  189. }