LOR spoiler

Add spoiler functionality

  1. // ==UserScript==
  2. // @name LOR spoiler
  3. // @namespace linux.org.ru
  4. // @description Add spoiler functionality
  5. // @include https://linux.org.ru/*
  6. // @include https://www.linux.org.ru/*
  7. // @version 2
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. $script.ready('jquery',function(){
  12. console.log('LOR spoiler is ON');
  13.  
  14. // spoiler
  15. var types = ['cut', 'code', 'pre'];
  16. var blocks = {
  17. cut: $('[id ^= cut]'),
  18. code: $('.code'),
  19. pre: $('pre:not([class])')
  20. };
  21.  
  22. var total_block_cnt = blocks.cut.length + blocks.code.length + blocks.pre.length;
  23. var line_limit = total_block_cnt > 2 ? 5 : 15;
  24.  
  25. var spoiler_prefix_on = '>>> ';
  26. var spoiler_prefix_off = '<<< ';
  27.  
  28. var tpl =
  29. '<span class="sign">'+
  30. '<span>'+ spoiler_prefix_on +'</span>'+
  31. '<a '+
  32. 'id="spoiler-hide-{TYPE}_{ID}" '+
  33. 'href="javascript:void(0);" '+
  34. 'onClick="javascript:var block=$(\'#hide-{TYPE}_{ID}\'); var prefix=this.previousElementSibling;'+
  35. 'if (block.css(\'display\')===\'none\') {'+
  36. 'block.show(); prefix.innerText=\''+spoiler_prefix_off+'\'; } else {'+
  37. 'block.hide(); prefix.innerText=\''+spoiler_prefix_on+'\'; };">'+
  38. '{TYPE}-spoiler'+
  39. '</a>'+
  40. '</span><br/>';
  41.  
  42. // change content
  43. if (total_block_cnt > 0) {
  44. for (var i = 0; i < types.length; i++) {
  45. var TYPE = types[i];
  46. var ID = 0;
  47. blocks[TYPE].each(function() {
  48. // limit
  49. var no_hl = $(this).find('pre.no-highlight code');
  50. var cur_blk = no_hl.length > 0 ? no_hl : $(this);
  51. if (cur_blk.text().split("\n").length <= line_limit) return;
  52.  
  53. // add spoiler
  54. var spoiler = tpl.replace(/\{TYPE\}/g, TYPE).replace(/\{ID\}/g, ID);
  55. $(this).attr('id','hide-'+TYPE+'_'+ ID).hide();
  56. $(this).before(spoiler);
  57. ID++;
  58. });
  59. }
  60. }
  61. });