Advertisement
zanetu

mal_covers_3.user.js

Jan 17th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           MAL Covers 3
  3. // @include        http://myanimelist.net/animelist/*
  4. // @include        http://myanimelist.net/mangalist/*
  5. // @description    Adds a cover image for each anime in your anime list.
  6. // @version        3.3
  7. // ==/UserScript==
  8.  
  9. (function() {
  10.  
  11. function main() {
  12.  
  13. /**
  14.  * jQuery.ScrollTo - Easy element scrolling using jQuery.
  15.  * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
  16.  * Dual licensed under MIT and GPL.
  17.  * Date: 5/25/2009
  18.  * @author Ariel Flesler
  19.  * @version 1.4.2
  20.  *
  21.  * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
  22.  */
  23. ;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
  24.  
  25. /* jQuery Sonar */
  26. (function(g,m,l,o){g.fn.sonar=function(a,b){"boolean"===typeof a&&(b=a,a=o);return g.sonar(this[0],a,b)};var k=l.body,r=g(m),j=function(a,b,f){if(a){k||(k=l.body);var c=a,d=0,e=k.offsetHeight,g=m.innerHeight||l.documentElement.clientHeight||k.clientHeight||0,h=l.documentElement.scrollTop||m.pageYOffset||k.scrollTop||0,j=a.offsetHeight||0;if(!a.sonarElemTop||a.sonarBodyHeight!==e){if(c.offsetParent){do d+=c.offsetTop;while(c=c.offsetParent)}a.sonarElemTop=d;a.sonarBodyHeight=e}b=b===o?0:b;return!(a.sonarElemTop+
  27. (f?0:j)<h-b)&&!(a.sonarElemTop+(f?j:0)>h+g+b)}},h={},p=0,n,s=function(){n&&clearTimeout(n);n=setTimeout(function(){var a,b,f,c,d,e,i;for(f in h){b=h[f];for(e=0,i=b.length;e<i;e++)if(c=b[e],a=c.elem,d=j(a,c.px,c.full),"scrollout"===f?!d:d){if(!c.tr)a["_"+f]?(g(a).trigger(f),c.tr=1):(b.splice(e,1),e--,i--)}else c.tr=0}},0)},q=function(a,b){var f=b.px,c=b.full,d=b.evt,e=j(a,f,c),i=0;a["_"+d]=1;if("scrollout"===d?!e:e)setTimeout(function(){g(a).trigger("scrollout"===d?"scrollout":"scrollin")},0),i=1;
  28. h[d].push({elem:a,px:f,full:c,tr:i});p||(r.bind("scroll",s),p=1)};g.sonar=j;h.scrollin=[];g.event.special.scrollin={add:function(a){a=a.data||{};this.scrollin||q(this,{px:a.distance,full:a.full,evt:"scrollin"})},remove:function(){this._scrollin=0}};h.scrollout=[];g.event.special.scrollout={add:function(a){a=a.data||{};this.scrollout||q(this,{px:a.distance,full:a.full,evt:"scrollout"})},remove:function(){this._scrollout=0}}})(jQuery,window,document);
  29.  
  30. /* MAL 3 */
  31. var mal3 = {};
  32.  
  33. mal3.version = 3;
  34.  
  35. mal3.config = {
  36.   defaults: {
  37.     mode: 'classic'
  38.   },
  39.   request_url: 'http://myanimelist.net/',
  40.   dummy_covers: {
  41.     nya: 'https://i.imgur.com/9CAifRR.png',
  42.     cnf: 'https://i.imgur.com/HWehYls.png'
  43.   },
  44.   cdn: 'http://cdn.myanimelist.net/images/'
  45. };
  46.  
  47. mal3.your_anime = {};
  48.  
  49. mal3.warnings = {};
  50.  
  51. mal3.trim = function(str) {
  52.   if (!String.prototype.trim) {
  53.     return str.replace(/^\s+|\s+$/g, '');
  54.   } else {
  55.     return str.trim();
  56.   }
  57. };
  58.  
  59. mal3.setCookie = function(name, value) {
  60.   var exdate = new Date();
  61.   exdate.setDate(exdate.getDate() + 365);
  62.   var expires = exdate.toGMTString();
  63.   document.cookie = name + "=" + escape(value) + "; expires=" + expires + "; path=/";
  64. };
  65.  
  66. mal3.getCookie = function(name) {
  67.   var cookies = {};
  68.   var rawCookies = document.cookie.split(';');
  69.   var cookie;
  70.   for (var i = 0; i < rawCookies.length; i++) {
  71.     cookie = rawCookies[i].split('=');
  72.     cookies[mal3.trim(cookie[0])] = mal3.trim(cookie[1]);
  73.   }
  74.   return cookies;
  75. };
  76.  
  77. mal3.lazyLoadImage = function(img) {
  78.   var $img = jQuery(img),
  79.     src = $img.attr('data-lazy-src');
  80.  
  81.   $img.unbind('scrollin') // remove event binding
  82.     .hide()
  83.     .removeAttr('data-lazy-src')
  84.     .attr('data-lazy-loaded', 'true');
  85.  
  86.   img.src = src;
  87.   $img.fadeIn('slow');
  88. };
  89.  
  90. mal3.toggleMode = function(e){
  91.   $('.mal3_action_link').removeClass('mal3_current');
  92.   if(e.target.id == 'mal3_enable_classic' && mal3.mode != 'classic') {
  93.     $('#mal3_enable_classic').addClass('mal3_current');
  94.     mal3.setCookie('mode','classic');
  95.   } else if (e.target.id == 'mal3_enable_decat' && mal3.mode != 'decat') {
  96.     $('#mal3_enable_decat').addClass('mal3_current');
  97.     mal3.setCookie('mode','decat');
  98.   }
  99.   $('#mal3_message').html('Reload page to see the changes.');
  100.   return false;
  101. };
  102.  
  103. mal3.clearCache = function() {
  104.   localStorage.clear();
  105.   $('#mal3_message').html('Cache cleared! Reload page to see the changes.');
  106. };
  107.  
  108. mal3.placeCopyright = function() {
  109.   $('#copyright').append('<br /><a href="https://gist.github.com/Alex7Kom/41e5b5ce9d69ce1251fc">MAL Covers 3 UserScript (v.3.'+ mal3.version +')</a> by Alex7Kom. Mode: <a href="#" id="mal3_enable_classic" class="mal3_action_link">Standard</a> | <a href="#" id="mal3_enable_decat" class="mal3_action_link">Description</a>. <a href="#" id="mal3_clear_cache" class="mal3_action_link">Clear cache</a>.<br><span id="mal3_message" class="mal3_message"></span>');
  110. };
  111.  
  112. mal3.showMessage = function(message) {
  113.   $('#mal3_message').html(message);
  114. };
  115.  
  116. mal3.showWarning = function(message, id) {
  117.   if (!mal3.warnings[id]) {
  118.     $('<div class="mal3warning">' + message + '</div>').prependTo('#list_surround');
  119.     mal3.warnings[id] = true;
  120.   }
  121. };
  122.  
  123. mal3.zoom = function() {
  124.   var cover_src = $(this).attr('src');
  125.   var cover_id = $(this).attr('data-id');
  126.   if(cover_src != mal3.config.dummy_covers.nya && cover_src != mal3.config.dummy_covers.cnf){
  127.     var cover_pos = $(this).offset();
  128.     var bbottom = $(document).height() - (cover_pos.top + $(this).height());
  129.     var under_height = bbottom - ($(document).height() - ($(document).scrollTop() + $(window).height()));
  130.     var above_height = $(document).scrollTop() - cover_pos.top;
  131.  
  132.     var cover_elem = $('#cover' + cover_id).length > 0 ? $('#cover' + cover_id) : $('<img class="mal3zoomed" id="cover' + cover_id + '" data-id="' + cover_id + '" src="' + cover_src + '" />');
  133.  
  134.     $(cover_elem).attr('style', 'top: ' + cover_pos.top + 'px; left: ' + cover_pos.left + 'px;');
  135.  
  136.     if(under_height > 200){
  137.       $(cover_elem)
  138.         .appendTo('body')
  139.         .bind('mouseover', function(){
  140.           mal3.zoomImage(this, under_height, above_height);
  141.         })
  142.         .bind('mouseleave', function(){
  143.           mal3.unZoomImage(this, cover_pos.top);
  144.         })
  145.         .click(function(){
  146.           mal3.openLink(cover_id);
  147.         });
  148.     } else {
  149.       $(cover_elem)
  150.         .appendTo('body')
  151.         .bind('mouseover', function(){
  152.           mal3.zoomImage(this, under_height, above_height, cover_pos.top + 5 - $(this).height()*1.25);
  153.         })
  154.         .bind('mouseleave', function(){
  155.           mal3.unZoomImage(this, cover_pos.top);
  156.         })
  157.         .click(function(){
  158.           mal3.openLink(cover_id);
  159.         });
  160.     }
  161.   }
  162. };
  163.  
  164. mal3.zoomImage = function(elem, under_height, above_height, top) {
  165.   var params = {width: '225'};
  166.   if(top){
  167.     params.top = top;
  168.   }
  169.   $(elem).animate(params, 300);
  170.   if(under_height < 0){
  171.     $.scrollTo($(document).scrollTop() - under_height-20, 300);
  172.   } else if(above_height > 0){
  173.     $.scrollTo($(document).scrollTop() - above_height, 300);
  174.   }
  175. };
  176.  
  177. mal3.unZoomImage = function(elem, top){
  178.   var params = {width: '100'};
  179.   if(top){
  180.     params.top = top;
  181.   }
  182.   $(elem).animate(params, 300, function(){
  183.     $(elem).remove();
  184.   });
  185. };
  186.  
  187. mal3.openLink = function(id){
  188.   window.open('http://myanimelist.net/' + (mal3.type == 'a' ? 'anime' : 'manga') + '/' + id + '/','_blank');
  189.   window.focus();
  190. };
  191.  
  192. mal3.bindZoom = function(){
  193.   $('img.mal3img').unbind('mouseenter');
  194.   $('img.mal3img').bind('mouseenter', mal3.zoom);
  195. };
  196.  
  197. mal3.expandInfo = function(event){
  198.   var series_id = $(event.target).attr("anime-id");
  199.   var colorType = $(event.target).attr("color-type");
  200.   if ($('#more'+series_id).css('display') == 'block') {
  201.     $('#more'+series_id).hide();
  202.     return false;
  203.   }
  204.  
  205.   var memberId = $('#listUserId').val();
  206.  
  207.   $.post("/includes/ajax-no-auth.inc.php?t=6", {color:colorType,id:series_id,memId:memberId,type:$('#listType').val()}, function(data) {
  208.     $('#more'+series_id).html(data.html);
  209.     if(mal3.your_anime[series_id] !== undefined){
  210.       $('#more' + series_id + ' td div:first').before('<img src="' + mal3.your_anime[series_id] + '" class="mal3decat" />');
  211.     }
  212.     $('#more'+series_id).show();
  213.   }, "json");
  214. };
  215.  
  216. mal3.insertCover = function(id, image) {
  217.     $("#" + id + " > tr > td > img.mal3img")
  218.     .attr('data-lazy-src',image)
  219.     .attr('data-id',id)
  220.     .bind('scrollin', { distance: 100 }, function() {
  221.         mal3.lazyLoadImage(this);
  222.     });
  223. };
  224.                    
  225. mal3.getCover = function(id, callback) {
  226.     var coverData = localStorage.getItem(mal3.type + '_' + id);
  227.     if (coverData !== null) {
  228.       var cover = coverData.split(';');
  229.         if(parseInt(cover[1]) + 1000*60*60*24*14 > Date.now()){
  230.             if (cover[0] != mal3.config.dummy_covers.cnf) {
  231.               var coverA = mal3.config.cdn + (mal3.type == 'a' ? 'anime' : 'manga') + '/' + cover[0];
  232.             } else {
  233.               var coverA = mal3.config.dummy_covers.cnf;
  234.             }
  235.            
  236.             if(mal3.mode == 'classic'){
  237.                 mal3.insertCover(id, coverA);
  238.                 mal3.bindZoom();
  239.             } else if(mal3.mode == 'decat') {
  240.                 mal3.your_anime[id] = coverA;
  241.             }
  242.             return callback();
  243.         }
  244.     }
  245.     var url = mal3.config.request_url + (mal3.type == 'a' ? 'anime' : 'manga') + '/' + id;
  246.    
  247.     $.ajax({
  248.     type: 'GET',
  249.     url: url,
  250.     success: function(body) {    
  251.       var data = body.match(mal3.coverpatt);
  252.       if (data !== null){
  253.         console.log(data[1]);
  254.         if(mal3.mode == 'classic'){
  255.             mal3.insertCover(id, data[1]);
  256.         } else if(mal3.mode == 'decat') {
  257.             mal3.your_anime[id] = data[1];
  258.         }
  259.         var cover = data[1].split(mal3.config.cdn + (mal3.type == 'a' ? 'anime' : 'manga') + '/');
  260.         localStorage.setItem(mal3.type + '_' + id, cover[1] + ';' + Date.now());
  261.       } else {
  262.         localStorage.setItem(mal3.type + '_' + id, mal3.config.dummy_covers.cnf + ';' + (Date.now() - 1000*60*60*24*14 + 1000*60*60*6));
  263.       }
  264.       callback();
  265.     }
  266.   });
  267. };
  268.  
  269. mal3.animepatt = /\/animelist\//i;
  270. mal3.url_patt = /\/(?:manga|anime)\/(\d+)\//i;
  271. mal3.coverpatt = new RegExp('<td class="borderClass"[^>]+><div[^>]+>(?:<a[^>]+>)?<img src="([^"]+)"[^>]+>(?:</a>)?</div>', 'i');
  272.  
  273. if(mal3.animepatt.exec(location.href)){
  274.   mal3.type = 'a';
  275. } else {
  276.   mal3.type = 'm';
  277. }
  278.  
  279. mal3.mode = mal3.getCookie()['mode'];
  280. if(mal3.mode != 'classic' && mal3.mode != 'decat'){
  281.   mal3.mode = mal3.config.defaults.mode;
  282. }
  283.  
  284. mal3.setCookie('mode', mal3.mode);
  285.  
  286. $(document).ready(function(){
  287.   mal3.placeCopyright();
  288.  
  289.   $('#mal3_enable_classic, #mal3_enable_decat').click(mal3.toggleMode);
  290.   $('#mal3_clear_cache').click(mal3.clearCache);
  291.  
  292.   if(mal3.mode == 'classic'){
  293.     $('#mal3_enable_classic').addClass('mal3_current');
  294.   } else if(mal3.mode == 'decat'){
  295.     $('#mal3_enable_decat').addClass('mal3_current');
  296.   }
  297.  
  298.   var list = $("a.animetitle");
  299.  
  300.   var get_array = [];
  301.  
  302.   for (var i = 0; i < list.length; i++) {
  303.     var id_afterexec = mal3.url_patt.exec($(list[i]).attr("href"));
  304.     get_array.push(id_afterexec[1]);
  305.     $(list[i]).parent().parent().parent().attr("id",id_afterexec[1]);
  306.     if(mal3.mode == 'classic'){
  307.       $("#"+id_afterexec[1]).find("a.animetitle").parent().before('<td class="mal3td ' + $("#"+id_afterexec[1]+" > tr > td:first").attr('class') + '"><img src="' + mal3.config.dummy_covers.nya + '" class="mal3img" /></td>');
  308.     } else if(mal3.mode == 'decat'){
  309.       var derp = String($('#' + id_afterexec[1] + ' a[title="View More Information"]').attr('onclick'));
  310.       $('#' + id_afterexec[1] + ' a[title="View More Information"]').removeAttr('onclick').click(mal3.expandInfo);
  311.       $('#' + id_afterexec[1] + ' a[title="View More Information"]').attr("anime-id",id_afterexec[1]).attr("color-type",derp.substring(derp.length-5,derp.length-4));
  312.     }
  313.   }
  314.  
  315.   var w = 0;
  316.  
  317.   function iterateOverCoverArray(){
  318.     w++;
  319.     if (w < get_array.length){
  320.       setTimeout(function(){
  321.         mal3.getCover(get_array[w], iterateOverCoverArray);
  322.       }, 0);
  323.     } else {
  324.       mal3.bindZoom();
  325.     }
  326.   }
  327.   mal3.getCover(get_array[w], iterateOverCoverArray);
  328.  
  329. });
  330.  
  331. }
  332.  
  333. var script = document.createElement('script');
  334. script.appendChild(document.createTextNode('('+ main +')();'));
  335. (document.body || document.head || document.documentElement).appendChild(script);
  336.  
  337. var style = document.createElement('style');
  338. style.innerHTML = '.mal3_action_link { text-decoration: none; border-bottom: 1px dashed; } .mal3_current { border: 0; } .mal3_message { color: Red; } .mal3img { width: 100%; height: auto; } .mal3td { width: 100px; height: 150px; } .mal3decat { float: right; margin: 15px; } .mal3zoomed { cursor: hand; cursor: pointer; position: absolute; width: 100px; height: auto; } .mal3warning { margin: 10px; padding: 10px; text-align: center; background: #fff; border: 2px solid #fae3e3;  color: #000; } .mal3warning a { text-decoration: underline; }';
  339. (document.body || document.head || document.documentElement).appendChild(style);
  340.  
  341. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement