Greasy Fork

Greasy Fork is available in English.

1337X - Magnet links to torrent search

Adds magnet links directly to the torrent search results

当前为 2018-10-14 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         1337X - Magnet links to torrent search
// @namespace    NotNeo
// @version      0.2
// @description  Adds magnet links directly to the torrent search results
// @author       NotNeo
// @match        https://1337x.st/search/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

	var intervalHandle;

    $(function() {
        addGlobalStyle(`
			main.container {
				max-width: 1600px;
			}
			table.table-list td.coll-5 {
				border-right: 1px solid silver;
			}
			table.table-list td.dl-buttons {
				border-left: 1px solid #f6f6f6;
				padding-left: 5px;
				padding-right: 5px;
				text-align: center;
				position: relative;
			}
			td.dl-buttons > a.list-dl-button {
				color: inherit;
				text-decoration: none;
				cursor: pointer;
				display: inline-block;
			}
			.list-dl-button > i.flaticon-magnet {
				font-size: 13px;
				color: #89ad19;
			}
			span.fetchNotifier {
				display: inline-block;
				position: absolute;
				z-index: 999;
				padding: 2px 8px;
				left: 45px;
				top: 0px;
				width: 140px;
				background-color: white;
				border: 1px black solid;
				border-radius: 20px;
			}

		`);


        $("table.table-list thead tr").append('<th class="coll-6">download</th>'); //adding the dowload thead
        $("table.table-list tbody tr").each(function(){ //adding all download buttons
            $(this).append('<td class="coll-6 dl-buttons"><a href="#" class="list-dl-button" title="Download via magnet"><i class="flaticon-magnet"></i></td>');
        });

        $("a.list-dl-button[href='#']").mouseover(MouseOverFunc); //adding mouseover events for all

		$("a.list-dl-button").click(function(e){ //click event for all
            var linkElem = $(this); //saving proper this to avoid scope mistakes
            clearInterval(intervalHandle); //clearing possible old intervals
            $(".fetchNotifier").remove(); //removing possible old messages
			if(linkElem.prop("href").substring(0, 6) != "magnet") { //if the link hasn't loaded yet
				e.preventDefault();
                linkElem.after('<span class="fetchNotifier" style="display: none;">Fetching magnet link...</span>');//adding message
                linkElem.next(".fetchNotifier").fadeIn(200);
				intervalHandle = setInterval(function(){ CheckForMagnet(linkElem); }, 100); //starting link checker interval
			} //else default action, i.e. opens the link
		});
    });

	function CheckForMagnet(theLink) {
		if(theLink.prop("href").substring(0, 6) == "magnet") { //if the link has been loaded
			clearInterval(intervalHandle);
            theLink.next(".fetchNotifier").fadeOut(300, function() { //hiding, then removing message
                $(this).remove();
            });
			location.href = theLink.prop("href"); //opening link
		}//else interval will call this function again
	}

    function MouseOverFunc() {
        var thisElem = $(this);//saving this element to avoid scope mistakes
        thisElem.unbind("mouseover"); //mouse over for this element wont be needed unless load fails
        thisElem.prop("title", "Download via magnet (fetching magnet link...)");
        var url = thisElem.parent().parent().find("td.name > a[href^='/torrent/']").prop("href"); //getting url
        $.get(url, function(res) { //ajax
            let result = $("a[href^='magnet:?']:first", res).prop("href");
            thisElem.prop("href", result);
            thisElem.prop("title", "Download via magnet");
        }).fail(function(x, status){
            console.log("error getting magnet link: " + status + "\nFor: " + thisElem.prop("href")); //printing error to console
            thisElem.mouseover(MouseOverFunc); //re-initializing mouseover event if load failed
        });
    }

	function addGlobalStyle(css) {
		var head, style;
		head = document.getElementsByTagName('head')[0];
		if (!head) { return; }
		style = document.createElement('style');
		style.type = 'text/css';
		style.innerHTML = css;
		head.appendChild(style);
	}

})();