Advertisement
Guest User

Untitled

a guest
Dec 17th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.   var translate = {cut:'текст', code:'код', pre:'блок'};
  22.  
  23.   var total_block_cnt = blocks.cut.length + blocks.code.length + blocks.pre.length;
  24.   var line_limit = total_block_cnt > 2 ? 5 : 15;
  25.  
  26.   var spoiler_prefix_on = 'Показать';
  27.   var spoiler_prefix_off = 'Скрыть';
  28.  
  29.   var tpl =
  30.     '<p>'+
  31.       '<span>&gt;&gt;&gt;&nbsp;</span>'+
  32.       '<a class="tag"'+
  33.       'id="spoiler-hide-{TYPE}_{ID}" '+
  34.       'href="javascript:void(0);" '+
  35.       'onClick="javascript:var block=$(\'#hide-{TYPE}_{ID}\');'+
  36.         'if (block.css(\'display\')===\'none\') {'+
  37.           'block.show(); this.innerText=this.innerText.replace(\''+spoiler_prefix_on+'\', \''+spoiler_prefix_off+'\'); } else {'+
  38.           'block.hide(); this.innerText=this.innerText.replace(\''+spoiler_prefix_off+'\', \''+spoiler_prefix_on+'\'); };">'+
  39.       spoiler_prefix_on + '&nbsp;{TYPE:tr}'+
  40.       '</a>'+
  41.     '</p>';
  42.  
  43.   // change content
  44.   if (total_block_cnt > 0) {
  45.     for (var i = 0; i < types.length; i++) {
  46.       var TYPE = types[i];
  47.       var ID = 0;
  48.      
  49.       blocks[TYPE].each(function() {
  50.         // limit
  51.         var no_hl = $(this).find('pre.no-highlight code');
  52.         var cur_blk = no_hl.length > 0 ?  no_hl : $(this);
  53.         if (cur_blk.text().split("\n").length <= line_limit) return;
  54.  
  55.         // add spoiler
  56.         var spoiler = tpl.replace(/\{TYPE\}/g, TYPE).replace(/\{ID\}/g, ID).replace(/\{TYPE:tr\}/g, translate[TYPE]);
  57.         $(this).attr('id','hide-'+TYPE+'_'+ ID).hide();
  58.         $(this).before(spoiler);
  59.  
  60.         ID++;
  61.       });
  62.     }
  63.   }
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement