Greasy Fork

豆瓣电影磁力链

在 https://movie.douban.com/ 中进入电影的详细页时,在电影海报下方自动添加与当前电影匹配的磁力链。

目前为 2018-06-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         豆瓣电影磁力链
// @namespace    http://tampermonkey.net/
// @version      2.18.0602.07
// @description  在 https://movie.douban.com/ 中进入电影的详细页时,在电影海报下方自动添加与当前电影匹配的磁力链。
// @author       anntiza
// @match        https://movie.douban.com/subject/*
// @connect      www.wcs123.com
// @connect      www.btanf.com
// @connect      xiguacili.net
// @run-at       document-idle
// @grant        GM.xmlHttpRequest
// @grant        GM.setClipboard
// @grant        GM_setValue
// @grant        GM.setValue
// @grant        GM_getValue
// @grant        GM.getValue
// ==/UserScript==

(function() {
    'use strict';

    let searchSite = ["xiguacili","wcs123","btanf"];
    let movieSite ={
        douban:{
            url: /movie\.douban\.com/,
            insertNode : "#interest_sect_level",
            key:function(){
                return document.querySelector("div#content h1 span").innerText.split(" ")[0];
            }
        }
    }

    function initTableHead(key){
        let table = document.createElement("table");
        table.setAttribute("style", "width: 100%; padding: 0; margin-bottom: 3px;");
        let thead = table.createTHead();
        let trh = document.createElement("tr");
        let th1 = document.createElement("th");
        th1.innerText = "操作";
        th1.setAttribute("style", "border:1px dashed #dddddd; text-align:center; width:45px; padding:5px; color:#007722; font-size:14px; padding:8px;");
        trh.appendChild(th1);

        let th2 = document.createElement("th");
        th2.innerText = "热度";
        th2.setAttribute("style", "border:1px dashed #dddddd; text-align:center; width:45px; padding:5px; color:#007722; font-size:14px; padding:8px;");
        trh.appendChild(th2);

        let th3 = document.createElement("th");
        th3.innerText = "大小";
        th3.setAttribute("style", "border:1px dashed #dddddd; text-align:center; width:70px; padding:5px; color:#007722; font-size:14px; padding:8px;");
        trh.appendChild(th3);

        let th4 = document.createElement("th");
        let search = document.createElement("div");
        search.innerText = "搜索: ";
        let selectBox = document.createElement("select");
        console.log(searchSite);
        if(searchSite){
            let loc = GM_getValue("search_index", "wcs123");
            searchSite.forEach((item,index)=>{
                console.log(item,index);
                let opt = new Option(item,item);
                console.log("item=loc",item,loc);
                if(item == loc){
                    console.log("相等了。");
                    opt.setAttribute("selected", "selected");
                }
                selectBox.options.add(opt);
            });
        }

        search.appendChild(selectBox);
        th4.appendChild(search);
        th4.setAttribute("style", "border:1px dashed #dddddd; text-align:center; padding:5px; color:#007722; font-size:14px; padding:8px;");
        trh.appendChild(th4);
        thead.appendChild(trh);
        table.appendChild(thead);
        selectBox.addEventListener("change", function (e) {
            GM_setValue("search_index", this.value);
            console.log("當前選項:"+ this.value);
            let data = getSearchIndex(key,table)
            //pushDataToTableBody(data);
            });
        return table;
    }

    function initLocalSite(){
        let keys = Object.keys(movieSite);
        let loc =null;
        keys.forEach(function(item,index){
            let currentSite = movieSite[keys[index]];
            console.log(currentSite);
            if(currentSite.url && currentSite.url.test(location.href)){
                console.log(currentSite);
                loc=currentSite;
            }
        });
        return loc;
    }

    function showTableHead(lo,table){
        console.log("站點信息::"+lo.insertNode);
        let insNode = document.querySelector(lo.insertNode);
        console.log("播放點: "+insNode);
        let keyword = lo.key();
        console.log(keyword);
        console.log(table);
        insNode.parentNode.insertBefore(table,insNode);
    }

    function getSearchIndex(key,table){
        let item =GM_getValue("search_index", "wcs123"); //.then(function(item){
        console.log("当前引擎:"+item);
        var search = magnetsData[item];
        // console.log("当前函数:"+ search);
        let dataBody =search(key,table);
        // console.log(["showData",dataBody]);
        //pushDataToTableBody(dataBody,table);
        //});
    }

    function pushDataToTableBody(data,table){
        console.log("pushDataToTableBody");
        console.log(data.length);
        console.log("pushDataToTableBody-datashow end");
        let old_tbody = table.querySelector("tbody");
        if(old_tbody){
            table.removeChild(old_tbody);
            console.log("delet old tbody");
        }
        let tbody = table.createTBody();
        console.log("create new tbody");
        if(data.length){
            console.log("have data length");
        }else{
            console.log("have not data length");
        }
        if (data.length) {
            data.forEach(function (item, index) {
                let tr = document.createElement("tr");
                let td1 = document.createElement("td");
                let copy_magnet = document.createElement("a");
                copy_magnet.href = item.magnet;
                copy_magnet.innerText = "复制";
                copy_magnet.title = "单击此处复制磁力链接到剪贴板";
                copy_magnet.onclick = (event)=> {
                    event.target.innerHTML = '<span style="color:#3c763d">成功</span>';
                    GM.setClipboard(event.target.href);
                    setTimeout(()=>{
                        event.target.innerHTML = "复制";
                    }, 3000);
                    event.preventDefault(); //阻止跳转
                };
                td1.appendChild(copy_magnet);
                td1.setAttribute("class", "pl");
                td1.setAttribute("style", "border:1px dashed #dddddd; text-align:center; padding:8px;");
                tr.appendChild(td1);
                let td2 = document.createElement("td");
                td2.innerHTML = item.hot;
                td2.setAttribute("class", "pl");
                td2.setAttribute("style", "border:1px dashed #dddddd; padding:8px;");
                tr.appendChild(td2);
                let td3 = document.createElement("td");
                let open_url = document.createElement("a");
                open_url.href = item.url;
                open_url.innerText = item.size;
                open_url.target = "_blank";
                open_url.title = "打开磁力链接目标网页";
                td3.appendChild(open_url);
                td3.setAttribute("class", "pl");
                td3.setAttribute("style", "border:1px dashed #dddddd; padding:8px;");
                tr.appendChild(td3);
                let td4 = document.createElement("td");
                let mov_title = document.createElement("a");
                mov_title.innerText = item.title;
                mov_title.title = item.title;
                mov_title.href = item.magnet;
                mov_title.target = "_blank";
                td4.appendChild(mov_title);
                td4.setAttribute("class", "pl");
                td4.setAttribute("style", "border:1px dashed #dddddd; padding:8px;");
                tr.appendChild(td4);
                tbody.appendChild(tr);

            });
            //table.appendChild(tbody);
        }
        table.appendChild(tbody);
        console.log(table);
    }

    function parseText(text) {
        let doc = null;
        try {
            doc = document.implementation.createHTMLDocument("");
            doc.documentElement.innerHTML = text;
            return doc;
        }
        catch (e) {
            //  alert("parse error");
        }
    }


    var magnetsData={
        wcs123:function(key,table){
            let data =[];
            GM.xmlHttpRequest({
                method: "GET",
                url: `http://www.wcs123.com/s/${key}-hot-desc-1`,
                onload: function(response) {
                    // console.log(response.responseText);
                    let magHtml = parseText(response.responseText);
                    let mags = magHtml.querySelectorAll("td.x-item");
                    console.log("数量:",mags.length);
                    if(mags){
                        mags.forEach(function (item, index) {
                            let title = item.querySelector("h3 a").innerText;
                            console.log("title.",index, title);
                            if(title.indexOf(key) != -1){
                                data.push({
                                    "title": item.querySelector("h3 a").innerText+index,
                                    "url": "http://www.wcs123.com" + item.querySelector("h3>a").href,
                                    "magnet": item.querySelector("span>a").href,
                                    "hot": item.querySelector("span:nth-child(5)>b").innerText,
                                    "size": item.querySelector("span:nth-child(6)>b").innerText
                                });
                            }
                        });
                    }
                    if(!data.length){
                        data.push({
                            "title": "当前电影无查询结果,更换其他磁力链服务器试试!",
                            "url": "#",
                            "magnet": "",
                            "hot": "",
                            "size": ""
                        });
                    }
                    console.log("data.length:",data.length);
                    pushDataToTableBody(data,table);
                },
                onerror:function(ex){
                    console.log(`獲取磁力鏈資源異常:${ex}`);
                    data.push({
                        "title": "磁力链资源服务器异常,请更换或重试!",
                        "url": "#",
                        "magnet": "",
                        "hot": "",
                        "size": ""
                    });
                    pushDataToTableBody(data,table);
                }
            });
            console.log("wcs123:",data);
        },

        btdb:function(key,table){
            let data =[];
            GM.xmlHttpRequest({
                method: "GET",
                url: `http://btdb.to/q/${key}/`,
                onload: function(response) {
                    // console.log(response.responseText);
                    let magHtml = parseText(response.responseText);
                    let mags = magHtml.querySelectorAll("li.search-ret-item");
                    console.log("数量:",mags.length);
                    if(mags){
                        mags.forEach(function (item, index) {
                            let title = item.querySelector("h2.item-title a").innerText;
                            console.log("title."+title);
                            if(title.indexOf(key) != -1){
                                data.push({
                                    "title": title,
                                    "url": "http://btdb.to" + item.querySelector("h2.item-title a").href,
                                    "magnet": item.querySelector("div.item-meta-info a.magnet").href,
                                    "hot": item.querySelector("div.item-meta-info span:nth-child(5)").innerText,
                                    "size": item.querySelector("div.item-meta-info span:nth-child(2)").innerText
                                });
                            }
                        });
                    }
                    if(!data.length){
                        data.push({
                            "title": "当前电影无查询结果,更换其他磁力链服务器试试!",
                            "url": "#",
                            "magnet": "",
                            "hot": "",
                            "size": ""
                        });
                    }
                    console.log("http://btdb.to",data.length);
                    pushDataToTableBody(data,table);
                },
                onerror:function(ex){
                    console.log(`獲取磁力鏈資源異常:${ex}`);
                    data.push({
                        "title": "磁力链资源服务器异常,请更换或重试!",
                        "url": null,
                        "magnet": null,
                        "hot": null,
                        "size": null
                    });
                    pushDataToTableBody(data,table);
                }
            });
            console.log("btdb",data);
        },
        xiguacili:function(key,table){
            let data =[];
            GM.xmlHttpRequest({
                method: "GET",
                url: `http://xiguacili.net/xigua/${key}.html`,  //http://xiguacili.net/xigua/${key}.html
                onload: function(response) {
                    // console.log(response.responseText);
                    let magHtml = parseText(response.responseText);
                    let mags = magHtml.querySelectorAll("div.row:nth-child(7) div.ffbox");
                    console.log("数量:",mags.length);
                    console.log(mags);
                    if(mags){
                        mags.forEach(function (item, index) {
                            let title = item.querySelector("div.title h3>a").innerText;
                            console.log("title."+title);
                            if(title.indexOf(key) != -1){
                                data.push({
                                    "title": title,
                                    "url": item.querySelector("div.title h3 a").href,
                                    "magnet": item.querySelector("div.categorybar span:nth-child(6)>a").href,
                                    "hot": item.querySelector("div.categorybar span:nth-child(5) > b").innerText,
                                    "size": item.querySelector("div.categorybar span:nth-child(3) > b").innerText
                                });
                            }
                        });
                    }
                    if(!data.length){
                        data.push({
                            "title": "当前电影无查询结果,更换其他磁力链服务器试试!",
                            "url": "#",
                            "magnet": "",
                            "hot": "",
                            "size": ""
                        });
                    }
                    console.log("http://xiguacili",data.length);
                    pushDataToTableBody(data,table);
                },
                onerror:function(ex){
                    console.log(`獲取磁力鏈資源異常:${ex}`);
                    data.push({
                        "title": "磁力链资源服务器异常,请更换或重试!",
                        "url": null,
                        "magnet": null,
                        "hot": null,
                        "size": null
                    });
                    pushDataToTableBody(data,table);
                }
            });
            console.log("xiguacili",data);
        },
        btanf:function(key,table){
            let data =[];
            GM.xmlHttpRequest({
                method: "GET",
                url: `http://www.btanf.com/search/${key}-first-asc-1`,
                onload: function(response) {
                    // console.log(response.responseText);
                    let magHtml = parseText(response.responseText);
                    let mags = magHtml.querySelectorAll("div.search-item");
                    console.log("数量:",mags.length);
                    console.log(mags);
                    if(mags){
                        mags.forEach(function (item, index) {
                            let title = item.querySelector("div.item-title>a").innerText;
                            console.log("title."+title);
                            if(title.indexOf(key) != -1){
                                data.push({
                                    "title": title,
                                    "url": "http://www.btanf.com"+item.querySelector("div.item-title>a").href,
                                    "magnet": item.querySelector("div:nth-child(3) > a:nth-child(5)").href,
                                    "hot": item.querySelector(" div:nth-child(3) > span:nth-child(2) > b").innerText,
                                    "size": item.querySelector("div:nth-child(3) > span:nth-child(4) > b").innerText
                                });
                            }
                        });
                    }
                    if(!data.length){
                        data.push({
                            "title": "当前电影无查询结果,更换其他磁力链服务器试试!",
                            "url": "#",
                            "magnet": "",
                            "hot": "",
                            "size": ""
                        });
                    }
                    console.log("http://xiguacili",data.length);
                    pushDataToTableBody(data,table);
                },
                onerror:function(ex){
                    console.log(`獲取磁力鏈資源異常:${ex}`);
                    data.push({
                        "title": "磁力链资源服务器异常,请更换或重试!",
                        "url": null,
                        "magnet": null,
                        "hot": null,
                        "size": null
                    });
                    pushDataToTableBody(data,table);
                }
            });
            console.log("xiguacili",data);
        },

    }


    // GM.setValue("search_index", "wcs123");
    //console.clear();
    // let sb = initSelectBox();
    let loc = GM_getValue("search_index", "wcs123");

    console.log(["loc:",loc]);
    let localSite = initLocalSite();
    let t = initTableHead(localSite.key());
    console.log("當前占點"+localSite);
    showTableHead(localSite,t);
    getSearchIndex(localSite.key(),t);

    // Your code here...
})();