osu! select code block

Highlights Code Blocks

  1. // ==UserScript==
  2. // @name osu! select code block
  3. // @namespace s4nji
  4. // @description Highlights Code Blocks
  5. // @include https://osu.ppy.sh/forum/*
  6. // @version 1
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. /*\
  11. * Creates a selection around the node
  12. \*/
  13.  
  14. window.onload = function() {
  15.  
  16. // Select Node | Credits to Álvaro G. Vicario- http://stackoverflow.com/a/4012861
  17. function selectNode(myNode){
  18. // Create a range
  19. try{ // FF
  20. var myRange = document.createRange();
  21. }catch(e){
  22. try{ // IE
  23. var myRange = document.body.createTextRange();
  24. }catch(e){
  25. return;
  26. }
  27. }
  28.  
  29. // Asign text to range
  30. try{ // FF
  31. myRange.selectNode(myNode);
  32. }catch(e){
  33. try{ // IE
  34. myRange.moveToElementText(myNode);
  35. }catch(e){
  36. return;
  37. }
  38. }
  39.  
  40. // Select the range
  41. try{ // FF
  42. var mySelection = window.getSelection();
  43. mySelection.removeAllRanges(); // Undo current selection
  44. mySelection.addRange(myRange);
  45. }catch(e){
  46. try{ // IE
  47. myRange.select();
  48. }catch(e){
  49. return;
  50. }
  51. }
  52. }
  53.  
  54. // Add select code links/buttons next to code titles
  55. var el = " <a href='#' class='select-code'>[Select Code]</a>";
  56. jQuery('.codetitle').append(el);
  57.  
  58. // Select Code Block near clicked select code links/buttons
  59. jQuery('.select-code').click( function(e) {
  60. e.preventDefault();
  61. selectNode( jQuery(this).parent().next()[0] );
  62. });
  63.  
  64. };