Anime1.me 下載器

下載Anime1.me網站上的動漫

目前为 2017-12-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         Anime1.me 下載器
// @namespace    https://blog.maple3142.net/
// @version      0.3.1
// @description  下載Anime1.me網站上的動漫
// @author       maple3142
// @match        https://anime1.me/*
// @match        https://p.anime1.me/pic.php*
// @match        https://p.anime1.me/ts.php*
// @match        https://p.anime1.me/mp4.php*
// @require      https://code.jquery.com/jquery-3.2.1.min.js
// @noframes
// @grant        none
// ==/UserScript==

(function($) {
	'use strict';
	//觸發下載(firefox需要同源才能正常觸發,否則會當成普通連結打開)
	function download(url,filename){
		const a=document.createElement('a');
		a.href=url;
		if(filename)a.download=filename;
		document.body.appendChild(a);
		a.click();
		a.remove();
	}
	const loc=window.location;
	//querystring
	const query=Object.assign(...loc.search.replace('?','').split('&').map(part=>part.split('=')).map(ar=>({[ar[0]]: ar[1]})))
	if(loc.hostname==='p.anime1.me'&&loc.pathname==='/pic.php'){//如果是普通下載頁面
		//取得資料
		const video=jwplayer().getPlaylist()[0];//目前使用jwplayer
		if(!video)return;
		const sources=video.sources;
		const title=video.title;
		const videomap=Object.assign(...sources.map(src=>({[src.label]: src.file})));//Object.assign({'HD': 'hd video url'},{'SD': 'sd video url'},something...)

		//詢問要下載的畫質
		const askmsg=`輸入要下載的畫質名稱:(${sources.map(src=>src.label).join(',')})`;
		const type=prompt(askmsg);
		//如果畫質存在
		if(type in videomap){
			download(videomap[type],`${title}.mp4`);
		}
	}
	else if(loc.hostname==='p.anime1.me'&&loc.pathname==='/ts.php'){//不支援下載
		const m3u8=`https://video.anime1.top/${query.vid}/list.m3u8`
		const msg=`抱歉!由於這種影片是由多個影片所組成的,目前還無法直接下載\n不過可以複製下方的網址然後使用vlc之類支援網路串流的播放器來使用`
		prompt(msg,m3u8)
	}
	else if(loc.hostname==='p.anime1.me'&&loc.pathname==='/mp4.php'){//特殊,因為需要Referer header才能得到影片檔
		if(!confirm('這類需要特殊下載方法,要保持此頁面開啟直到下載完成\n是否繼續?'))return;
		//初始化下載器元素
		$(document.body).append(`<div id="pbox" style="position: absolute;top: 0px;left: 0px;width: 500px;height: 500px;background-color: white;"></div>`)
		//渲染函數
		function update(loaded,total){
			const percent=parseInt(loaded/total*100);
			$('#pbox').empty().append($('<div>').append(
					$('<progress>').attr('max',total).attr('value',loaded)
				).append(`${percent}%`))
				.append($('<div>').text(`${loaded/1024/1024} MB/${total/1024/1024} MB`))
			document.title=`${percent}%`
		}
		//利用ajax去抓取並轉換成blob網址然後觸發下載
		const xhr=new XMLHttpRequest();
		xhr.responseType='blob';
		xhr.onprogress=e=>{
			if(!e.lengthComputable)return;
			update(e.loaded,e.total)
		}
		xhr.onload=e=>{
			const fileurl=URL.createObjectURL(xhr.response)
			download(fileurl,`${query.vid}.mp4`)
		}
		xhr.open('GET',`https://vfr01.cdn.watch/${query.vid}.mp4`)
		xhr.send()
	}
	else{//其他頁面
		if($('.acpwd-pass').length){ //如果需要密碼就自動登入
			$('.acpwd-pass').get(0).value='anime1.me'; //目前的密碼(2017-12-03T14:15:37.823Z)
			$('.acpwd-submit').get(0).click();
			return;
		}
		//找到每個影片
		const $articles=$('article');
		for(let art of $articles){
			const $title=$(art).find('.entry-title');
			const url=$(art).find('iframe').attr('src');
			if(!url)continue;//如果沒有影片
			$title.append($('<button>').addClass('search-submit').text('下載').click(e=>window.open(url,'_blank')));
		}
	}
})(jQuery.noConflict());