bs.to autoplay

auto play. sit back and relax.

Ajankohdalta 14.4.2020. Katso uusin versio.

// ==UserScript==
// @name         bs.to autoplay
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  auto play. sit back and relax.
// @author       xZaheer
// @match    https://bs.to/*
// @match    https://*.vivo.sx/*
// @grant    GM_setValue
// @grant    GM_getValue
// @grant    GM_deleteValue
// @grant    GM_openInTab
// @grant    window.close
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// ==/UserScript==

class VivoHandler {
    constructor() {
        if(window.location.href.includes("vivo.sx")) {
            setTimeout(this.initEvents.bind(this), 2000);
        }
    }

    initEvents() {
        if(window.location.href.includes("https://vivo.sx")) {
            this.play();
        } else {
            this.resize();
            this.onEnd();
        }
    }

    play() {
        var source = document.getElementsByTagName('body')[0].innerHTML;
        if (source != null) {
            source = source.replace(/(?:.|\n)+Core\.InitializeStream\s*\(\s*\{[^)}]*source\s*:\s*'(.*?)'(?:.|\n)+/, "$1");
            var toNormalize = decodeURIComponent(source);
            var url = ""
            for (var i = 0; i < toNormalize.length; i++) {
                var c = toNormalize.charAt(i);
                if (c != ' ') {
                    var t = (function (c) { return c.charCodeAt == null ? c : c.charCodeAt(0); })(c) + '/'.charCodeAt(0);
                    if (126 < t) {
                        t -= 94;
                    }
                    url += String.fromCharCode(t);
                }
            }
            if (!url.toLowerCase().startsWith("http")) {
                alert("Vivo-Script Defect!");
                return;
            }
        }
        GM_setValue("playing", true);
        window.location.href = url;
    }

    resize() {
        let video = document.querySelector("video");
        video.style.width = "100%";
        video.style.height = "100%";

    }

    onEnd() {
        let video = document.querySelector("video");
        video.onended = function(){
            window.location.href = GM_getValue("lastSession");
        }
    }

}

class BsHandler {
    constructor() {
        this.buttonElem = null;
        if(window.location.href.includes("bs.to")) {
            this.initEvents();
        }
    }

    initEvents() {
        if(this.isLoggedIn()) {
            this.addAutoPlayButton();
            GM_deleteValue("playing");
        }

        if(window.location.href.includes("bs.to/serie") && this.isRunning() && this.isEpisodeView()){
            this.goNextEpisode();
        }

        if(this.isRunning() && !this.isEpisodeView() && this.isEpisodeSelected()){
            if(this.isVivoAvailable()) {
                this.clickPlay();
            }
        }

        this.closeOnVideoLoaded()
    }

    addAutoPlayButton() {
        let location = document.querySelector("#root > header > section");
        this.buttonElem = document.createElement('a');
        this.buttonElem.innerHTML = this.isRunning() ? "Deactivate Auto Play" : "Activate Auto Play";
        this.buttonElem.addEventListener('click', this.isRunning() ? this.deactivateAutoPlay : this.activateAutoPlay);
        location.appendChild(this.buttonElem);
    }

    isLoggedIn() {
        let logoutButtonElem = document.querySelector("#root > header > section > a:nth-child(4)");
        return (logoutButtonElem) ? true : false;
    }

    activateAutoPlay() {
        GM_setValue("status", "running");
        location.reload();

    }

    deactivateAutoPlay() {
        GM_deleteValue("status");
        location.reload();
    }

    isRunning() {
        return (GM_getValue("status") === "running") ? true : false;
    }

    goNextEpisode() {
        let nextEpisode = document.querySelector("#root > section > table > tbody > tr:not(.watched)");
        if(nextEpisode) {
            let nextEpisodeUrl = nextEpisode.querySelector("td:nth-child(2) > a:nth-child(2)").href;
            window.location.href = nextEpisodeUrl;
            GM_setValue("lastSession", window.location.href);
            return;
        }
        let nextSession = document.querySelector("#seasons > ul > .active + li > a").href;
        if(nextSession) {
            window.location.href = nextSession;
            return;
        }
    }

    isEpisodeView() {
        let checkElem = document.querySelector("#root > section > table > tbody > tr:nth-child(1) > td:nth-child(2) > a:nth-child(2) > strong");
        return (checkElem) ? true : false;
    }

    isEpisodeSelected() {
        let checkElem = document.querySelector("#root > section > ul.hoster-tabs.top");
        return (checkElem) ? true : false;

    }

    isVivoAvailable() {
        let vivoButton = document.querySelector("#root > section > ul.hoster-tabs.top > li > a > i.vivo");
        if(!vivoButton) {
            return false;
        } else {
            return true;
        }
    }

    clickPlay() {
        // setTimeout needed because loading time of js libs
        setTimeout(() => {
            let playerElem = document.querySelector("section.serie .hoster-player");
            let clickEvent = new Event("click");
            clickEvent.which = 1;
            clickEvent.pageX = 1;
            clickEvent.pageY = 1;
            playerElem.dispatchEvent(clickEvent);

        }, 1000)
    }

    closeOnVideoLoaded() {
        setInterval(() => {
            if(GM_getValue("playing")) {
                window.close();
            }
        }, 1000)
    }
}


(function() {
    'use strict';
    let vivoHandler = new VivoHandler();
    let bsHandler = new BsHandler();

})();