// ==UserScript==
// @name Japonx 字幕&视频 下载(不要用迅雷下载,支持PotPlayer播放)
// @description 下载 japonx 的字幕和视频。
// @version 4.0.1
// @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, VideoFile, VideoDownload=false, VideoUrlMP4,
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){
console.log('CheckUrl error:', url, e);
}
});
}
}
var CheckHostBtn=$('<button>').text(localStorage['CheckHost']?'关闭':'开启').click(function(){
if(!localStorage['CheckHost']) localStorage['CheckHost']='开启';
else delete localStorage['CheckHost'];
this.textContent=localStorage['CheckHost']?'关闭':'开启';
});
$('<div>').attr({'id':'JaPonxTool'}).append('不可访问时自动刷新',CheckHostBtn).insertBefore('#bx-search');
if(localStorage['CheckHost']){
GM_xmlhttpRequest({
'url':urls,
'onload':function(e){
console.log(e);
},
'onerror':function(e){
console.log(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);
if(s.search(/\d+HD/)>-1) VideoFile=VideoID+"HD";
else if(s.search(/\d_hd/)>-1) VideoFile=VideoID.toLowerCase().replace('-','')+'_hd';
else if(s.search(/\|[a-z]{3,5}-?\d{2,4}(?:_C)?\|/i)>-1) VideoFile=s.match(/\|([a-z]{3,5}-?\d{2,4}(?:_C)?)\|/i)[1];
else if(s.search(/\|?720p\|?/)>-1&&s.search(/\|?Sub\|?/)>-1) VideoFile=VideoID+".720p.Sub";
else VideoFile=VideoID;
if(s.search(/\|?(mp4)\|?/i)>-1) {
VideoUrl=VideoUrlMP4="https://player.japronx.com/Stream/"+VideoFile+".mp4";
VideoDownload=true;
} else if(s.search(/\|?(m3u8)\|?/i)>-1) {
//VideoUrl="https://play.japronx.com/"+VideoFile+"/"+VideoFile+".m3u8";
//VideoUrl="https://p.japronx.com/output/"+M3U8_Hash+"/index.m3u8?md5="+M3U8_MD5+"&expires="+M3U8_Expires;
VideoUrlMP4="https://player.japronx.com/Stream/"+VideoFile+".mp4";
} else {
VideoUrlMP4="https://player.japronx.com/Stream/"+VideoFile+".mp4";
}
//console.log('VideoUrl: ',VideoUrl);
//console.log('VideoFile:', VideoFile);
//适用网址:310、3786、3789、3791
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>');
switch(M3U8_Hash.length) {
case 2:
$.each(M3U8_Hash, function(i,Hash){
CheckVtt(Hash);
});
break;
case 1:
CheckVtt(M3U8_Hash.toString());
case 0:
//VideoDownMP4();
VideoDown(VideoUrlMP4);
}
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 VideoDownMP4(){
console.log('尝试获取 MP4 文件', VideoUrlMP4);
//console.log('VideoUrlMP4:',);
JaPronX.CheckUrl(VideoUrlMP4, function(e){
console.log('Check VideoUrlMP4:', e.status, VideoUrlMP4);
switch(e.status){
case 200:
console.log('200: ',e.status, VideoUrlMP4);
$('.d-btn.download').attr({'href':VideoUrlMP4, 'target':'blank', 'download':VideoID+'.mp4'}).text('下载').css({'color':'black','width':'80px'});
break;
case 404:
$('.d-btn.download').attr({'href':VideoUrl, 'target':'blank', 'download':VideoID+'.M3U8'}).text('M3U8').css({'color':'black'});
break;
default:
console.log(e.status, ' 错误,正在重试')
$('.d-btn.download').text('正在重试');
setTimeout(VideoDownMP4, 2000);
}
});
}
function VideoDown(VideoUrl){
JaPronX.CheckUrl(VideoUrl, function(e){//M3U8
//console.log('load: ', VideoUrl, e);
switch(e.status){
case 200:
if(VideoDownload) $('.d-btn.download').attr({'href':VideoUrl, 'target':'blank', 'download':VideoID+'.mp4'}).css({'color':'black'});
else {
VideoDownMP4();
}
$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');
VideoDownMP4();
break;
default:
$('.d-btn.download').text('获取失败').css({'width':'120px'}).attr({'href':'javascript:location.reload();'});
break;
}
});
}
},
'error' : function(e){
console.log('$ error:',e);
}
});
}
ajaxGetID();
})();