Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name LOR spoiler
- // @namespace linux.org.ru
- // @description Add spoiler functionality
- // @include https://linux.org.ru/*
- // @include https://www.linux.org.ru/*
- // @version 2
- // @grant none
- // ==/UserScript==
- $script.ready('jquery',function(){
- console.log('LOR spoiler is ON');
- // spoiler
- var types = ['cut', 'code', 'pre'];
- var blocks = {
- cut: $('[id ^= cut]'),
- code: $('.code'),
- pre: $('pre:not([class])')
- };
- var translate = {cut:'текст', code:'код', pre:'блок'};
- var total_block_cnt = blocks.cut.length + blocks.code.length + blocks.pre.length;
- var line_limit = total_block_cnt > 2 ? 5 : 15;
- var spoiler_prefix_on = 'Показать';
- var spoiler_prefix_off = 'Скрыть';
- var tpl =
- '<p>'+
- '<span>>>> </span>'+
- '<a class="tag"'+
- 'id="spoiler-hide-{TYPE}_{ID}" '+
- 'href="javascript:void(0);" '+
- 'onClick="javascript:var block=$(\'#hide-{TYPE}_{ID}\');'+
- 'if (block.css(\'display\')===\'none\') {'+
- 'block.show(); this.innerText=this.innerText.replace(\''+spoiler_prefix_on+'\', \''+spoiler_prefix_off+'\'); } else {'+
- 'block.hide(); this.innerText=this.innerText.replace(\''+spoiler_prefix_off+'\', \''+spoiler_prefix_on+'\'); };">'+
- spoiler_prefix_on + ' {TYPE:tr}'+
- '</a>'+
- '</p>';
- // change content
- if (total_block_cnt > 0) {
- for (var i = 0; i < types.length; i++) {
- var TYPE = types[i];
- var ID = 0;
- blocks[TYPE].each(function() {
- // limit
- var no_hl = $(this).find('pre.no-highlight code');
- var cur_blk = no_hl.length > 0 ? no_hl : $(this);
- if (cur_blk.text().split("\n").length <= line_limit) return;
- // add spoiler
- var spoiler = tpl.replace(/\{TYPE\}/g, TYPE).replace(/\{ID\}/g, ID).replace(/\{TYPE:tr\}/g, translate[TYPE]);
- $(this).attr('id','hide-'+TYPE+'_'+ ID).hide();
- $(this).before(spoiler);
- ID++;
- });
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement