Add a “Random” button to TV Shows in Ororo.tv

userscript console-script

Originally posted as GitHub Gist on 08 Mar 2018 , updated at 16 Nov 2020 (details)


Source: https://gist.github.com/paulera/50769789249564c6fbdc86ab4b740b9e

Description: Tampermonkey script that adds a "Random" button to TV Shows episodes list in the streaming service ororo.tv


ororotv-random_episode_button.js

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
/***
This script works only in ororo.tv. It adds a "Random" button in "TV Show" episodes list.

To activate it:

method 1: On a TV Show episodes list page, open the console, copy and paste all this code, and press enter.

method 2: Create an entry in your bookmarks called "Ororo random episode" with the url exacty as below. Use it in TV Show episodes list.
javascript:function openRandomEpisode(){do{episodeInfo=getRandomEpisode()}while(window.episodes.length&&window.prioritizeUnwatched&&episodeInfo.watched&&!window.allEpisodesWatched);0==window.episodes.length&&episodeInfo.watched&&(console.log("Could not find an unwatched episode"),window.allEpisodesWatched=!0),activateSeason(episodeInfo.season),scrollToEpisode(episodeInfo),console.log("Start episode: Season "+episodeInfo.season+" Episode "+episodeInfo.number),episodeInfo.linkElement.click()}function getRandomEpisode(){return window.getRandomEpisodeCount++,console.log("Looking for a random episode (try #"+window.getRandomEpisodeCount+")"),0==window.episodes.length&&(window.episodes=$(".show-content__episode-list .js-season-tab .js-episode-wrapper .episode-box"),console.log("Acquiring list of episodes"),window.getRandomEpisodeCount=0),episodeIndex=Math.floor(Math.random()*window.episodes.length),$episode=$(window.episodes.splice(episodeIndex,1)),episodeInfo={element:$episode,season:$episode.parent().parent().parent().attr("id"),number:parseInt($episode.parent().find(".js-num-episode").html()),watched:$episode.find(".mark-watched").attr("class").indexOf(" watched")>0,linkElement:$episode.find("a.js-episode")},console.log("Chosen episode:"),console.log(episodeInfo),episodeInfo.watched?console.log("WATCHED"):console.log("NEW"),episodeInfo}function activateSeason(e){console.log("Activate season "+e),$($(".show-content__season-list").children()[e-1]).find("a").click()}function scrollToEpisode(e){console.log("Scrolling to episode"),duration=400,$("html,body").animate({scrollTop:e.element.offset().top-e.element.height()-$(".site-header").height(),easing:"swing"},duration),$(".show-content__episode-list .js-season-tab .js-episode-wrapper").css("background-color","transparent"),e.element.parent().css("background-color","#ddeaff")}window.episodes=[],window.getRandomEpisodeCount=0,window.allEpisodesWatched=!1,window.prioritizeUnwatched=!1,$(document).ready(function(){setTimeout(function(){$('<span class="random"><div class="ui button show-trailer__button"><i title="Random" class="fa fa-random"></i>&nbsp;Random</div></span>').insertAfter(".poster-image").click(openRandomEpisode),$(".show-trailer").css("display","inline").css("margin-bottom","15px"),$(".random").css("display","inline-block").css("margin-bottom","15px")},50)});

method 3: Use the extension Tampermonkey (Chrome) or Greasemonkey (Firefox) and add this whole code as a new userscript.

This is provided "as it is" with no license, support whatsoever.
***/

// ==UserScript==
// @name         Ororo.tv random episode
// @namespace    http://paulera.com.br
// @version      1.0
// @description  Add a "Random" button in ororo.tv episodes list for TV Shows
// @author       Paulo Amaral
// @match        https://ororo.tv/en/shows/*
// @grant        none
// ==/UserScript==

window.episodes = [];
window.getRandomEpisodeCount = 0;
window.allEpisodesWatched = false;
window.prioritizeUnwatched = false; // set to true to pick from unwatched first if any

function openRandomEpisode() {
    do {
        episodeInfo = getRandomEpisode();
    } while (window.episodes.length && window.prioritizeUnwatched && episodeInfo.watched && !window.allEpisodesWatched);

    if (window.episodes.length == 0 && episodeInfo.watched) {
        console.log("Could not find an unwatched episode");
        window.allEpisodesWatched = true;
    }

    activateSeason(episodeInfo.season);
    scrollToEpisode(episodeInfo);

    console.log("Start episode: Season " + episodeInfo.season + " Episode " + episodeInfo.number);
    episodeInfo.linkElement.click();
}


function getRandomEpisode() {

    window.getRandomEpisodeCount++;
    console.log("Looking for a random episode (try #" + window.getRandomEpisodeCount + ")");

    if (window.episodes.length == 0) {
        window.episodes = $(".show-content__episode-list .js-season-tab .js-episode-wrapper .episode-box");
        console.log("Acquiring list of episodes");
        window.getRandomEpisodeCount = 0;
    }

    episodeIndex = Math.floor(Math.random() * window.episodes.length);
    $episode = $(window.episodes.splice(episodeIndex, 1));
    episodeInfo = {
        "element": $episode,
        "season": $episode.parent().parent().parent().attr('id'),
        "number": parseInt($episode.parent().find(".js-num-episode").html()),
        "watched": $episode.find(".mark-watched").attr('class').indexOf(" watched") > 0,
        "linkElement": $episode.find("a.js-episode")
    };

    console.log("Chosen episode:");
    console.log (episodeInfo);

    if (episodeInfo.watched) {
        console.log ("WATCHED");
    } else {
        console.log ("NEW");
    }

    return episodeInfo;
}

function activateSeason(seasonNumber) {
    console.log("Activate season " + seasonNumber);
    $($(".show-content__season-list").children()[seasonNumber - 1]).find("a").click();
}

function scrollToEpisode(episodeInfo) {
    console.log("Scrolling to episode");
    duration = 400; // miliseconds
    $('html,body').animate({
        scrollTop: episodeInfo.element.offset().top - episodeInfo.element.height() - $(".site-header").height(),
        easing: 'swing'
    }, duration);
    $(".show-content__episode-list .js-season-tab .js-episode-wrapper").css('background-color','transparent');
    episodeInfo.element.parent().css('background-color','#ddeaff');
}

$(document).ready(function() {
    setTimeout(function() {
        $('<span class="random"><div class="ui button show-trailer__button"><i title="Random" class="fa fa-random"></i>&nbsp;Random</div></span>')
            .insertAfter(".poster-image").click(openRandomEpisode);
        $('.show-trailer').css('display', 'inline').css('margin-bottom','15px');
        $('.random').css('display', 'inline-block').css('margin-bottom','15px');
    }, 50);
});