Mefiquote (updated for redesign)

Adds "quote" links to Metafilter comments. Updated for the 2014 redesign.

当前为 2018-05-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Mefiquote (updated for redesign)
  3. // @namespace http://plutor.org/
  4. // @description Adds "quote" links to Metafilter comments. Updated for the 2014 redesign.
  5. // @include https://metafilter.com/*
  6. // @include https://*.metafilter.com/*
  7. // @version 0.0.1.20180505182510
  8. // ==/UserScript==
  9.  
  10. //
  11. // DONE 2011-02-23
  12. // * Use MeFi's own jquery object (properly this time)
  13. // * Use a content scope injector instead of unsafeWindow
  14. // * Handle new ajax comments - big thanks to pb
  15. //
  16. // TODO
  17. // * Indicate when "quote" will quote the selection
  18. // * Make it work with epiphany and opera
  19. // * Ability to link to the original post on preview
  20.  
  21. // Content Scope
  22. var script = document.createElement('script');
  23. script.appendChild(document.createTextNode('('+ everything.toString() +')();'));
  24. script.setAttribute("type", "application/javascript");
  25. (document.body || document.head || document.documentElement).appendChild(script);
  26.  
  27. /* ======================================================================== */
  28. function everything() {
  29. var BUTTONTEXT = 'quote';
  30. var QUOTEFORMAT = '<a href="%l">%n</a>: "<i>%q</i>"';
  31.  
  32. /* ======================================================================== */
  33.  
  34. function mq_quotethis(evt) {
  35. var commenttextarea = $("#comment");
  36. if (commenttextarea.length < 1) return;
  37.  
  38. var quotelink = $(this);
  39. var metadata = quotelink.parent();
  40. var comment = metadata.parent();
  41.  
  42.  
  43. // Get all of the data to fill in placeholders
  44. var quotebits = new Object;
  45. quotebits['%'] = '%';
  46.  
  47. if (mq_selection_within_comment(comment)) {
  48. quotebits.q = document.getSelection().toString();
  49. } else {
  50. quotebits.q = new String(comment.html());
  51. quotebits.q = quotebits.q.replace(/<br>/ig, '');
  52.  
  53. // Remove the trailing metadata
  54. if (quotebits.q.lastIndexOf('<span class="smallcopy">posted by') > -1)
  55. quotebits.q = quotebits.q.slice(0,
  56. quotebits.q.lastIndexOf('<span class="smallcopy">posted by'));
  57.  
  58. // Remove the player from music
  59. if (quotebits.q.lastIndexOf('<object ') > -1)
  60. quotebits.q = quotebits.q.slice(0,
  61. quotebits.q.lastIndexOf('<object '));
  62.  
  63. // Remove the more inside junk
  64. quotebits.q = quotebits.q.replace(/<\/?div[^>]*>/g, '');
  65. quotebits.q = quotebits.q.replace(/^[ \t\n]*/, '');
  66. quotebits.q = quotebits.q.replace(/[ \t\n]*$/, '');
  67. }
  68.  
  69. // Default to top of the thread, just in case
  70. quotebits.l = "" + location.protocol + "//" + location.host + location.pathname;
  71.  
  72. // The rest of the data
  73. metadata.children('a').each( function(i) {
  74. var url = $(this).attr('href');
  75. var path = url.replace(/https?:\/\/([^\/]*\.)?metafilter.com/, '');
  76. if (url == path && path.match('^/'))
  77. url = "" + location.protocol + "//" + location.host + path;
  78.  
  79. if (path.match(/^\/user\/(\d+)/)) {
  80. quotebits.i = RegExp.$1;
  81. quotebits.n = $(this).html();
  82. quotebits.n = quotebits.n.replace(/<.*/, '');
  83. quotebits.p = url;
  84. } else if (path.match(/#\d+$/)) {
  85. quotebits.l = url;
  86. }
  87. } );
  88.  
  89. // Replace all of the placeholders
  90. var quoteregex = new RegExp('%(.)', 'g');
  91. var quotehtml = new String();
  92. var lastIndex = 0;
  93. while ( quoteregex.exec(QUOTEFORMAT) ) {
  94. var thisIndex = quoteregex.lastIndex;
  95. quotehtml = quotehtml.concat( QUOTEFORMAT.substr(lastIndex, thisIndex-lastIndex-2) );
  96. var val = quotebits[QUOTEFORMAT.substr(thisIndex-1, 1)];
  97. if (val != undefined) {
  98. quotehtml = quotehtml.concat( quotebits[QUOTEFORMAT.substr(thisIndex-1, 1)] );
  99. } else {
  100. quotehtml = quotehtml.concat( '%' + QUOTEFORMAT.substr(thisIndex-1, 1) );
  101. }
  102.  
  103. lastIndex = thisIndex;
  104. }
  105. quotehtml = quotehtml.concat( QUOTEFORMAT.substr(lastIndex) );
  106.  
  107. // GM_log( quotehtml );
  108. var commentval = commenttextarea.val() || "";
  109. if (commentval != "" && !commentval.match(/\n\n$/)) {
  110. commentval += "\n\n";
  111. }
  112. commentval += quotehtml + "\n\n";
  113.  
  114. commenttextarea.val(commentval);
  115. }
  116.  
  117. /* ======================================================================== */
  118.  
  119. function mq_load_preferences() {
  120. BUTTONTEXT = Cookie.get('mefiquote_buttontext') || BUTTONTEXT;
  121. QUOTEFORMAT = Cookie.get('mefiquote_quoteformat') || QUOTEFORMAT;
  122. }
  123.  
  124. function mq_save_preferences() {
  125. var buttontext_el = $('#mq_buttontext');
  126. var quoteformat_el = $('#mq_quoteformat');
  127.  
  128. Cookie.set('mefiquote_buttontext', (buttontext_el.val() || BUTTONTEXT),
  129. 24*365*10, '', 'metafilter.com', false);
  130. Cookie.set('mefiquote_quoteformat', (quoteformat_el.val() || QUOTEFORMAT),
  131. 24*365*10, '', 'metafilter.com', false);
  132.  
  133. return true; /* So it actually submits, too */
  134. }
  135.  
  136. /* ======================================================================== */
  137.  
  138. function mq_escape(str) {
  139. return str.replace(/"/g, '&quot;');
  140. }
  141.  
  142. /* ======================================================================== */
  143.  
  144. function mq_selection_within_comment(comment) {
  145. var selection = window.getSelection();
  146.  
  147. // an empty selection doesn't count as being within the comment, otherwise
  148. // the quote button tries to quote it and things behave counterintuitively
  149. if (selection == null || selection.isCollapsed) {
  150. return false;
  151. }
  152.  
  153. // check to see if the selection is inside this comment
  154. var rangeStart = selection.getRangeAt(0).startContainer;
  155. var rangeEnd = selection.getRangeAt(0).endContainer;
  156.  
  157. if (rangeStart != null && rangeEnd != null) {
  158. var start_found = false;
  159. var end_found = false;
  160.  
  161. // TODO - Why doesn't parents().index() do the right thing?
  162. $(rangeStart).parents().andSelf().each( function() {
  163. if (this == comment.get(0)) start_found = true;
  164. });
  165. $(rangeEnd).parents().andSelf().each( function() {
  166. if (this == comment.get(0)) end_found = true;
  167. });
  168.  
  169. return (start_found && end_found);
  170. }
  171. return false;
  172. }
  173.  
  174. /* ======================================================================== */
  175. function mq_init_preferences() {
  176. var inputs = $('input');
  177. var submit_button = $('input[type=submit]').filter( function() {
  178. return $(this).val().match(/Save your Preferences/);
  179. } );
  180. if (inputs.length < 1 || submit_button.length < 1) return;
  181.  
  182. // Create the fieldset
  183. var mefiquote_fieldset = $('<fieldset>'
  184. + '<legend>MefiQuote preferences</legend>'
  185. + '<label for="mq_buttontext">Quote button text: </label>'
  186. + '<input type="text" id="mq_buttontext" name="mq_buttontext" value="'
  187. + mq_escape(BUTTONTEXT)
  188. + '" maxlength="200" size="30" onfocus="this.style.background=\'#ddd\';" onblur="this.style.background=\'#ccc\';" /><br />'
  189. + '<label for="mq_quoteformat">Quote format:<br />'
  190. + '<span class="smallcopy" style="text-align: left">%i - commenter\'s user id<br />%l - url of comment<br />%n - commenter\'s name<br />%p - url of commenter\'s profile<br />%q - comment text<br />%% - an actual percent ("%")</span></label>'
  191. + '<textarea name="mq_quoteformat" id="mq_quoteformat" cols="60" rows="8" wrap="VIRTUAL" style="width:400px;height:200px;" onfocus="this.style.background=\'#ddd\';" onblur="this.style.background=\'#ccc\';">'
  192. + mq_escape(QUOTEFORMAT)
  193. + '</textarea>'
  194. + '</fieldset>')
  195. .insertBefore(submit_button);
  196.  
  197. // Add javascript to the form
  198. submit_button.parents('form').submit( mq_save_preferences );
  199. }
  200.  
  201. function mq_init_thread() {
  202. console.log("init-thread");
  203.  
  204. var commenttextarea = $("#comment");
  205. if (commenttextarea.length < 1) return;
  206.  
  207. var n = 0;
  208. $('span').each( function(i) {
  209. var curr = $(this);
  210. if (curr.hasClass('smallcopy') && curr.html().match(/^posted by/) &&
  211. curr.parents('#prevDiv2, form').length == 0 &&
  212. curr.find('.quotebutton').length == 0) {
  213. // Skip the first (post) quote link on preview
  214. if (location.pathname.match('^/contribute/post_comment_preview.mefi') && n++ == 0)
  215. return;
  216.  
  217. // Add the button
  218. var quotebutton = $('<a href="#comment">' + BUTTONTEXT + '</a>')
  219. .attr('target', '_self')
  220. .addClass('quotebutton');
  221.  
  222. curr.append(' [').append( quotebutton ).append(']');
  223. }
  224. } );
  225. }
  226.  
  227. function mq_init_newcomments() {
  228. $("#newcomments").bind('mefi-comments',
  229. function() {
  230. console.log("event!");
  231. mq_init_thread();
  232. } );
  233. console.log("init-newcomments");
  234. }
  235.  
  236. /**
  237. * Modified from cookie-js 0.4 by Maxime Haineault (max@centdessin.com)
  238. * <http://code.google.com/p/cookie-js/>
  239. */
  240. Cookie = {
  241. /** Get a cookie's value */
  242. get: function(key) {
  243. // Still not sure that "[a-zA-Z0-9.()=|%/_]+($|;)" match *all* allowed characters in cookies
  244. tmp = document.cookie.match((new RegExp(key +'=[a-zA-Z0-9.()=|%/_]+($|;)','g')));
  245. if(!tmp || !tmp[0]) return null;
  246. else return unescape(tmp[0].substring(key.length+1,tmp[0].length).replace(';','')) || null;
  247. },
  248. /** Set a cookie */
  249. set: function(key, value, ttl, path, domain, secure) {
  250. cookie = [key+'='+ escape(value),
  251. 'path='+ ((!path || path=='') ? '/' : path),
  252. 'domain='+ ((!domain || domain=='')? window.location.host : domain)];
  253. if (ttl) cookie.push('expires=' + Cookie.hoursToExpireDate(ttl));
  254. if (secure) cookie.push('secure');
  255. return document.cookie = cookie.join('; ');
  256. },
  257. /** Unset a cookie */
  258. unset: function(key, path, domain) {
  259. path = (!path || typeof path != 'string') ? '' : path;
  260. domain = (!domain || typeof domain != 'string') ? '' : domain;
  261. if (Cookie.get(key)) Cookie.set(key, '', 'Thu, 01-Jan-70 00:00:01 GMT', path, domain);
  262. },
  263.  
  264. /** Return GTM date string of "now" + time to live */
  265. hoursToExpireDate: function(ttl) {
  266. if (parseInt(ttl) == 'NaN' ) return '';
  267. else {
  268. now = new Date();
  269. now.setTime(now.getTime() + (parseInt(ttl) * 60 * 60 * 1000));
  270. return now.toGMTString();
  271. }
  272. }
  273. }
  274.  
  275. function mq_init() {
  276. mq_load_preferences();
  277.  
  278. var url = location.pathname;
  279.  
  280. if (url.match(/^\/(\d+)/) || url.match('^/contribute/post_comment_preview.mefi')) {
  281. mq_init_thread();
  282. mq_init_newcomments();
  283.  
  284. // Attach a listener to clicks -- just once
  285. $(".content").on("click", "a.quotebutton", mq_quotethis);
  286. } else if (url.match('^/contribute/customize.cfm')) {
  287. mq_init_preferences();
  288. }
  289. }
  290.  
  291. mq_init();
  292. }