Redacted Preview

Preview songs on Redacted

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Redacted Preview
// @namespace    http://benzy.org/
// @version      0.1
// @description  Preview songs on Redacted
// @author       John Bednarczyk
// @match        https://redacted.ch/torrents*
// @grant        none
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==


(function () {
    'use strict';

    // API KEY
    var apiKey = "YOUR_API_HERE"; // Enter your youtube API here keeping the quotation marks

    // URL to add video link to
    var youtubeURL = "https://www.youtube.com/watch?v=";

    // Get list of all songs
    var song = $("span").filter( function() {
        return ($(this).text().indexOf('(') > -1);
    }); // anywhere match

    // Change the text for every song, also assigns class to each song
    for(var i = 0 ; i < song.length ; i++){
        song[i].setAttribute("class","song");
        song[i].setAttribute("id","song"+i);

        // Create Button
        var button = document.createElement('button');
        button.setAttribute("class","play");

        // Set Style
        button.style.background='LightBlue';
        button.style.borderRadius = "3px";
        button.style.padding = "3px 3px 3px 3px";

        button.innerHTML = "Play";

        // Add Spacing
        song[i].appendChild(document.createTextNode(" "));

        // Add link to end of song
        song[i].appendChild(button);
    }

    // Detect button click
    $('button.play').click(function() {

        // Get Artist/songTitle
        var artist = document.getElementsByTagName("h2")[0].firstChild.text;
        var songTitle = this.previousSibling.parentElement.previousSibling.textContent;

        window.open(youtubeURL + getLink(artist, songTitle));
    });

    // Function to get a VideoID
    function getLink(artist, song){

        // Setup url for api
        var url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' +
            encodeURIComponent(artist) + "-" +
            encodeURIComponent(song) + '&maxResults=1&fields=items(id(videoId))&key=' + apiKey;

        // call api and get videoId
        var xhReq = new XMLHttpRequest();
        xhReq.open("GET", url, false);
        xhReq.send(null);
        var id = JSON.parse(xhReq.responseText);

        return id.items[0].id.videoId;
    }
})();