Enhanced dmhy

为种子列表添加支持迅雷离线的磁链,用明确的文件名直接下载种子,批量导出磁链

目前为 2016-04-07 提交的版本。查看 最新版本

// ==UserScript==
// @name        Enhanced dmhy
// @description 为种子列表添加支持迅雷离线的磁链,用明确的文件名直接下载种子,批量导出磁链
// @namespace   https://greasyfork.org/users/726
// @include     http*://share.dmhy.org/*
// @include     http*://dl.dmhy.org/*
// @version     20151007
// @grant       none
// ==/UserScript==

if(location.host=='dl.dmhy.org'){

// 用一个微妙的方法改变下载文件名,其实改名的基本思路就是使用download属性设置文件名,但是download属性似乎只支持同域名下载,所以这里需要在dl.dmhy.org这个域名下生成下载链接
if(location.pathname=='/' && location.hash.indexOf('download')==1){
    var json = location.hash.substr(10);
    json = decodeURIComponent(json);
    var argv = JSON.parse(json);
    if(argv){
        var link = document.createElement('a');
        link.href = argv.link;
        if (typeof link.download === 'string') {
            link.download = argv.name;
        }
        link.innerHTML = '<style>*{margin:0;padding:0;}</style><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAA0SURBVChTY0AG7HFL/qNjqBQmGCqKsSnAhYnWAFYIA9gUwDBUCSogWiEMEK0QBrArZGAAAJdCe+n9ocQtAAAAAElFTkSuQmCC">'; // 这里有一句粗糙的CSS Reset以及一个base64编码的箭头图片
        document.documentElement.innerHTML = link.outerHTML; // 替换掉整个页面
    }
}

}else{

// 种子列表页面
(function($){
    $('#topic_list>thead>tr>th:nth-child(4)').attr('width', '30px').after('<th width="30px"><span>离线</span></th><th width="30px"><span>种子</span></th>'); // 在表头中添加两列

    var rows = [];
    function Row(i){
        this.id = i;
        this.tr = $('#topic_list>tbody>tr').eq(i);

        this.tr.children('td:nth-child(4)').after('<td><a class="download-arrow arrow-thunder">&nbsp;</a></td><td><a class="download-arrow arrow-torrent">&nbsp;</a></td>'); // 添加两列

        this.link_title   = this.tr.find('td.title>a');       // 种子页面链接
        this.link_magnet  = this.tr.find('.arrow-magnet');    // 原始磁链
        this.link_thunder = this.tr.find('.arrow-thunder');   // 离线磁链
        this.link_torrent = this.tr.find('.arrow-torrent');   // 种子链接

        this.link_thunder.attr('href', this.link_magnet.attr('href').substr(0, 52)); // 将原来的磁链截短,只保留btih

        this.link_torrent.css('opacity', '.2'); // 将种子链接设置为半透明,表示需要加载

        // 当鼠标移入种子链接所在td时
        this.link_torrent.closest('td').mouseenter(function(){
            if(this.link_torrent_done)
                return;
            var href = this.link_torrent.attr('href');
            if(href){
                // 如果链接已经存在,直接通过iframe显示
                var iframe = $('<iframe width="12" height="12" frameborder="0"></iframe>');
                iframe.attr('src', href);
                this.link_torrent.replaceWith(iframe);
                this.link_torrent_done = true;
            }else{
                // 否则先加载数据
                this.loadData(0);
            }
        }.bind(this));
    }
    Row.prototype.loadData = function(recursive){
        if(recursive)
            $('.export_current').text(this.id);
        if(!this.tr.hasClass('loading')){
            this.tr.addClass('loading');

            // 读取种子页面,以便获取种子的下载链接和文件名
            $.get(this.link_title.attr('href'), function(html){
                // 为了避免页面元素加载,这里使用正则提取数据而不是DOM
                var torrent_link = html.match(/dl\.dmhy\.org\/([^"]+)/)[1]; // 下载链接

                // 获得文件名,这里需要能够处理包含文件夹的种子
                var torrent_name = html.match(/([^>]+?)\s*<span class="bt_file_size">/)[1];
                torrent_name = torrent_name.split('/');
                var torrent_with_dir = false;
                if(torrent_name.length>1)
                    torrent_with_dir = true;
                this.link_magnet.data('dir', torrent_with_dir);
                this.link_thunder.data('dir', torrent_with_dir);
                torrent_name = torrent_name[0];
                var dn = 'dn=' + encodeURIComponent(torrent_name);
                this.link_thunder.attr('href', this.link_thunder.attr('href')+'&'+dn);
                this.link_magnet.attr('href', this.link_magnet.attr('href').replace(/dn=[^&]*/, dn));

                torrent_name = torrent_name+'.torrent';

                // 接下来是一些谜一样的代码

                // 将下载链接和文件名放入一个对象中
                var argv = {
                    link : torrent_link,
                    name : torrent_name,
                };
                // 构造一个指向dl.dmhy.org链接,通过hash传递参数
                this.link_torrent.attr('href', 'https://dl.dmhy.org/#download#'+JSON.stringify(argv));

                if(!recursive){
                    this.link_torrent.mouseenter();
                }else{
                    if(this.id+1<rows.length){
                        setTimeout(function(){
                            rows[this.id+1].loadData(1);
                        }.bind(this), 200);
                    }else{
                        on_export_loaded();
                    }
                }

            }.bind(this));
        }else{
            if(this.id+1<rows.length){
                rows[this.id+1].loadData(1);
            }else{
                on_export_loaded();
            }
        }
    }

    // 处理每一行
    for(var i=0;i<$('#topic_list>tbody>tr').length;++i){
        rows.push(new Row(i));
    }

    if(rows.length){
        var export_loading = false;
        jQuery('.nav_title>.fl').append(' | <a download="dmhy_magnet_fast.txt" id="export_fast">快速导出磁链(可能不包含文件名)</a> | <a download="dmhy_magnet_full.txt" id="export_full" class="export_load" title="点击开始加载数据">导出磁链(包含文件名)</a> | <a download="dmhy_magnet_file.txt" id="export_file" class="export_load" title="点击开始加载数据">导出磁链(只导出单个文件的磁链)</a>');

        function export_blob(file_only){
            var text = '';
            for(var i=0;i<rows.length;++i){
                var link = rows[i].link_thunder;
                if(file_only&&link.data('dir'))
                    continue;
                text += link.attr('href') + '\r\n';
            }
            var blob = new Blob([text], {type: 'text/plain'});
            return window.URL.createObjectURL(blob);
        }

        $('#export_fast').mouseenter(function(){
            $(this).attr('href', export_blob(0));
        });

        $('.export_load').click(function(){
            if(export_loading)
                return;
            export_loading = true;
            $('.export_load').prepend('<span class="export_loading">(加载中 ... <span class="export_current"></span> / <span class="export_total"></span> )</span>');
            $('.export_total').text(rows.length);
            rows[0].loadData(1);
        });

        function on_export_loaded(){
            $('.export_loading').remove();
            $('#export_full').attr('href', export_blob(0));
            $('#export_file').attr('href', export_blob(1));
        }
    }

})(jQuery);

}