xchina

good

ของเมื่อวันที่ 28-09-2024 ดู เวอร์ชันล่าสุด

// ==UserScript==
// @name        xchina
// @version      2024.09.28.05
// @author       You
// @description  good
// @include     https://xchina.co/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @connect       *
// @require     https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js
// @require		https://code.jquery.com/jquery-3.6.0.min.js
// @require     https://update.greasyfork.org/scripts/480132/1349340/Get_all_img_Library.js
// @namespace https://greasyfork.org/users/1210231
// ==/UserScript==
$(function(){
	add_bu()
		.click(()=>{sessionStorage.setItem('startAjax','true');location.reload(true)})  //先清除缓存,再ajax
 
	if(sessionStorage.getItem('startAjax')=='true'){get_list(get_big_img);sessionStorage.setItem('startAjax','false')}  //判断是否ajax
})
function add_bu(){
	let bu = $('<button>Ajax10</button>')
			.css({
				width:'100%',
				'font-size':'10vw'
			})
	$('.photos').before(bu)
	return bu
}
function get_list(callback){
	if(window.location.href.match(/xchina\.co\/photo\/id-\w+\.html/g)){}else{sessionStorage.removeItem('page');return}
 
	let key = "xchina"
	let atag = $('a.next[href]:first')
	let nums = (()=>{
					if(atag.length>0&&atag.prev().length>0){
						return Number(atag.prev().text())
					}else{
						return 1
					}
				})()
	atag = $('a[current="true"]:first')
	if(atag.length==0){callback($('.photos').children());return}
	$('.photos').children().remove()
	//page记录当前ajax到了第几页
	let page = sessionStorage.getItem('page')
	if(!page){page=0}else{page = Number(page)}
	if(page>=nums-1){page=0}
 
	let geturl = function(i){
		            if(i>10){sessionStorage.setItem('page',i+page);Put_img_to_ClickShowBox();return false}
					if(i + page + 1>=nums){sessionStorage.setItem('page',0);Put_img_to_ClickShowBox();return false} //超出总页数或10页就退出
					return atag[0].href.replace(/\d+(?=\.)/,(i+1+page))
				}
	let getimg = function(data){return $(data).find('.photos').children().attr('ccc','yes')}
	let putimg = function(img,i){
		$('.photos').append(img)
		setTimeout(function(){$('.mass_top').css('font-size','10vw').text(`${page + i + 1}/${nums}`)},1000)
 
		$('.photos').children('*:not([ccc])').remove()
		del_ad();
		callback(img)
	}
	window.GAIL.get_img_obo_ajax_href(key,nums,geturl,getimg,putimg)
}
function Put_img_to_ClickShowBox(){
	let imgs = $('.photos img[big_src][src*="img.xchina"]')
	if(window.GAIL.Add_ClickShowBox){
		window.GAIL.Add_ClickShowBox(imgs,5);
	}
	console.log('Put_img_to_ClickShowBox is run');
}
function get_big_img(img){
	let iimg = img.find('img')
	iimg.each(function(){
        let parent = $(this).parent().before($(this).attr('big','yes'))
        parent.hide()
		$(this).attr('big_src',this.src.replace(/_[^\.]+/g,"").replace(/\w+$/g,"jpg"))
		//this.src = this.src.replace(/_[^\.]+/g,"").replace(/\w+$/g,"jpg")
		$('body').append($('<p></p>').text(this.src))
		$(this).parents('a:first[href]').removeAttr('href')
		$(this).click(function(){
			let name = document.title + new Date().getTime() + ".jpg"
			let src = $(this).attr('big_src')
			let _this = this
			GM_download({
				url:src,
				name:name,
				onload:()=>{$(_this).remove()}
			})
		})
	})
 
	$(".push-bottom").remove()
	return
 
}
function checkimg_unloaded(){
	let checking = function(){
		let img = $('img[big]:visible').filter(function(){return this.naturalWidth == 0})
		if(img.length>0){
			img.each(function(){
				var clone = $(this).clone(true)
				$(this).after(clone)
				$(this).remove()
				clone[0].scrollIntoView()
			})
		}else{}
	}
	let check = setInterval(checking,500)
}
function del_ad(){
	$('*:not(.download_bu,.mass_top,.clickShowBox,.clickShowBox *,.clickShowBox_ShowBu)').filter(function(){return $(this).css('position')=="fixed"}).remove()
}
$('body').on('touchend',del_ad);
 
 
 
        function Add_ClickShowBox(imgs,num){
			showNum = num;
			let box = $('.clickShowBox');
			if(box.length!=0){return}
			box = `
			<div class="clickShowBox">
				<p class="pages">1/10</p>
				<button class="close">x</button>
				<div class="downloadBU">
					<button class="download">↓</button>
					<button class="downloadall">↓↓</button>
				</div>
			</div>
			<div class="clickShowBox_ShowBu"></div>
			`
			box = $(box);
			$('body').prepend(box);
			$('.clickShowBox').hide();
 
			imgs.each(function(){
				const item = $('<div class="item"></div>')
				    .append($('<img>').attr({'small_src':this.src,src:"",'big_src':$(this).attr('big_src')}));
				$('.clickShowBox').append(item);
			})
			Add_ClickShowBox_css();
 
			ClickShowNext({img:$('img'),onlyDown:false});
			$('.clickShowBox .close').click(function(){
				$('.clickShowBox').fadeOut();
				$('.clickShowBox_ShowBu').show()
			})
			$('.clickShowBox_ShowBu').click(function(){
				$('.clickShowBox').fadeIn();
				$(this).hide();
				Show_imgs(num);
			})
			$('.clickShowBox .download').click(function(){
				BU_nomal($(this))
				const img = $('.clickShowBox .item img').eq(nowIndex);
				let src = img[0].src;
				if(img.attr('big_src')){
					src = img.attr('big_src');
					img[0].src = src;
				}
				let name = document.title + new Date().getTime() + src.match(/\.jpg|\.jpeg|\.webp|\.png/g)[0];
				if(img.attr('name')){
					name = img.attr('name');
				}
				BU_busy($(this))
				try{
					GM_download({
						url:src,
						name:name,
						onload:function(){
							BU_done($('.download'));
						},
						error:function(){
							BU_error($('.download'));
						}
					})
				}catch(error){
					console.log(error);
					BU_error($('.download'));
				}
			})
			donwnloader.AllComplete = ()=>{
				BU_done($('.clickShowBox .downloadall'));
			}
			donwnloader.OneSuccess = (src)=>{
				$('.clickShowBox .item img').filter(function(){return $(this).attr('big_src') == src})
				                            .attr('src',src);
			}
			$('.clickShowBox .downloadall').click(function(){
				BU_busy($(this));
				try{
					donwnloader.Download_img($('.clickShowBox .item img').clone());
				}catch(error){
					console.log(error);
					BU_error($(this));
				}
			})
		}
 
		function Add_ClickShowBox_css(){
			let css = `
			.clickShowBox{
				width: 100%;
				height: 100vh;
				background-color: #2d2d2d;
				overflow: hidden;
				border-radius: 0vw;
				position: fixed;
				z-index: 9999;
			}
			.clickShowBox .item{
				width: 100%;
				height: 100%;
				background-color: #2D2D2D;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			.clickShowBox .item img{
				width:100%;
				height:auto;
			}
			.clickShowBox .pages{
				font-size: 5vw;
				color: rgba(255,255,255,0.5);
				position: fixed;
				top: 1.5vw;
				margin: 2vw;
			    right:12vw
			}
			.clickShowBox .close{
				width: 10vw;
				height:10vw;
				font-size: 6vw;
				border-radius: 10vw;
				background-color: rgba(255,255,255,0.1);
				color: rgba(255,255,255,0.1);
				position: fixed;
				right: 0;
				top:0;
				margin: 2vw;
				font-weight: bold;
				border: none;
			}
			.clickShowBox .close:active{
				filter:invert(100%);
			}
			.clickShowBox .downloadBU{
				display: flex;
				flex-direction: row;
				position: fixed;
				bottom:0;
			}
			.clickShowBox .download
			,.clickShowBox .downloadall{
				width: 10vw;
				height:10vw;
				font-size: 5vw;
				border-radius: 2vw;
				background-color: #ff8a17;
				color: white;
				margin: 0 0 2vw 2vw;
				border: none;
				opacity: .4;
				position: relative;
			}
			.clickShowBox .download:active
			,.clickShowBox .downloadall:active{
				opacity: .6;
			}
			.clickShowBox .busy{
				animation: BU_busy infinite 1s linear;
			}
			@keyframes BU_busy{
				0%{top:0}
				25%{top:2vw}
				75%{top:-2vw}
				100%{top:0}
			}
			.clickShowBox .error{
				background-color: red;
			}
			.clickShowBox_ShowBu{
				width: 10vw;
				height: 10vw;
				border-radius: 10vw;
				background-color: orange;
				position: fixed;
				bottom: 30%;
				right: -5vw;
				z-index: 999999;
				display: flex;
				align-items: center;
				justify-content: center;
 
			}
			.clickShowBox_ShowBu::after{
				content: "";
				width: 70%;
				height: 70%;
				background-image: url('data:image/svg+xml;utf8,<svg width="10" height="10" xmlns="http://www.w3.org/2000/svg"><path d="M 10 10 L 0 5 L 10 0 Z" fill="White"/></svg>');
				background-size: cover;
				background-repeat: no-repeat;
				transform: scaleX(0.8);
			}
			`
			if(window.GAIL && window.GAIL.Add_css){
				window.GAIL.Add_css(css);
			}else{
				console.log('window.GAIL.Add_css is none');
			}
		}
		let showNum = 10;
		function BU_busy(bu){
			bu.addClass('busy');
		}
		function BU_done(bu){
			bu.removeClass('busy');
		}
		function BU_error(bu){
			bu.removeClass('busy');
			bu.addClass('error');
		}
		function BU_nomal(bu){
			bu.removeClass('busy').removeClass('error');
		}
		let nowIndex = 0;
		function ClickShowNext({img,onlyDown}){
			if(!$){return;}
			if(img.length<=1){console.log('only one img');return;}
			let item = $('.clickShowBox .item');
			$('.clickShowBox .pages').text(1+"/"+item.length);
			item.click(function(event){
				let y = event.clientY;
				let index = item.index($(this));
				index = !onlyDown && y<$(this).height()/2 ? index-1:index+1;
				index = index>=0?Math.min(index,item.length-1):0
				item.eq(index)[0].scrollIntoView();
				$('.clickShowBox .pages').text((index+1)+"/"+item.length);
				nowIndex = index;
				Show_imgs(showNum);
			});
		}
		function Show_imgs(i){
			let img = $('.clickShowBox .item img[small_src][src=""]');
			let start = Math.max(0,nowIndex-i);
			let end = Math.min(img.length,nowIndex+i);
			img.slice(start,end).each(function(){
				this.src = $(this).attr('small_src');
			});
			console.log(`${start} ${end} ${img.length}`)
		}
		function Add_css(cssString){
			var style = document.createElement('style');
			style.type = 'text/css';
			style.innerHTML = cssString;
			document.body.appendChild(style);
		}
		class Downloader{
			constructor(){
				this.downloadType = "";
				this.downloaded = [];
				this.downloading = 0;
				this.downloadError = [];
				this.imgs = "";
				this.maxDownloadingCounts = 10;
				this.timeout = 2000;
				this.AllComplete = null;
				this.OneSuccess = null;
				this.OneError = null;
				this.needCheckSrc = true;
			}
 
			async Download_img(imgs){
				let self = this;
				if(this.downloadType==""){
					await this.Test_downloadType(imgs.eq(0));
				}
 
				this.Set_download(imgs)
				if(this.downloadType=="GM_download"){
					this.Donwload_img_by_GM();
				}else{
					this.Donwload_img_by_atag();
				}
			}
 
			async Test_downloadType(img){
				return new Promise((resolve)=>{
					let timeout = 3000;
					let isOk = false;
					GM_download({
						url:img[0].src,
						name:"test.png",
						onload:()=>{isOk = true;this.downloadType = "GM_download";resolve()},
					})
					setTimeout(()=>{
						if(!isOk){this.downloadType = "atag";}
						resolve()
					},timeout)
				})
			}
			Set_download(imgs){
				this.downloaded = [];
				this.downloading = 0;
				this.downloadError = [];
				this.imgs = imgs;
			}
 
			Donwload_img_by_GM(){
				let self = this;
				async function Download_one(i){
					if(i>=self.imgs.length){if(self.AllComplete){self.AllComplete()};return;}
					if(self.downloading>=self.maxDownloadingCounts){setTimeout(()=>{Download_one(i)},1000);return;}
 
					let name = '';
					let src = '';
					self.downloading++;
					try{
						await self.Check_and_get_nameAndsrc(i)
								.then((m)=>{
									name = m.name;
									src = m.src;
								});
					}catch(error){
		                console.log(error)
						console.log('imgsrc is error:');
						self.downloadError.push(i);
						self.downloading--;
						Download_one(i+1);
						return;
					}
					console.log(name)
		            console.log(document.title)
					let timeout = false;
					setTimeout(() => {timeout = true;},10000);
					GM_download({
						url:src,
						name:name,
						onload:function(){
							self.downloaded.push(i);
							self.downloading--;
							if(window.GAIL.showmass){
								window.GAIL.showmass((self.downloaded.length+self.downloadError.length)+"/"+self.imgs.length);
							}
							self.imgs.eq(i).remove();
 
							if(self.OneSuccess){self.OneSuccess(src);}
						},
						onerror:function(){
							self.downloading--;
							self.downloadError.push(i);
							if(self.OneError){self.OneError(src);}
						},
						onprogress:function(){
							if(timeout){return false;}
						}
					});
					Download_one(i+1);
				}
				Download_one(0);
			}
			Check_and_get_nameAndsrc(i){
				let self = this;
				return new Promise(async (resolve,reject)=>{
					let src = self.imgs.eq(i).attr('big_src');
					if(!src){src = self.imgs.eq(i)[0].src;}
					if(!src){reject();}
					console.log("check:"+src)
					try{
						await self.check_src_is_right(src);
					}catch(error){
						reject();
					}
					let ext = src.match(/\.jpg|\.png|\.webp|\.gif|\.bmp/g);
					if(!ext){ext = '.png';}else{ext = ext[0];}
					let name = document.title + new Date().getTime() + ext;
					resolve({name:name,src:src});
				});
			}
			Donwload_img_by_atag(){
				let self = this;
				let myWindow = window.open('_blank');
				async function Download_one(i){
					if(i>=imgs.length){myWindow.alert('end');myWindow.history.go(0-myWindow.history.length+1);myWindow.close();return;}
 
					let name = '';
					let src = '';
					self.downloading++;
					try{
						await self.Check_and_get_nameAndsrc(i)
								.then((m)=>{
									name = m.name;
									src = m.src;
								});
					}catch(error){
						console.log('imgsrc is error:');
						self.downloadError.push(i);
						self.downloading--;
						Download_one(i+1);
						return;
					}
 
					if(!myWindow){alert('windows is closed');return;}
					myWindow.location.href = src;
					let a = $('<a></a>').attr({
						'href':$(myWindow.document.body).find('img:first').attr('src'),
						'download':name,
					})
					a[0].click();
					setTimeout(function() {Download_one(i+1);self.imgs.eq(i).remove()}, 1000);
					self.downloaded.push(i);
					self.downloading--;
				}
				Download_one(0);
			}
			check_src_is_right(src){
				let self = this;
				return new Promise((resolve,reject)=>{
					if(!self.needCheckSrc){resolve();}
					let iimg = new Image();
					iimg.onload = function(){
						if(this.width*this.height*this.naturalWidth*this.naturalHeight==0){reject();}else{resolve();}
					}
					iimg.onerror = function(){reject();}
					iimg.src = src;
					setTimeout(function() {iimg.abort();reject();}, 2000);
				})
			}
		}
		let donwnloader = new Downloader();
		donwnloader.needCheckSrc = false;
		if(window.GAIL){
			window.GAIL.Add_css = Add_css;
			window.GAIL.Add_ClickShowBox = Add_ClickShowBox;
		}else{
			window.GAIL = {
			    Add_css:Add_css,
				Add_ClickShowBox:Add_ClickShowBox,
			}
		}