Advertisement
overloop

animelyrics.user.js

Jul 12th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        animelyrics
  3. // @namespace   mugiseyebrows.ru
  4. // @include     http://www.animelyrics.com/*
  5. // @version     1
  6. // @grant       none
  7. // @require     https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
  8. // ==/UserScript==
  9.  
  10. var data = {kanji:[],romaji:[],translation:[]};
  11.  
  12. function getRomaji() {
  13.   data.romaji = [];
  14.   data.translation = [];
  15.   $('table table tr td span').each(function(i,e){
  16.     var t = $(e).text();
  17.     t = t.replace(/Lyrics from Animelyrics.com/,'');
  18.     if (i % 2 == 0) {
  19.       data.romaji.push(t);
  20.     } else {
  21.       data.translation.push(t);
  22.     }
  23.   });
  24. }
  25.  
  26. function myAjaxSetup() {
  27.   $.ajaxSetup({
  28.     'beforeSend' : function(xhr) {
  29.         xhr.overrideMimeType('text/html; charset=Shift_JIS');
  30.     },
  31.   });
  32. }
  33.  
  34. function createButton(p,id,t,fn) {
  35.   var button = $('<input/>').appendTo(p).attr('type','button').attr('id',id).attr('value',t);
  36.   $(button).click(fn);
  37. }
  38.  
  39. function getKanji() {
  40.   var links = [];
  41.   $('a').each(function(i,e){ if (e.href.match(/jis$/)) { links.push(e.href); } });
  42.   if (links.length>0) {
  43.     myAjaxSetup();
  44.     $.ajax({url:links[0],
  45.     success:function(d){
  46.       data.kanji = $(d).find('#kanji').text().trim().split(/\n\n/);
  47.     }});
  48.   }
  49. }
  50.  
  51. function showData() {
  52.   $('#my-textarea-0').val( data.kanji.join("\n\n") );
  53.   $('#my-textarea-1').val( data.romaji.join("\n\n") );
  54.   $('#my-textarea-2').val( data.translation.join("\n\n") );
  55. }
  56.  
  57.  
  58. function getTextBlock(text1, text2) {
  59.    
  60.     if (text1.length < 1)
  61.         return '';
  62.    
  63.     var lines1 = [];
  64.     var lines2 = [];
  65.     var line = text1.shift();
  66.    
  67.     while (line.trim() != '' && text1.length > 0) {
  68.         lines1.push(line)
  69.         line = text1.shift();
  70.     }
  71.     lines1.push(line);
  72.    
  73.     while (lines2.length < lines1.length && text2.length > 0) {
  74.         line = text2.shift();
  75.         lines2.push(line);
  76.     }
  77.    
  78.     return lines1.join("\n") + "\n" + lines2.join("\n");
  79. }
  80.  
  81. function showLineByLine() {
  82.   var text1 = $('#my-textarea-0').val().split(/\n/);
  83.   var text2 = $('#my-textarea-2').val().split(/\n/);
  84.  
  85.   text1.push('');
  86.   text2.push('');
  87.  
  88.   var blocks = [];
  89.   while (text1.length>0) {
  90.     blocks.push(getTextBlock(text1,text2));
  91.   }
  92.  
  93.   $('#my-textarea-3').val(  blocks.join("\n") );
  94. }
  95.  
  96. function showFormated() {
  97.  
  98.   var rows = [
  99.     data.kanji.join('</p><p>').replace(/\n/g,'<br/>'),
  100.     data.romaji.join('</p><p>').replace(/\n/g,'<br/>'),
  101.     data.translation.join('</p><p>').replace(/\n/g,'<br/>'),
  102.   ]
  103.  
  104.   $('#my-div-formated').html('<hr/><p>' +  rows.join('</p><hr/><p>') + '</p><hr/>');
  105. }
  106.  
  107. function toggleUI() {
  108.   //console.log('toggleUI');
  109.     $('#my-div').toggle();
  110.   return false;
  111. }
  112.  
  113. function initUI() {
  114.  
  115.   $('<li/>').appendTo('#utility').attr('id','my-li');
  116.   $('<a/>').appendTo('#my-li').attr('href','#').html('grab').click(toggleUI);
  117.  
  118.   $('<div/>').appendTo('body').css({position:'absolute',top:'0',left:'0','background-color':'#ffffff','z-index':500,width:'97%',display:'none'}).attr('id','my-div');
  119.  
  120.   createButton('#my-div','my-btn-kanji','getKanji',getKanji);
  121.   createButton('#my-div','my-btn-romaji','getRomaji',getRomaji);
  122.   createButton('#my-div','my-btn-show','showData',showData);
  123.   createButton('#my-div','my-btn-formated','showFormated',showFormated);
  124.   createButton('#my-div','my-btn-linebyline','showLineByLine',showLineByLine);
  125.  
  126.   $('<br/>').appendTo('#my-div');
  127.  
  128.   for (var i=0;i<4;i++) {
  129.     $('<textarea>').appendTo('#my-div').css({height:'400px',width:'19%',float:'left'}).attr('id','my-textarea-' + i);
  130.   }
  131.  
  132.   //$('<textarea>').appendTo('#my-div').css({'overflow-y':'auto',height:'400px',width:'20%'}).attr('id','my-div-formated');
  133.  
  134.   $('<div/>').appendTo('#my-div').css({'overflow-y':'auto',height:'400px',width:'19%',float:'left'}).attr('id','my-div-formated');
  135.  
  136. }
  137.  
  138. $(document).ready(function(){
  139.   initUI();
  140. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement