SkipperScript

It should be able to skip and autoskip intos

As of 2019-06-25. See the latest version.

// ==UserScript==
// @name         SkipperScript
// @version      0.3
// @match        ://stream.proxer.me/*
// @description  It should be able to skip and autoskip intos
// @author       Schlauewurst
// @require      http://code.jquery.com/jquery-3.4.1.min.js
// @require      https://www.gstatic.com/firebasejs/6.2.3/firebase-app.js
// @require      https://www.gstatic.com/firebasejs/6.2.3/firebase-firestore.js
// @namespace    https://greasyfork.org/users/312840
// ==/UserScript==

/*TODO
Add Database
Show Status -> Has Episode Data, Has Data of Same Anime, No Data
Add visualy ->  Intro(red) outro(red) filler(blue)
Icons
Add compatibly to other players
Skip Flashbacks
Skip Filler
Parse all the Info from somewhere
*/
(function() {
    'use strict';
    var whatskip = "intro",
        database,
        currentdata,
        IntroStart,
        IntroEnd,
        OutroStart,
        url = new URL(window.location.href).searchParams.get("ref").split("/"),
        thisepisode = parseInt(url[3]),
        thisanime = parseInt(url[2]);

    function initDatabase() {
        firebase.initializeApp({
            apiKey: "AIzaSyB9s0n2m60SQQdbVTnN6UICAkBrC4svfwg",
            authDomain: "proxerinfobase.firebaseapp.com",
            projectId: "proxerinfobase"
        });
        return firebase.firestore();
    }

    function displayTime(sek){
        const time = new Date(null);
        time.setSeconds(sek);
        return time.getMinutes() +":"+ ('0' + time.getSeconds()).slice(-2);
    }

    function getSekonds(time){
        time = time.split(":");
        return parseInt(time[0]*60) +parseInt(time[1])
    }

    function createCookie(name, value, days = 365) {
        var expires;
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            expires = "; expires=" + date.toGMTString();
        } else {
            expires = "";
        }
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            let c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                let c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) {
                    c_end = document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }

    function loadSettings(data = false) {
        if (data) {
            OutroStart = currentdata.StartOutro;
            IntroEnd = currentdata.EndIntro;
            IntroStart =currentdata.StartIntro;
            $("#infobox").text("Match: "+currentdata.match);
        }
        "false" == getCookie("show") && ($("#hidebtn").text("Show"), $("#buttonlist").hide());
        "true" == getCookie("autoplay") && $("#autoplay").find("input").prop("checked", !0);
        "true" == getCookie("skipintro") && $("#skipintro").find("input").first().prop("checked", !0);
        "true" == getCookie("skipoutro") && $("#skipoutro").find("input").first().prop("checked", !0);

        if (player.ready && $("#autoplay").find("input").is(":checked")) {
            player.play()
        }
    }

    function makeButtons() {
        $("#psplayercontrols").append(" | <button id='hidebtn'>Hide</button>")
        $("#player_code").find("div").first().prepend("<span style='padding:10px;margin:5% 0px 0px 10%;position:fixed;z-index:10;background:grey;width:80%;height:80%;' id='buttonlist'>")
        $("#buttonlist").append("<div id='autoplay'><label for='autoplay'>Autoplay</label><input name='autoplay' type='checkbox'></input></div>");
        $("#buttonlist").append("<div id='skipintro'><label for='skipintro'>Skip Intro</label><input name='skipintro' type='checkbox'></input></div>");
        $("#buttonlist").append("<div id='skipoutro'><label for='skipoutro'>Skip Outro</label><input name='skipoutro' type='checkbox'></input></div>");
        $("#buttonlist").append("<div id='infobox'>Match: None</div>");
        $("#buttonlist").append("<div><label>Intro Start:</label><input id='getIntroStart' value='0:00'></input></div>");
        $("#buttonlist").append("<div><label>Intro End:</label><input id='getIntroEnd' value='1:40'></input></div>");
        $("#buttonlist").append("<div><label>Outro Start:</label><input id='getOutroStart' value='22:00'></input></div>");
        $("#buttonlist").append("<div><button id='updatedata'>Update Data</button></div>");
        $("#player_code").find("div").first().append("</span>");
        $("#player_code").find("div").first().prepend("<button style='z-index:10;position:fixed;bottom:50px;left:20px;display:none' id='skipbtn'>Skip</button>")
    }

    function startNextEpisode() {
        window.top.location.href = "https://proxer.me/watch/" + thisanime + "/" + (parseInt(thisepisode) + 1) + "/" + url[4]
    }

    function workWithData() {
        console.log(currentdata);
        if (currentdata == undefined) {
            loadSettings();
        } else {
            loadSettings(true);
        }
    }

    function getCurrentData() {
        database.collection("Anime").get().then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                let info = doc.data();
                if (info.Anime == thisanime) {
                    if(currentdata == undefined){
                        currentdata = info;
                        currentdata.match = "same Anime";
                    }
                    if (info.Episode == thisepisode) {
                        currentdata = info;
                        currentdata.match = "same Episode";
                    }
                }
            });
            workWithData()
        });
    }

    function startTimer(data = false) {
        setInterval(function() {
            let skip1 = $("#skipintro input").is(":checked"),
                skip2 = $("#skipoutro input").is(":checked");

            $("#skipbtn").hide();
            if (skip1 && player.currentTime > IntroStart && player.currentTime < IntroEnd) {
                player.currentTime = IntroEnd;
            } else if (player.currentTime > IntroStart && player.currentTime < IntroEnd) {
                $("#skipbtn").show();
                whatskip = "intro"
            }

            if (skip2 && player.currentTime > OutroStart) {
                startNextEpisode()
            } else if (player.currentTime > OutroStart) {
                $("#skipbtn").show();
                whatskip = "outro"
            }
        }, 1000);
    }

    $(document).ready(function() {
        database = initDatabase();
        getCurrentData();
        makeButtons();
        startTimer();

        $(document).on("click", "#hidebtn", function() {
            if ($(this).text() == "Hide") {
                $(this).text("Show");
                $("#buttonlist").hide();
                createCookie("show", false);
            } else {
                $(this).text("Hide");
                $("#buttonlist").show();
                createCookie("show", true);
            }
        })

        $(document).on("click", "#autoplay", function() {
            let autoplay = $(this).find("input").is(":checked")
            createCookie("autoplay", autoplay);
        })

        $(document).on("click", "#skipintro", function() {
            let skipintro = $(this).find("input").first().is(":checked")
            createCookie("skipintro", skipintro);
        })

        $(document).on("click", "#skipoutro", function() {
            let skipoutro = $(this).find("input").first().is(":checked")
            createCookie("skipoutro", skipoutro);
        })

        $(document).on("click", "#updatedata", function() {
            database.collection("Anime").doc(thisanime+"-"+thisepisode).set({
                StartOutro: getSekonds($("#getOutroStart").val()),
                EndIntro: getSekonds($("#getIntroEnd").val()),
                StartIntro: getSekonds($("#getIntroStart").val()),
                Anime:thisanime,
                Episode:thisepisode
            }).then(function(docRef) {
                console.log("Doc written");
                $("#updatedata").text("Data Updated");
                OutroStart = getSekonds($("#getOutroStart").val());
                IntroStart=getSekonds($("#getIntroStart").val());
                IntroEnd=getSekonds($("#getIntroEnd").val());
            }).catch(function(error) {
                console.error("Error adding document: ", error);
            });
        })


        $(document).on("click", "#skipbtn", function() {
            if (whatskip == "intro") {
                player.currentTime = IntroEnd;
            } else if (whatskip == "outro") {
                startNextEpisode()
            }
            $("#skipbtn").hide();
        })

    })

})();