Advertisement
tonyrulez

PH! forum SPOILER gomb v0.6.5 [UserScript]

Nov 9th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        PH! forum SPOILER gomb
  3. // @author      http://prohardver.hu/tag/spammer.html
  4. // @namespace   https://greasyfork.org/users/2358-spammer
  5. // @version     0.6.5
  6. // @description A PH! lapcsalad forumaban uj hozzaszolas irasakor megjelenik a SPOILER gomb
  7. // @match       http://prohardver.hu/muvelet/hsz/*
  8. // @match       http://itcafe.hu/muvelet/hsz/*
  9. // @match       http://gamepod.hu/muvelet/hsz/*
  10. // @match       http://mobilarena.hu/muvelet/hsz/*
  11. // @match       http://logout.hu/muvelet/hsz/*
  12. // @require     http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  13. // ==/UserScript==
  14.  
  15. $(document).ready(function () {
  16.    
  17.     //-----------------------------------------------------//
  18.     //----- 1. Textarea sor kivalaszto --------------------//
  19.     //-----------------------------------------------------//  
  20.  
  21.     function getNumberOfLinesInTextarea(tarea) {
  22.         return (tarea.value.length < 1 ? 0 : tarea.value.split("\n").length);
  23.     }
  24.    
  25.     function selectTextareaLine(tarea, lineNum) {
  26.         lineNum--; // array starts at 0
  27.         var lines = tarea.value.split("\n");
  28.    
  29.         if (lineNum + 1 > lines.length) {
  30.             throw 'You supplied an index which is out of bounds!';
  31.         }
  32.    
  33.         // calculate start/end
  34.         var startPos = 0,
  35.             endPos = tarea.value.length;
  36.         for (var x = 0; x < lines.length; x++) {
  37.             if (x === lineNum) {
  38.                 break;
  39.             }
  40.             startPos += (lines[x].length + 1);
  41.    
  42.         }
  43.    
  44.         endPos = lines[lineNum].length + startPos;
  45.    
  46.         // do selection
  47.         // Chrome / Firefox
  48.         if (typeof (tarea.selectionStart) !== "undefined") {
  49.             tarea.focus();
  50.             tarea.selectionStart = startPos;
  51.             tarea.selectionEnd = endPos;
  52.             return true;
  53.         }
  54.    
  55.         // IE
  56.         if (document.selection && document.selection.createRange) {
  57.             tarea.focus();
  58.             tarea.select();
  59.             var range = document.selection.createRange();
  60.             range.collapse(true);
  61.             range.moveEnd("character", endPos);
  62.             range.moveStart("character", startPos);
  63.             range.select();
  64.             return true;
  65.         }
  66.    
  67.         return false;
  68.     }
  69.    
  70.     //---------------------------------------------//
  71.     //----- 2. Gombok kinezete --------------------//
  72.     //---------------------------------------------//  
  73.  
  74.     var $spoiler_button_CSS = {
  75.      
  76.         "font-family":"Tahoma,Kalimati,sans-serif",
  77.         "font-weight":"bold",
  78.         "min-width":"90px",
  79.         "height":"23px"
  80.  
  81.     }
  82.    
  83.     var $spoiler_button_with_title_CSS = {"width":"120px"};
  84.     var $spoiler_button_with_link_title_CSS = {"width":"180px"};
  85.    
  86.     var $spoiler_button = '<input type="button" value="SPOILER" id="spoiler_button">';
  87.     var $spoiler_button_with_title = '<input type="button" value="SPOILER (+cím)" id="spoiler_button_with_title">';
  88.     var $spoiler_button_with_link_title = '<input type="button" value="SPOILER (+linkesített cím)" id="spoiler_button_with_link_title">';
  89.    
  90.     //-----------------------------------------------------//
  91.     //----- 3. Textarea + gombok beszurasa ----------------//
  92.     //-----------------------------------------------------//  
  93.    
  94.     $('textarea').attr("id","commentbox"); //hozzadunk egy ID-t.
  95.     var $commentbox = $('#commentbox');
  96.     $('.buttons:eq(1)').after('<div class="buttons" id="extra_buttons">'+$spoiler_button + $spoiler_button_with_title + $spoiler_button_with_link_title+'</div>'); //beszurunk egy uj button divet a a gyariak utan (utolso, smiley div ele)
  97.    
  98.     //-----------------------------------------------------------------------------------------------//
  99.     //----- 4. Gombok -------------------------------------------------------------------------------//
  100.     //-----------------------------------------------------------------------------------------------//  
  101.     // A gombok szovegkiemelo funkciojanak megvalositasaert koszonet Sk8erPeternek a segitsegert!----//
  102.     //-----------------------------------------------------------------------------------------------//
  103.    
  104.     $('#spoiler_button').css($spoiler_button_CSS); //ugy nezzenek ki, mint a "gyari" gombok
  105.     $('#spoiler_button_with_title').css($spoiler_button_CSS).css($spoiler_button_with_title_CSS); //ugy nezzen ki, mint a "gyari" gombok + egyeni
  106.     $('#spoiler_button_with_link_title').css($spoiler_button_CSS).css($spoiler_button_with_link_title_CSS); //ugy nezzen ki, mint a "gyari" gombok + egyeni
  107.    
  108.     var tarea = document.getElementById('commentbox');
  109.     var $tarea = $(tarea);
  110.     var $spoiler_block_start = '[B][SPOILER][/B][OFF]';
  111.     var $spoiler_block_end = '[/OFF][B][/SPOILER][/B]';
  112.     var $type_here_placeholder = '----Kezdheted pötyögni a spoileres szöveget----';
  113.     var $prompt_title_text = 'Írd be a címét annak, amiről a spoilert írod (pl. The Sopranos 1x01)';
  114.     var $prompt_link_text = 'Másold be a címhez tartozó linket (pl. IMDB link). Ha mégsem akarsz linket hozzáadni, hagyd üresen és akkor csak a címet fogja beilleszteni.';
  115.  
  116.         //------------------------------------------//
  117.         // --- 4.1. Sima SPOILER gomb megnyomasa ---//
  118.         //------------------------------------------//
  119.         $('#spoiler_button').click(function () {
  120.            
  121.             //beszuras  
  122.             try {
  123.                 var textToInsert =
  124.                   ''
  125.                 + ''
  126.                 + '\n'+$spoiler_block_start
  127.                 + '\n'+$type_here_placeholder
  128.                 + '\n'+$spoiler_block_end;
  129.        
  130.                 $tarea.val($tarea.val() + textToInsert);
  131.                
  132.                 var numberOfLinesToInsert = textToInsert.split("\n").length;
  133.                 var numberOfLinesInTextareaAfterInsertion = getNumberOfLinesInTextarea(tarea);
  134.        
  135.                 var numberOfLineToSelectFromTextToInsert = 3;
  136.                 if (numberOfLineToSelectFromTextToInsert > numberOfLinesToInsert) {
  137.                     throw 'You can\'t select a line that is out of bounds.';
  138.                 }
  139.                
  140.                 selectTextareaLine(tarea, numberOfLinesInTextareaAfterInsertion - numberOfLinesToInsert + numberOfLineToSelectFromTextToInsert);
  141.        
  142.             } catch (err) {
  143.                 alert('An error occurred: ' + err);
  144.             }
  145.        
  146.         }); //click vege
  147.    
  148.  
  149.         //-----------------------------------------------------//
  150.         // --- 4.2. Cimmel ellatott SPOILER gomb megnyomasa ---//
  151.         //-----------------------------------------------------//
  152.         $('#spoiler_button_with_title').click(function () {
  153.            
  154.            
  155.             var $prompt_title = prompt($prompt_title_text, "");
  156.                
  157.             //csekkoljuk, hogy irtak-e cimet
  158.             if ($prompt_title === null){
  159.                
  160.                 var $title = ""; //nincs cim, 'Cancel'-t nyomott
  161.            
  162.             } else if ($prompt_title === '') {
  163.            
  164.                 var $title = ""; //nincs cim, 'Enter'-t nyomott
  165.            
  166.             } else if ($prompt_title.length > 0) {
  167.                
  168.                 var $title = '[B]'+$prompt_title+'[/B]'; //irt cimet
  169.            
  170.             } else {
  171.              
  172.                 var $title = "";
  173.                
  174.             }
  175.            
  176.             //beszuras  
  177.             try {
  178.                 var textToInsert =
  179.                   $title  
  180.                 + '\n'
  181.                 + '\n'+$spoiler_block_start
  182.                 + '\n'+$type_here_placeholder
  183.                 + '\n'+$spoiler_block_end;
  184.        
  185.                 $tarea.val($tarea.val() + textToInsert);
  186.                
  187.                 var numberOfLinesToInsert = textToInsert.split("\n").length;
  188.                 var numberOfLinesInTextareaAfterInsertion = getNumberOfLinesInTextarea(tarea);
  189.        
  190.                 var numberOfLineToSelectFromTextToInsert = 4;
  191.                 if (numberOfLineToSelectFromTextToInsert > numberOfLinesToInsert) {
  192.                     throw 'You can\'t select a line that is out of bounds.';
  193.                 }
  194.                
  195.                 selectTextareaLine(tarea, numberOfLinesInTextareaAfterInsertion - numberOfLinesToInsert + numberOfLineToSelectFromTextToInsert);
  196.        
  197.             } catch (err) {
  198.                 alert('An error occurred: ' + err);
  199.             }
  200.        
  201.         }); //click vege
  202.    
  203.    
  204.         //-----------------------------------------------------------------//
  205.         // --- 4.3. Linkesitett cimmel ellatott SPOILER gomb megnyomasa ---//
  206.         //-----------------------------------------------------------------//
  207.         $('#spoiler_button_with_link_title').click(function () {
  208.            
  209.             //bekerjuk a cimet
  210.             var $prompt_title = prompt($prompt_title_text, "");
  211.                
  212.             //csekkoljuk, hogy irtak-e cimet
  213.             if ($prompt_title === null){
  214.                
  215.                 var $title = ""; //nincs cim, 'Cancel'-t nyomott
  216.            
  217.             } else if ($prompt_title === '') {
  218.            
  219.                 var $title = ""; //nincs cim, 'Enter'-t nyomott
  220.            
  221.             } else if ($prompt_title.length > 0) {
  222.                
  223.                 var $title = '[B]'+$prompt_title+'[/B]'; //irt cimet
  224.            
  225.             } else {
  226.              
  227.                 var $title = "";
  228.                
  229.             }
  230.            
  231.             //bekerjuk a linket
  232.             var $prompt_link = prompt($prompt_link_text, "");
  233.            
  234.             //csekkoljuk, hogy irtak-e linket
  235.             if ($prompt_link === null){
  236.                
  237.                 var $insert_title_format = $title; //nincs link, 'Cancel'-t nyomott, ezert csak a cimet szurjuk be
  238.            
  239.             } else if ($prompt_link === '') {
  240.            
  241.                 var $insert_title_format = $title; //nincs link, 'Enter'-t nyomott, ezert csak a cimet szurjuk be
  242.            
  243.             } else if ($prompt_link.length > 0) {
  244.                
  245.                         if ($title.length > 0) {
  246.                            
  247.                             var $insert_title_format = '[L:'+$prompt_link+']'+$title+'[/L]'; //irt linket es cimet is, megformazzuk
  248.                        
  249.                         } else {
  250.                            
  251.                             var $insert_title_format = '[L:'+$prompt_link+'][link][/L]'; //csak linket irt, de cimet nem
  252.                         }                      
  253.            
  254.             } else {
  255.              
  256.                 var $insert_title_format = $title; //barmilyen egyeb esetben csak cimet szurunk be
  257.             }
  258.  
  259.  
  260.             //beszuras  
  261.             try {
  262.                 var textToInsert =
  263.                   $insert_title_format
  264.                 + '\n'
  265.                 + '\n'+$spoiler_block_start
  266.                 + '\n'+$type_here_placeholder
  267.                 + '\n'+$spoiler_block_end;
  268.        
  269.                 $tarea.val($tarea.val() + textToInsert);
  270.                
  271.                 var numberOfLinesToInsert = textToInsert.split("\n").length;
  272.                 var numberOfLinesInTextareaAfterInsertion = getNumberOfLinesInTextarea(tarea);
  273.        
  274.                 var numberOfLineToSelectFromTextToInsert = 4;
  275.                 if (numberOfLineToSelectFromTextToInsert > numberOfLinesToInsert) {
  276.                     throw 'You can\'t select a line that is out of bounds.';
  277.                 }
  278.                
  279.                 selectTextareaLine(tarea, numberOfLinesInTextareaAfterInsertion - numberOfLinesToInsert + numberOfLineToSelectFromTextToInsert);
  280.        
  281.             } catch (err) {
  282.                 alert('An error occurred: ' + err);
  283.             }
  284.        
  285.         }); //click vege
  286.    
  287.  
  288.    
  289. }); //script vege
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement