Raw Source
drantor / Jump to Last Watched AnimeLoads Episode

// ==UserScript==
// @name       Jump to Last Watched AnimeLoads Episode
// @namespace  https://openuserjs.org/users/drantor
// @version    0.5
// @description My first Javascript :D it's scrolls to the end of the watched episodes on Animeloads.org
// @match      http://*anime-loads.org/media/*
// @grant		none
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @updateURL 	https://dl.dropboxusercontent.com/u/11260601/dev/animeloads_scroller.js
// @downloadURL https://dl.dropboxusercontent.com/u/11260601/dev/animeloads_scroller.js
// @author drantor (Felix Beckmann)
// @copyright  2014+, Felix Beckmann
// ==/UserScript==

var lastLabel;
var errMsg;

function scrollToLast() {
    var newLabel = findLastLabel();
    if(newLabel != lastLabel) {
        lastLabel = newLabel;
    }
    if(lastLabel !== null) {
        errMsg.style.display="none";
        $('body').scrollTo(lastLabel);
    } else {
        errMsg.style.display="inline";
    }
}

function init() {
    $.fn.scrollTo = function( target, options, callback ){
      if(typeof options == 'function' && arguments.length == 2){ callback = options; options = target; }
      var settings = $.extend({
        scrollTarget  : target,
        offsetTop     : 50,
        duration      : 500,
        easing        : 'swing'
      }, options);
      return this.each(function(){
        var scrollPane = $(this);
        var scrollTarget = (typeof settings.scrollTarget == "number") ? settings.scrollTarget : $(settings.scrollTarget);
        var scrollY = (typeof scrollTarget == "number") ? scrollTarget : scrollTarget.offset().top + scrollPane.scrollTop() - parseInt(settings.offsetTop);
        scrollPane.animate({scrollTop : scrollY }, parseInt(settings.duration), settings.easing, function(){
          if (typeof callback == 'function') { callback.call(this); }
        });
      });
    }
    var comments = document.getElementsByClassName('comments littlelink');
    comments = comments.item(0);
    comments.parentElement.appendChild(document.createElement('br'));
    var clickMe = comments.parentElement.appendChild(document.createElement('button'));
    clickMe.setAttribute('name', 'scrollToLastWatched');
    clickMe.setAttribute('value', 'scrollToLastWatched');
    clickMe.setAttribute('type', 'button');
    clickMe.appendChild(document.createTextNode('Scroll to last Watched'));
    clickMe.addEventListener("click", scrollToLast, false);
    errMsg = comments.parentElement.appendChild(document.createElement("div"));
    errMsg.style.display="none";
    errMsg.style.color="#ff0000";
    errMsg.appendChild(document.createTextNode("Fehler kein Scrollpunkt"));
}

function findLastLabel() {
    var table = document.getElementById("partlist");
    var labels = table.getElementsByClassName("ui-icon watched");
    var foundFirstWatched = false;
    var foundLabel = null;
    for(var i=0;i<labels.length;i++) {
        var label = labels.item(i);
        if(label.childNodes.length == 1 && label.firstChild.textContent == "angesehen") {
            if(label.getAttribute("aria-pressed") == "true") {
                foundFirstWatched = true;
                foundLabel = label;
                continue;
            } else {
                if(foundLabel === null) {
                    foundLabel = label;
                    break;
                }
                if(foundFirstWatched) {
                    break;
                }
            }
        }
    }
    return foundLabel;
}

$(document).ready(init);