Baidu Search AutoPager

百度搜索自动翻页,网站预览图。

目前為 2015-09-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Baidu Search AutoPager
// @author      Crab
// @namespace   [email protected]
// @description 百度搜索自动翻页,网站预览图。
// @include     /^https?:\/\/www\.baidu\.com\/(?:(?:baidu|s)\?.*|[#?&].*)?$/
// @version     0.2
// @grant       none
// ==/UserScript==
var loadingBar = null,
	scrollTimeout = null,
	content_left = document.getElementById('content_left'),
	resultsLength = document.querySelectorAll('#content_left>div.c-container').length,
	queryString = (location.search.match(/wd=[^&]+/) || [''])[0],
	pageNum = Math.floor(((location.search.match(/&pn=(\d+)/) || [0, 0])[1] - 0) / resultsLength), //初始页码
	imagesList = [],
	isLastPage = false;

var cE = function(name, attr, parent){
	var e = document.createElement(name);
	for (var i in attr || []) 
		i == 'text' ? (e.textContent = attr[i]) : e.setAttribute(i, attr[i]);
	parent && (Array.isArray(parent) ? 
		parent[0].insertBefore(e, parent.length == 2 ? parent[1] : parent[0].firstChild) : 
		parent.appendChild(e));
	return e;
}, addPreviews = function(list){
	[].forEach.call(list, function(div){
		var cite = div.querySelector('cite, span.g, span.c-showurl');
		if(!cite) return;
		var match = cite.textContent.match(/[^\/]+/);
		match && match[0].length > 1 && imagesList.push(cE('img', {align: 'left', 'pic-src': 'https://'+ match[0].charAt(0) + '.searchpreview.de/preview?s='+ match[0] +'&ra=0'}, [div]));
	});
}, toggleLoadingBar = function(show){
	(loadingBar || (loadingBar = cE('div', {id:'loadingBar'}, [document.getElementById('foot')])))
		.classList[show ? 'add' : 'remove']('loading');
}, getNextPageData = function(url, callback){
	var req = new XMLHttpRequest();
	req.open('GET', url, true);
	req.send();
	req.onload = function(){
		callback((new DOMParser()).parseFromString(req.responseText, 'text/html'));
	};
}, nextPage = function(){
	toggleLoadingBar(true);
	var url = 's?tn=monline_dg&pn='+ ((pageNum + 1) * resultsLength) + '&' + queryString;
	getNextPageData(url, function(doc){
		var df = document.createDocumentFragment(),
			last = content_left.children[content_left.children.length - 1];
		[].forEach.call(doc.querySelectorAll('#content_left>div.c-container'), df.appendChild.bind(df));
		//添加预览图
		addPreviews(df.children);
		//往最后结果后面插入
		last.parentNode.insertBefore(df, last.nextElementSibling);
		//替换新页码
		var pageNumMenu = doc.getElementById('page'),
			_pageNumMenu = document.getElementById('page');
		_pageNumMenu.parentNode.replaceChild(pageNumMenu, _pageNumMenu);
		//更新地址
		history.pushState({}, doc.title, url);
		toggleLoadingBar();
		onScroll();
		if(pageNumMenu.lastElementChild.localName === 'strong')
			return (isLastPage = true) && !imagesList.length && removeEventListener('scroll', onScroll); //图片都加载完成 最后一页
		else
			isLastPage = false;
		pageNum++;
	});
}, onResize = function(){
	var content_right = document.getElementById('content_right'), //右侧栏高度
		div_ch = 0; //第一页搜索项累计高
	onScroll();
	if(!content_right) return;
	[].forEach.call(content_left.children, function(div){
		div.style.setProperty('width', (div_ch < content_right.clientHeight) ? 'calc(98% - '+ content_right.clientWidth +'px)' : '100%', 'important');
		div_ch += div.clientHeight;
	});
}, onScroll = function(){
	clearTimeout(scrollTimeout);
	scrollTimeout = setTimeout(function(){
		var de = document.documentElement;
		if(!de.scrollTop) de = document.body;//兼容chrome
		!isLastPage && de.scrollTop + window.innerHeight > de.scrollHeight - 5 && nextPage();
		//lazyload
		var loadedList = [];
		imagesList.forEach(function(img){
			var r = img.getBoundingClientRect();
			if(r.bottom >= 0 && window.innerHeight >= r.top){
				loadedList.push(img);
				img.parentNode.classList.add('loading');
				img.onload = img.onerror = function(){
					img.parentNode.classList.remove('loading');
					img.onload = img.onerror = function(){};
				}
				img.src = img.getAttribute('pic-src');
				img.removeAttribute('pic-src');
			}
		});
		loadedList.forEach(function(img){
			imagesList.splice(imagesList.indexOf(img), 1);
		});
	}, 300);
};

var cssText = (function(){/*
	// 序号
	body {
		counter-reset: resultNum;
		min-width: 600px!important;
		--loading-spinner: url('data:image/gif;base64,R0lGODlhIAAgAKIEALa2toGBgb6+vgwMDObm5tra2jQ0NAAAACH/C05FVFNDQVBFMi4wAwEAAAAh/h1CdWlsdCB3aXRoIEdJRiBNb3ZpZSBHZWFyIDQuMAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAHACwAAAAAIAAgAAADz3i6zDGhyUnXuK2QKkFk18AIAMAxxGWAmEKUwskOphIuJFDIyzOstlYBxmMYLp/boQTYFBWA0OY2BMQWVefSYPgojhDJa0HI6Xpcbk0Q5pitjXR6J2NatQyAHHiq0isBakV/JwReT4hFdjBlAY6Ph3WLVpCQiJMCjZWRiZ0UhH2DJaASfjxmmRNlTFcyb60Kr301B6ujWKxaTaVEWCRaL2c4t0xQJXg8wTXFB1Wwp7dLJcPCPM4LzLXZrsfY09RPvca0ChpPBKAkz54NZcgVCQAh+QQJCgAHACwAAAAAIAAgAAAD0Xi6zDGgyUmXGaaFUCXgzCAy19AxhKGGo/K0p6IawiKayg3GRzDbrReO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yYxJWqgUXTq8BqiVUSy24YmLOSi7Dn8sKQgfXpUXYIy/ZMyVa3wrjlKrhQ88UBIHatKAVD+wSCQA7') no-repeat center;
	}
	.c-border{
		margin-left: 120px;
	}
	.c-container h3::before{
		content: counter(resultNum) ". ";
		counter-increment: resultNum;
		color: #000 !important;
	}
	#container{
		width: 100%!important;
		margin: 0;
		padding: 0;
	}
	#content_left {
		width:90% !important;
		padding:0 5% !important;
		float:none!important;
	}
	#content_right{
		float:none!important;
		position: absolute;
		right: 5%;
	}
	.c-container{
		width: 100%!important;
		min-height: 90px;
	}
	.c-container>img{
		width:111px;
		height:82px;
		border: 1px solid #BBB;
		margin: 2px 4px 5px 0px;
	}

	.c-container{
		position: relative;
	}
	.c-container.loading::before{
		content: '';
		position: absolute;
		left: 1px;
		top: 3px;
		width: 111px;
		height: 82px;
		background: rgba(255,255,255,.8) var(--loading-spinner);
	}
	.c-container>img{
		opacity:1;
		transition: opacity 500ms ease-in-out 0s;
	}
	.c-container.loading>img{
		opacity:.3;
	}
	#loadingBar{
		width: 32px;
		height: 32px;
		margin-left: calc(50% - 16px);
		background: var(--loading-spinner);
	}
	#loadingBar:not(.loading){
		display:none;
	}
	
*/}).toString().replace(/^.*|\/\/.*|.*\}$/g, '');
if(navigator.userAgent.indexOf('WebKit')>-1 || //chrome 还不支持css变量;
	navigator.userAgent.indexOf('Firefox')>-1 && 
	parseInt(navigator.userAgent.match(/Firefox\/(\d+)/)[1]) < 31 //FF31下不支持标准css变量
){
	cssText = cssText.replace(/var\(--loading-spinner\)/g, cssText.match(/--loading-spinner\s*:(.*)/)[1]);
}
cE('style', {text: cssText}, document.head);
//noreferrer
cE('meta', {name: 'referrer', content: 'never'}, document.head);
//第一页预览
content_left && addPreviews(content_left.querySelectorAll('#content_left>div.c-container'));
onResize();
onScroll();
addEventListener('resize', onResize);
addEventListener('scroll', onScroll);