Add magnet to 115

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Add magnet to 115
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Jin Liu
// @match        http*://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    function add_magnet_to_115(magnet) {
        GM_xmlhttpRequest({
            method: "POST",
            url: "http://localhost:3333/add-magnet-to-115",
            data: "param1=" + magnet,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            onload: function (response) {
                if(response.status == 200) {
                    alert("Added magnet to 115 successfully.");
                } else {
                     alert(response.responseText);
                }
            },
            onerror: function (response) {
                alert(response.responseText);
            }
        });
    }

    // 有些链接是动态加载的,所以设定了一个延时
    setTimeout(function() {
        inject();
    }, 2000);

    function inject() {
        var links = document.querySelectorAll('a');
        Array.prototype.forEach.call(links, function(link) {
            var href;
            if (link.hasAttribute('href')) {
                href = link.getAttribute('href');
                if (href.startsWith("magnet:?") || href.startsWith("thunder://") || href.startsWith("ed2k://")) {
                    var ele = document.createElement('a');
                    ele.style.cssText = 'background: #2777F8 !important; border-radius: 3px !important; color: white !important; padding: 3px !important; margin: 2px !important; cursor: pointer !important';
                    ele.innerHTML = '115';
                    ele.addEventListener('click', function (e) {
                        add_magnet_to_115(href);
                        e.stopPropagation();
                    }, false);
                    link.parentNode.insertBefore(ele, link);
                }
            }
        });

    }

})();