Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name animelyrics
- // @namespace mugiseyebrows.ru
- // @include http://www.animelyrics.com/*
- // @version 1
- // @grant none
- // @require https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js
- // ==/UserScript==
- var data = {kanji:[],romaji:[],translation:[]};
- function getRomaji() {
- data.romaji = [];
- data.translation = [];
- $('table table tr td span').each(function(i,e){
- var t = $(e).text();
- t = t.replace(/Lyrics from Animelyrics.com/,'');
- if (i % 2 == 0) {
- data.romaji.push(t);
- } else {
- data.translation.push(t);
- }
- });
- }
- function myAjaxSetup() {
- $.ajaxSetup({
- 'beforeSend' : function(xhr) {
- xhr.overrideMimeType('text/html; charset=Shift_JIS');
- },
- });
- }
- function createButton(p,id,t,fn) {
- var button = $('<input/>').appendTo(p).attr('type','button').attr('id',id).attr('value',t);
- $(button).click(fn);
- }
- function getKanji() {
- var links = [];
- $('a').each(function(i,e){ if (e.href.match(/jis$/)) { links.push(e.href); } });
- if (links.length>0) {
- myAjaxSetup();
- $.ajax({url:links[0],
- success:function(d){
- data.kanji = $(d).find('#kanji').text().trim().split(/\n\n/);
- }});
- }
- }
- function showData() {
- $('#my-textarea-0').val( data.kanji.join("\n\n") );
- $('#my-textarea-1').val( data.romaji.join("\n\n") );
- $('#my-textarea-2').val( data.translation.join("\n\n") );
- }
- function getTextBlock(text1, text2) {
- if (text1.length < 1)
- return '';
- var lines1 = [];
- var lines2 = [];
- var line = text1.shift();
- while (line.trim() != '' && text1.length > 0) {
- lines1.push(line)
- line = text1.shift();
- }
- lines1.push(line);
- while (lines2.length < lines1.length && text2.length > 0) {
- line = text2.shift();
- lines2.push(line);
- }
- return lines1.join("\n") + "\n" + lines2.join("\n");
- }
- function showLineByLine() {
- var text1 = $('#my-textarea-0').val().split(/\n/);
- var text2 = $('#my-textarea-2').val().split(/\n/);
- text1.push('');
- text2.push('');
- var blocks = [];
- while (text1.length>0) {
- blocks.push(getTextBlock(text1,text2));
- }
- $('#my-textarea-3').val( blocks.join("\n") );
- }
- function showFormated() {
- var rows = [
- data.kanji.join('</p><p>').replace(/\n/g,'<br/>'),
- data.romaji.join('</p><p>').replace(/\n/g,'<br/>'),
- data.translation.join('</p><p>').replace(/\n/g,'<br/>'),
- ]
- $('#my-div-formated').html('<hr/><p>' + rows.join('</p><hr/><p>') + '</p><hr/>');
- }
- function toggleUI() {
- //console.log('toggleUI');
- $('#my-div').toggle();
- return false;
- }
- function initUI() {
- $('<li/>').appendTo('#utility').attr('id','my-li');
- $('<a/>').appendTo('#my-li').attr('href','#').html('grab').click(toggleUI);
- $('<div/>').appendTo('body').css({position:'absolute',top:'0',left:'0','background-color':'#ffffff','z-index':500,width:'97%',display:'none'}).attr('id','my-div');
- createButton('#my-div','my-btn-kanji','getKanji',getKanji);
- createButton('#my-div','my-btn-romaji','getRomaji',getRomaji);
- createButton('#my-div','my-btn-show','showData',showData);
- createButton('#my-div','my-btn-formated','showFormated',showFormated);
- createButton('#my-div','my-btn-linebyline','showLineByLine',showLineByLine);
- $('<br/>').appendTo('#my-div');
- for (var i=0;i<4;i++) {
- $('<textarea>').appendTo('#my-div').css({height:'400px',width:'19%',float:'left'}).attr('id','my-textarea-' + i);
- }
- //$('<textarea>').appendTo('#my-div').css({'overflow-y':'auto',height:'400px',width:'20%'}).attr('id','my-div-formated');
- $('<div/>').appendTo('#my-div').css({'overflow-y':'auto',height:'400px',width:'19%',float:'left'}).attr('id','my-div-formated');
- }
- $(document).ready(function(){
- initUI();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement