漫畫閱讀器

自動連續載入接續下一頁

2018-03-09 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

// ==UserScript==
// @name           漫畫閱讀器
// @version        1.2.1
// @namespace      486FF937-D190-42D3-B107-6ED190443E1C
// @description    自動連續載入接續下一頁
// @match          http://comic.sfacg.com/HTML/*/*/
// @match          http://comic.sfacg.com/HTML/*/*/?#p=*
// @match          http://v.comicbus.com/online/*.html?ch=*
// @match          http://*.manhuagui.com/comic/*/*.html*
// @run-at         document-end
// @grant          none
// @require        https://code.jquery.com/jquery-2.2.3.min.js
// ==/UserScript==

// 圖片大小轉換的click處理用函式
function ch_size(){
	var max_height = window.innerHeight-20;
	// 算出要將圖片給垂直置中的top值
	var pic_top = $(this).offset().top-10;
	// 算出當卷軸位置和發生click事件的圖片top之間的間距
	var point = $(document).scrollTop()-$(this).offset().top;
	var ori_width = $(this).attr('originalWidth');
	var move_scroll;

	if(point < 0) point = 0;

	// 當寬等於原始設定寬長,而且高度小於
	if(this.width == ori_width && this.height <= max_height){
		// move_scroll = $(this).offset().top-(screen.height-this.height)/2;
		move_scroll = $(this).offset().top-(window.innerHeight-this.height)/2;
	}else if(this.width == ori_width){
		move_scroll = pic_top;
		$(this).removeAttr('width');
		$(this).attr('height', max_height);
	}else{
		move_scroll = point*(ori_width-this.width)/this.width + $(document).scrollTop();
		$(this).removeAttr('height');
		$(this).attr('width', ori_width);
	}

	$(document).scrollTop(move_scroll);
}

// 調整圖片大小load處理函式
function fit_img(){
	if(this.naturalWidth > this.naturalHeight){
		if(this.naturalWidth > max_horiz_width){
			$(this).attr('width', max_horiz_width);
		}else if(this.naturalWidth < min_horiz_width){
			$(this).attr('width', min_horiz_width);
		}
	}else{
		if(this.naturalWidth < min_verti_width){
			$(this).attr('width', min_verti_width);
		}else if(this.naturalWidth > max_verti_width){
			$(this).attr('width', max_verti_width);
		}
	}

	$(this).removeAttr('style');
	$(this).attr('originalWidth', this.width);
	$(this).click(ch_size);
}

// 預先load圖片 並且調整大小
function pre_load(){
	for(var i=index, j=0; j<pre_pages && i<picCount; i++, j++){
		$(pic[i]).css({
			'border': '3px dashed #FF0000',
			'max-width': max_horiz_width + 'px',
			'min-width': '800px'
		});
		
		$(pic[i]).load(fit_img);
		pic[i].src = picAy[i];
	}
}

// 將load好的圖片給顯示出來
function show_img(){
	for(var i=index, j=0; j<pre_pages && i<picCount; i++, j++){
		$pic_area.append(pic[i]);
		$pic_area.append('<div style="height: 22px; background: #E6E6E6; margin: 30px 0px;"><a style="font: bold 18px Arial;" href="' + bookmark() + '">第 ' + (++index) + ' 頁</a></div>');
	}
	
	if(index==picCount){
		$(document).unbind('scroll');
	}
}

function sfacg(){
	picCount = unsafeWindow.picCount;
	picAy = unsafeWindow.picAy;
	pic = new Array(picCount);
	$pic_area = $('#curPic').parent().parent();
	index = unsafeWindow.curIndex-1;
	var host = unsafeWindow.hosts[0];
	bookmark = function(){
		return '?#p=' + (index+1);
	}

	// 初始化圖片陣列 圖片位址陣列
	for(var i=0; i<picCount; i++){
		pic[i] = new Image();
		picAy[i] = host + picAy[i];
	}
	
	// 將原本的連結圖片給移除
	$('#curPic').parent().remove();
	$('DIV.Reduction_top').hide();
	$('TD[valign="top"][bgcolor="#FFFFFF"]:last-child').hide();
	
	// 置中顯示
	$pic_area.attr('align', 'center');
}

function comicvip(){
	// 獲得網站本身的變數和方法
	var su = unsafeWindow.su;
	var ti = unsafeWindow.ti;
	var nn = unsafeWindow.nn;
	var mm = unsafeWindow.mm;
	
	for(let i of document.scripts[13].innerHTML.match(/var +?\w+? *?= *?lc\(/g)){
		eval(i.slice(0, -3) + 'unsafeWindow.' + i.match(/var +?(\w+?) *?= *?lc\(/)[1]);
	}
	
	picCount = unsafeWindow.ps;
	picAy = new Array(picCount);
	pic = new Array(picCount);
	$pic_area = $('#TheImg').parent();
	index = unsafeWindow.p-1;
	
	bookmark = function(){
		return '?ch=' + unsafeWindow.ch + '-' + (index+1);
	}
	
	// 初始化圖片陣列 圖片位址陣列
	let imgurl = document.scripts[13].innerHTML.match(/ge\('TheImg'\)\.src *?= *?(.+?;)/)[1];
	for(var i=0; i<picCount; i++){
		pic[i] = new Image();
		let p = i + 1;
		picAy[i] = eval(imgurl);
	}

	// 將原本的連結圖片給移除
	$('#TheImg, #div_li1').remove();
}

function ikanman(){
	picCount = unsafeWindow.cInfo.files.length;
	picAy = new Array(picCount);
	pic = new Array(picCount);
	$pic_area = $("table.pr").parent();
	index = unsafeWindow.pVars.page-1;
	bookmark = function(){
		return "/comic/" + cInfo.bid + "/" + cInfo.cid + ((index+1) == 1 ? "" : "_p" + (index+1)) + ".html";
	}
	
	for(var i=0; i<picCount; i++){
		pic[i] = new Image();
		picAy[i] = SMH.utils.getPicUrl(i);
	}
	
	unsafeWindow.SMH.utils.goPage = function(n){
		location.href = "/comic/" + cInfo.bid + "/" + cInfo.cid + (n == 1 ? "" : "_p" + n) + ".html";
	};
	
	// 將原本的連結圖片給移除
	$pic_area.html("");
	$pic_area.attr("align", "center")
	$("#servList, .support, #share, .backToTop").remove()
}


var picCount;
var picAy;
var pic;
var $pic_area;
var index;
var bookmark;

// scroll的觸發事件
$(document).scroll(function(){
	var now_scroll = $(document).height()-screen.height-$(document).scrollTop();
	
	if(now_scroll < overange){
		show_img();
		pre_load();
	}
});

switch(location.hostname){
	case 'comic.sfacg.com':
		sfacg();
		break;
	case 'v.comicbus.com':
		comicvip();
		break;
	case "www.manhuagui.com":
	case "tw.manhuagui.com":
		ikanman();
		break;
	default:
		alert("有新的域名");
}

// 強制第一頁產生scrollbar 避免max_horiz_width算錯
$('html').css('overflow-y', 'scroll');
const min_horiz_width = 1200;
const max_horiz_width = $(window).width()-20;
const min_verti_width = 500;
const max_verti_width = screen.width*0.5;
const pre_pages = 5;
const overange = screen.height * 3;

pre_load();
show_img();
pre_load();