Rai Play video download

This script allows you to watch and download videos on Rai Play.

// ==UserScript==
// @name         Rai Play video download
// @namespace    http://andrealazzarotto.com
// @version      10.0.2
// @description  This script allows you to watch and download videos on Rai Play.
// @author       Andrea Lazzarotto
// @match        https://www.raiplay.it/*
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/foundation-essential/6.2.2/js/foundation.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @grant        none
// @license      GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

(function() {
    'use strict';

    var $ = window.jQuery;
    var Foundation = window.Foundation;
    var download_icon = '<svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg"><path d="M42 40v4H6v-4h36zM20 24v-8h8v8h7L24 37 13 24h7zm8-14v4h-8v-4h8zm0-6v4h-8V4h8z" /></svg>';

    var showModal = (title, content) => {
        var modal = $(`
            <div id="video-download-modal" class="small reveal" data-reveal aria-labelledby="Download video">
                <h2 id="modal-title">${title}</h2>
                <div id="modal-content"></div>
                <button class="close-button" data-close aria-label="Chiudi finestrella" type="button">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        `);
        modal.css({
            'background-color': '#001623',
        });
        modal.find('#modal-content').append(content);
        var instance = new Foundation.Reveal(modal);
        instance.open();
        modal.find('.close-button').css({
            'color': 'white',
        }).click(() => instance.close());
    };

    var getVideo = () => {
        var path = location.href.replace(/\.html(\?.*)?$/, '.json');
        $.getJSON(path).then((data) => {
            return $.ajax({
                'url': 'https://video.lazza.dk/rai?r=' + data.video.content_url,
                'method': 'GET',
            }).promise();
        }).then((response) => {
            if (!response) {
                showModal('Niente da fare...', "<p>Non è stato possibile trovare un link del video in formato MP4. Il video potrebbe essere protetto da DRM.</p>");
            } else {
                showModal('Link diretto', `
                    <p>Clicca il bottone per aprire il video in formato MP4. Usa il tasto destro per salvarlo.</p>
                    <p><strong>Per i video lunghi è raccomandato l'uso di un download manager.</strong></p>
                    <p><a href="${response}" class="button" target="_blank">Video MP4</a></p>
                `);
            }
        }).fail(() => {
            showModal('Errore di rete', "<p>Si è verificato un errore di rete. Riprova più tardi.</p>");
        });
    };

    var downloadButton = () => {
        if ($('#video-download-button').length) {
            return;
        }

        $('.rai-player-infobutton').before(`
            <button id="video-download-button" class="vjs-control vjs-button" aria-disabled="false">
                <span aria-hidden="true" class="vjs-icon-placeholder">${download_icon}</span>
                <span class="vjs-control-text" aria-live="polite">Download</span>
            </button>
        `);
        $('#video-download-button').click(getVideo)
            .find('svg').css({
                'fill': '#039cf9',
                'height': '3.5em',
            });
    };

    $(document).arrive('rai-player .rai-player-infobutton', () => {
        downloadButton();
    });

    $(document).arrive('#accountPanelLoginPanel', () => {
        var anonymous = !!$('#accountPanelLoginPanel').is(':visible');
        if (anonymous) {
            $('body').on('touchstart mousedown', 'a.card-item__link', (event) => {
                location.href = $(event.currentTarget).attr('href');
            });

            $('body').on('touchstart mousedown', 'button[data-video-json]', (event) => {
                location.href = $(event.currentTarget).data('video-json').replace(/\.json/, '.html');
            });
        }
    });

    $(document).ready(() => {
        if (location.pathname.startsWith('/video')) {
            $('rai-sharing').after(`
                <a id="inline-download-button" class="cell small-4 medium-shrink highlight__share" aria-label="Download">
                    <div class="leaf__share__button button button--light-ghost button--circle float-center">${download_icon}</div>
                    <span class="button-label">Download</span>
                </a>
            `);
            $('#inline-download-button').click(getVideo);
        }
    });
})();