ygg

make a link to the torrent into the front page

目前為 2020-01-10 提交的版本,檢視 最新版本

// ==UserScript==
// @name         ygg
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  make a link to the torrent into the front page
// @author       You
// @include        *://*.yggtorrent.ws/*
// @grant        none
// @require https://greasyfork.org/scripts/394721-w84kel/code/w84Kel.js?version=763614
// ==/UserScript==

(function() {
    'use strict';
    waitForKeyElements( "#\\#torrents", el => display(el), false );
})();

function display() {
    let el = arguments[0];
    let movies = [];
    let rows = el.querySelector('table').rows;

    for (let i = 1; i < rows.length; i++) {
        let movieCell = rows[i].cells[1];
        let movieLink = movieCell.childNodes[0];
        let movieLinkButton = document.createElement('button');
        let movieId = movieLink.href.split("/").pop().split("-")[0];
        movieCell.classList.add('movieCell');
        movieLink.classList.add('movieLink');
        movieLinkButton.innerText = "download";
        movieLinkButton.classList.add('directLink');
        movieLinkButton.onclick = function() {torrentLink(movieLink.href, movieId)};
        movieCell.appendChild(movieLinkButton);
        movies.push(movieCell);
    }

}

function torrentLink(ref, id) {
    fetch("https://www2.yggtorrent.ws/engine/download_torrent?id=" + id, {
        "credentials":"include",
        "headers":{
            "accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "accept-language":"fr-FR,fr;q=0.9,en;q=0.8,en-US;q=0.7",
            "sec-fetch-mode":"navigate",
            "sec-fetch-site":"same-origin",
            "sec-fetch-user":"?1",
            "upgrade-insecure-requests":"1"
        },
        "referrer":ref,
        "referrerPolicy":"no-referrer-when-downgrade",
        "body":null,
        "method":"GET",
        "mode":"cors"
    })
        .then(rep => rep.blob())
        .then(blob => {
        var url = window.URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = url;
        a.download = "filename.torrent";
        document.body.appendChild(a); //we need to append the element to the dom -> otherwise it will not work in firefox
        a.click();
        a.remove(); //afterwards we remove the element again
    });
}