Greasy Fork

Greasy Fork is available in English.

动漫花园提取BT磁力链

提取、选择并复制动漫花园当前搜索结果的BT磁力链

当前为 2019-01-28 提交的版本,查看 最新版本

// ==UserScript==
// @name         动漫花园提取BT磁力链
// @description  提取、选择并复制动漫花园当前搜索结果的BT磁力链
// @require      https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
// @author       YD-Cat
// @version      1.21
// @namespace    http://greasyfork.icu/zh-CN/users/242083-yd-cat
// @copyright    2019+,  YD-Cat
// @include      http*://share.dmhy.org/*
// ==/UserScript==
var $ = jQuery.noConflict();
var clickFun=function(){

    //显示窗口
    $("#extract").click(function(){
        var magnetObjs=$('.download-arrow.arrow-magnet')
        var titleObjs=$('td.title>a')

        //显示或隐藏
        if($('#magnetBox').css('display')!='block'){
            //清空内容
            $('#magnetUrl tbody').text('')
            $('#magnetBox span').text($('#magnetUrl :checked').length)
            //没有资源
            if(magnetObjs.length<=0){
                let str='<tr style="text-align: center;" class="even"><td></td><td>没有资源</td><td></td></tr>';
                $("#magnetUrl tbody").append(str)
                $('#magnetBox').fadeIn(200)
                let left=window.innerWidth/2-$('#magnetUrl')[0].offsetWidth/2;
                let top=window.innerHeight*0.2;
                $('#magnetBox').css({
                    'left':left,
                    'top':top
                })
            }
            else{
                for(var i=0;i<magnetObjs.length;i++){
                    let str='<tr style="text-align: center;" class="even"><td style="user-select: none;">'+ ++i +'</td><td class="align myTitle">'+titleObjs[--i].innerText+'<td class="align" style="display:none;">'+magnetObjs[i].href.substring(0,magnetObjs[i].href.indexOf('&dn'))+'</td><td><input type="checkBox"></td></tr>'
                    $("#magnetUrl tbody").append(str)
                }
                $('#magnetBox').fadeIn(200)
                let left=window.innerWidth/2-$('#magnetUrl')[0].offsetWidth/2;
                let top=window.innerHeight*0.2;
                $('#magnetBox').css({
                    'left':left,
                    'top':top
                })
            }
        }
        else{
            $('#magnetBox').fadeOut(200)
        }
    })

    //全选
    $("#selectAll").click(function(){
        var flag=stringToBoolean($('#selectAll').attr('flag'))
        $('#magnetUrl [type="checkBox"]').each(function(){this.checked= !flag})
        $('#magnetBox span').text($('#magnetUrl :checked').length)
        $('#selectAll').attr('flag',!flag)
    })
    //反选
    $('#selectInvert').click(function(){
        $('#magnetUrl [type="checkBox"]').each(function(){this.checked=!this.checked})
        $('#magnetBox span').text($('#magnetUrl :checked').length)
        isSelectAll()
    })
    //关闭窗口
    $("#close").click(function(){
        $('#magnetBox').fadeOut(200)
    })
    //复制已选
    $('#copySelect').click(function(){
        let str='';
        $('#magnetUrl input').each(function(){
            if(this.checked){
                str+=this.parentNode.previousElementSibling.innerText+'\n'
            }
        })
        copy(str)
    })
    //复制全部
    $('#copyAll').click(function(){
        var parents=$('#magnetUrl input').parent();
        var brothers=parents.prev();

        let str='';
        brothers.each(function(i, brother){
            str+=brother.innerText+'\n'
        })
        copy(str)
    })

    //给选择框绑定事件
    window.onload=function(){
        $(document).on('click','#magnetUrl [type="checkBox"]',function(event){
            event.stopPropagation();
            $('#magnetBox span').text($('#magnetUrl :checked').length)
            isSelectAll()
        });
        $(document).on('click','#magnetUrl tbody>tr',function(){
            $(this).find('input')[0].checked=!$(this).find('input')[0].checked
            $('#magnetBox span').text($('#magnetUrl :checked').length)
            isSelectAll()
        });
    };
};
(function() {
    //添加样式
    var style='<style type="text/css">#magnetBox{font-size: 13px;position: fixed;background: white;padding: 2px;display:none;border: 1px solid #247;}#magnetUrl{max-height: 406px;;overflow: auto;}#magnetUrl thead>tr{background-color: #7e99be;color: white;font-size: 15px;}#magnetUrl th{padding:5px;}#magnetUrl tbody>tr:hover{background-color: #c5cbd3;;}#magnetUrl td{padding:6px 8px;color: #247;}.align{text-align: left;padding-left: 10px;}hr{margin:2px 0 0}#selectBox{margin:3px 2px 2px}#selectBox:after{content: "";display: block;clear: both;}#selectBox>ul{margin:0; padding:0;}#selectBox>ul>li{list-style-type: none;}#selectBox a{display:block;padding:5px;border:1px solid #247;}#selectBox>ul>li:nth-of-type(n+2){margin-left:10px;}#copyText{position:absolute;top:-9999px;z-index:-999;width:0;height:0;opacity:0;}.myTitle{max-width:500px;word-break: break-all;}</style>'
    $('.table.clear').before(style)
    //添加jQuery
//     var jquery=document.createElement('script');
//     jquery.src= 'https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js';
//     document.getElementsByClassName('table clear')[0].appendChild(jquery)
    //添加提取按钮
    var ExtractText='<div class="fl" style="margin-left: 15px;"><a href="javascript:void(0)" id="extract">提取链接</a></div>'
    //添加提取界面
    var TextBox='<div id="magnetBox"><div id="magnetUrl"><table border="0"><thead><tr><th width="30">序列</th><th style="min-width:400px;">标题</th><th style="display:none;">磁力链接</th><th width="30">选择</th></tr></thead><tbody></tbody></table></div><textarea id="copyText"></textarea><hr><div id="selectBox"><ul><li class="fl" id="copySelect"><a href="javascript:void(0)">复制已选</a></li><li class="fl" id="copyAll"><a href="javascript:void(0)">复制全部</a></li><li class="fl" id="selectAll" flag=false><a href="javascript:void(0)">全选</a></li><li class="fl" id="selectInvert"><a href="javascript:void(0)">反选</a></li><li class="fl" style="color:#666;line-height:28px">已选择:<span>0</span>项</li><li class="fr" id="close"><a href="javascript:void(0)">关闭</a></li></ul></div>'

    $('.nav_title').find('.fl').after(ExtractText)
    $('.table.clear .nav_title:first').after(TextBox)
    //点击事件
    clickFun();
})();
//String转换为Boolean
function stringToBoolean(str){
    switch(str.toLowerCase())
    {
        case "true": case "yes": case "1": return true;
        case "false": case "no": case "0": case null: return false;
        default: return Boolean(str);
    }
}
//复制事件
function copy(str){
    str=str.substring(0,str.length-1)
    $('#copyText').val(str)
    $('#copyText').select()
    document.execCommand('copy')
}
//检测是否全选择
function isSelectAll(){
    if(Number($('#magnetBox span').text())==$('#magnetUrl [type="checkBox"]').length){
        $('#selectAll').attr('flag',true)
    }
    else{
        $('#selectAll').attr('flag',false)
    }
}