reddit cubing competition result helper

Select a score and press "c" to show a text-box with the markdown needed for making the result list.

  1. // ==UserScript==
  2. // @name reddit cubing competition result helper
  3. // @namespace http://www.mathemaniac.org
  4. // @version 1.0
  5. // @description Select a score and press "c" to show a text-box with the markdown needed for making the result list.
  6. // @match http://www.reddit.com/r/Cubers/comments/*/cubing_competition*/
  7. // @copyright 2013-2017, Sebastian Paaske Tørholm
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
  9. // ==/UserScript==
  10.  
  11. $('html').keypress(function (event) {
  12. if (event.shiftKey || event.altKey || event.ctrlKey) return;
  13.  
  14. if (event.charCode != 99) return; // 'c'
  15.  
  16. var selection = window.getSelection();
  17. if (selection.rangeCount == 0) return;
  18.  
  19. var p = selection.getRangeAt(0).startContainer.parentNode;
  20. var post = $(p).closest('div.entry');
  21.  
  22. var username = $('a.author', post)[0].innerText;
  23.  
  24. var line = '1. ' + selection.toString() + " - ["+username+"](/u/"+username+")";
  25.  
  26. $(post).append('<div><input type="text" value="'+line+'" style="width: 400px;"/></div>');
  27. });