Japonx 字幕&视频 下载(支持PotPlayer播放)

下载 japonx 的字幕和视频。

Fra og med 20.11.2018. Se den nyeste version.

// ==UserScript==
// @name        Japonx 字幕&视频 下载(支持PotPlayer播放)
// @description 下载 japonx 的字幕和视频。
// @version     4.0.5
// @author      ThisAV
// @homepage    https://greasyfork.org/zh-CN/users/122964
// @namespace   https://greasyfork.org/users/122964
// @feedback    https://greasyfork.org/en/scripts/372676/feedback
// @connect     japronx.com
// @connect     japonx.net
// @connect     japonx.vip
// @include     https://japonx.net/portal/index/detail/id/*
// @include     https://japonx.vip/portal/index/detail/id/*
// @include     https://www.japonx.net/portal/index/detail/id/*
// @include     https://www.japonx.vip/portal/index/detail/id/*
// @run-at      document-idle
// @require     http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
// @require     https://greasyfork.org/scripts/373130/code/ProtocolCheck.js?version=635816
// @icon        https://www.japonx.vip/favicon.ico
// @grant       GM_addStyle
// @grant       unsafeWindow
// @grant       GM_xmlhttpRequest
// @grant       GM_openInTab
// ==/UserScript==

(function(){
    'use strict';
    var urls=location.href,
        host=location.hostname,
        PageId=location.href.replace(/.+\/id\/(\d+).html/i,'$1'),
        VideoID=$('dt:contains(番號)').next().text(), //番号
        VideoUrl,
        VideoDownload=false,
        VideoSubUrl,
        webTitle=document.title='【'+VideoID+'】'+document.title;

    var JaPronX={
        CheckUrl : function(url, callback){
            GM_xmlhttpRequest({
              url:url,
                /*
              headers: {
                'Host':'japronx.com',
                'Content-Type': 'application/x-www-form-urlencoded'
              },*/
                method:'head',
                synchronous:true,
                ontimeout:10000,
                onload:callback,
                onerror:function(e){
                    if(url.search('.mp4')>-1&&e.status==0) {
                        $('.d-btn.download').attr({'href':url,'target':'blank','download':VideoID+'.mp4'}).text('服务器无响应,请求失败').css({'width':'250px','color':'red'});
                    }
                    console.log('CheckUrl error:', e.status, url, e);
                }
            });
        }
    }

    GM_addStyle('#JaPonxTool{position:absolute;right:280px;top:20px;} .btnC{border:1px solid #d7dadb;padding:8px;border-radius:6px;display:inline-block;color:#000;background:#fff;margin:15px 0 0 15px;}');

    var $btnDiv = $('#d-btn');
    if(0) $btnDiv.append('<a class="d-btn sub" target="_blank" href="https://'+host+'/portal/index/ajax_get_js.html?id='+PageId+'">Ajax</a>');
    $('<a>').attr({'class':'btnC','href':"https://www.japonx.vip/portal/index/detail/id/"+(Number(PageId)+1)+".html"}).text('下一页').insertAfter($('#logo-maker, h2:contains(熱門推薦)').css({'display':'inline'}));


    var ajaxGetID=function(){
        GM_xmlhttpRequest({
            'url':'/portal/index/ajax_get_js.html?id='+PageId,
            'method':'get',
            //'data' : 'id='+PageId,
            'responseType':'text',
            'async':false,
            'onload' : function(e){
                var s=e.responseText;
                GM_addStyle("dl.desc {width:400px;} #contents-inline dl a:hover{border:1px solid #00aaff;margin:1px!important;} .play{margin-right:0px!important;} .PotPlayer, .sub{width:130px;}");
                //console.log('GM_xmlhttpRequest: ',e, s);

                //console.log('VideoUrl: ',VideoUrl);

                var Arry=s.split(/\||'\.split\('/i), M3U8_Hash=[], M3U8_MD5, M3U8_Expires, VttData/*字幕上传日期*/, VttHash/*字幕文件 Hash*/, $zmBtn;
                $.each(Arry,function(index,val){
                    //console.log(index, val.length, val);
                    switch(val.length) {
                            //M3U8 32位 Hash 提取
                        case 32: M3U8_Hash.push(val);break;
                        case 22: M3U8_MD5=val;break;
                        case 10: if(/\d{10}/.test(val)) M3U8_Expires=val;break;
                        case 8: if(/2\d{7}/.test(val)) VttData=val;break;
                    }
                });

                //console.log(M3U8_Hash, M3U8_Hash.length, M3U8_MD5, M3U8_Expires, VttData);
                if(!M3U8_MD5) {
                    console.log('获取 M3U8_MD5 失败');
                    $('.d-btn.download').text('获取 M3U8_MD5 失败').css({'width':'230px'});
                    setTimeout(ajaxGetID, 2000);
                    return false;
                }

                //字幕
                if(s.search(/\|vtt\|/im)===-1) $zmBtn = $('<a class="d-btn sub" style="color:#ccc;" href="javascript:void(0);">无字幕文件</a>');
                console.log('M3U8_Hash.length',M3U8_Hash.length);
                switch(M3U8_Hash.length) {
                    case 2:
                        $.each(M3U8_Hash, function(i,Hash){
                            CheckVtt(Hash);
                        });
                        break;
                    case 1:
                        CheckVtt(M3U8_Hash.toString());
                        break;
                    case 0:
                        $('.d-btn.download').text('未找到资源').css({'width':'150px'});
                }

                function CheckVtt(Hash){
                    JaPronX.CheckUrl('https://'+host+'/upload/admin/'+VttData+'/'+Hash+'.vtt',function(e){
                        //console.log(i, Hash, e);
                        switch(e.status) {
                            case 200:
                                VideoSubUrl='https://'+host+'/upload/admin/'+VttData+'/'+Hash+'.vtt';
                                $zmBtn = $('<a class="d-btn sub" target="_blank">下载字幕</a>').attr({'download':VideoID+'.vtt', 'href':VideoSubUrl});
                                $btnDiv.append($zmBtn);
                                break;
                            case 404:
                                JaPronX.CheckUrl("https://p.japronx.com/output/"+Hash+"/index.m3u8?md5="+M3U8_MD5+"&expires="+M3U8_Expires,function(e){
                                    //console.log('Video:', i, Hash, e);
                                    if(e.status===200) {
                                        VideoUrl="https://p.japronx.com/output/"+Hash+"/index.m3u8?md5="+M3U8_MD5+"&expires="+M3U8_Expires;
                                        VideoDown(VideoUrl);
                                    }
                                });
                                break;
                            default:
                                console.log(e.status);
                        }
                    });
                }

                function VideoDown(VideoUrl){
                    JaPronX.CheckUrl(VideoUrl, function(e){//M3U8
                        //console.log('load: ', VideoUrl, e);
                        switch(e.status){
                            case 200:
                                 $('.d-btn.download').attr({'href':VideoUrl, 'target':'blank'}).text('M3U8').css({'color':'black','width':'100px'});
                                $btnDiv.append($('<a class="d-btn PotPlayer">').attr({'href':'potplayer://'+VideoUrl}).css({'color':'black'}).text('Potplayer').click(function(){
                                    window.protocolCheck(this.href, function(){
                                        if(confirm("协议未注册,现在去注册程序协议?")) GM_openInTab("https://greasyfork.org/zh-CN/scripts/372676/#potplayer");
                                    });
                                }));
                                break;
                            case 404:
                                $('.d-btn.download').text('404');
                                break;
                            default:
                                $('.d-btn.download').text('获取失败').css({'width':'120px'}).attr({'href':'javascript:location.reload();'});
                                break;
                        }
                    });
                }
            },
            'error' : function(e){
                console.log('$ error:',e);
            }
        });
    }
    ajaxGetID();
})();