apGoogleImageSearch.user.js

Google Image Search を AutoPagerize 対応にするフィルタースクリプト。thx!!! furyu-tei

// ==UserScript==
// @name           apGoogleImageSearch.user.js
// @namespace      http://furyu.tea-nifty.com/
// @description    prepare for AutoPagerize - Google Image Search ver.0.01d
// @include        http://images.google.*
// ==/UserScript==

(function(){

// ===== Parameters
var	LINK_CHANGE=true;
var	LINK_TARGET='_blank';
var	PATCH_LDRIZE=true;


// =====
var	w=window,d=w.document;
if (w!=top||d.getElementById('apg_patched')) return;


var	curUrl=w.location.href;

if (curUrl.match(/\/images\?/)) {

	var	pathToUrl=(function(){
		var wimg=new Image();
		var	spathToUrl=function(path){
			wimg.src=path;
			return wimg.src;
		}
		return function(path,base){
			if (!base) return spathToUrl(path);
			if (path.match(/^[a-z]+:/)) return path;
			var base=base.replace(/[?#].*$/,'');
			if (path.match(/^\//)) {	//	absolute?
				base=base.replace(/^([a-z]+:\/\/.*?)\/.*$/,'$1');
			}
			else if (!path.match(/^[?#]/)) {
				base=base.replace(/^(.*\/).*$/,'$1')
			}
			return base+path;
		}
	})();
	
	var	XHRexec=(function(){
		var	getXHR=(function(){
			if (typeof ActiveXObject!="undefined") {
				var	msXml=['Msxml2.XMLHTTP','Microsoft.XMLHTTP'],ci,len;
				for (ci=0,len=msXml.length; ci<len; ci++) {
					with ({msXml:msXml[ci]}) {
						try {
							new ActiveXObject(msXml);
							return function(){return new ActiveXObject(msXml)};
						}
						catch(e){}
					}
				}
				return function(){return null};
			}
			else if (typeof XMLHttpRequest!="undefined") {
				return function(){return new XMLHttpRequest()};
			}
			else {
				return function(){return null};
			}
		})();
		
		return function(opt){
			var	xh=getXHR();
			if (!xh) return;
			var	async=(opt.async===false)?false:true;
			var	method=opt.method.toUpperCase();
			if (!method) method='GET';
			var	url=pathToUrl(opt.url);
			var	headers=opt.headers;
			var	params=opt.params;
			var	data=(opt.data)?opt.data:'';
			if (!data&&params) {
				var	pstrs=[];
				for (var mem in params) {
					if (!params.hasOwnProperty(mem)) continue;
					pstrs[pstrs.length]=mem+'='+encodeURIComponent(params[mem]);
				}
				if (method=='POST') {
					data=pstrs.join('&');
				}
				else {
					url=url+'?'+pstrs.join('&');
				}
			}
			try {
				xh.open(method,url,async);
			}
			catch(e) {
				if (typeof opt.onerror=='function') opt.onerror(xh);
				return;
			}
			if (headers) {
				for (var mem in headers) {
					if (!headers.hasOwnProperty(mem)) continue;
					try {xh.setRequestHeader(mem,headers[mem])} catch(e){};
				}
			}
			if (method=='POST'&&data) {
				xh.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			}
			var	callback=function(timeout){
				if (timeout) {
					if (typeof opt.ontimeout=='function') {
						opt.ontimeout(xh);
					}
					else if (typeof opt.onerror=='function') {
						opt.onerror(xh);
					}
				}
				else if ((200<=xh.status&&xh.status<300)||xh.status==0) {
					if (typeof opt.onload=='function') opt.onload(xh);
				}
				else {
					if (typeof opt.onerror=='function') opt.onerror(xh);
				}
				try{delete xh;}catch(e){xh=null;}
			};
			if (async) {
				var	tid=null;
				if (opt.timeout) {
					tid=setTimeout(function(){
						xh.onreadystatechange=function(){};
						try{xh.abort();}catch(e){}
						callback(true);
					},1000*opt.timeout);
				}
				xh.onreadystatechange=function(){
					if (xh.readyState!=4) return;
					if (tid) clearTimeout(tid);
					setTimeout(function(){callback(false)},1);
				};
			}
			xh.send(data);
			if (!async) {
				callback(false);
			}
		};
	})();	//	end of XHRexec()
	
	var	classCssAddRule=function(doc){
		if (!doc) doc=d;
		var	styleElm=doc.createElement('style');
		styleElm.type='text/css';
		try {
			doc.getElementsByTagName('head')[0].appendChild(styleElm);
		}
		catch(e){
			return function(){};
		}
//		var	s=doc.styleSheets[doc.styleSheets.length-1];
		var	s=doc.styleSheets[0];
		if (s.insertRule) {		// for others
			return function(selector,property){
				s.insertRule(selector+'{'+property+'}',s.cssRules.length);
			};
		}
		else if (s.addRule) {	// for IE
			return function(selector,property){
				s.addRule(selector,'{'+property+'}');
			};
		}
		else return function(){};
	};	//	end of classCssAddRule()
	
	if (curUrl.match(/gbv=\d+/)) {
		var	chgUrl=curUrl.replace(/gbv=\d+/,'gbv=1');
	}
	else {
		var	chgUrl=curUrl+'&gbv=1';
	}
	
	var	cssAddRule=new classCssAddRule();
	
	cssAddRule('a.apg_image_link','text-decoration:none;');
	cssAddRule('a.apg_image_link img','border:none;');
	cssAddRule('a.apg_original_page','text-decoration:none;');
	cssAddRule('a.apg_google_frame','text-decoration:none;');
	cssAddRule('a.apg_original_page:link','color:navy;');
	cssAddRule('a.apg_google_frame:link','color:gray;');
	cssAddRule('a.apg_original_page:visited','color:navy;');
	cssAddRule('a.apg_google_frame:visited','color:gray;');
	
	var	tDataImageCount=0;
	var	fakeContent=null;
	XHRexec({
		method:'GET'
	,	url:chgUrl
	,	async:false
	,	onload:function(xh){
			if (xh.responseText.match(/<body[\s\S]*?>([\s\S]*)<\/body>/i)) {
				d.body.innerHTML=RegExp.$1;
				
				var	patch=function(doc){
					if (!doc) doc=d;
					var	link=doc.getElementById('nn').parentNode;
					link.rel='next';
					if (doc===d) link.id='apg_patched';
					var	imgContent=doc.getElementById('ImgCont');
					if (PATCH_LDRIZE) {
						imgContent.id='ImgContent';
						fakeContent=d.createElement('div');
						fakeContent.style.display='none';
						fakeContent.id='ImgContent';
						imgContent.parentNode.insertBefore(fakeContent,imgContent);
					}
					var	target=imgContent.firstChild;
					while (target) {
						while (target.nodeType==1&&target.nodeName.toUpperCase()=='TABLE') {
							target.className=(target.className||'')+' autopagerize_page_element';
							if (!LINK_CHANGE) break;
							
							//var	patImgLink=doc.createElement('a');
							var	patImgLink=d.createElement('a');
							patImgLink.target=LINK_TARGET;
							patImgLink.className='apg_image_link';
							patImgLink.title='Image';
							
							//var	patOrgLink=doc.createElement('a');
							var	patOrgLink=d.createElement('a');
							patOrgLink.className='apg_original_page';
							patOrgLink.target=LINK_TARGET;
							patOrgLink.innerHTML='\u25a0'; // '■'
							patOrgLink.title='Original Page';
							
							var	links=target.getElementsByTagName('a'),ilinks=[];
							for (var ci=0,len=links.length; ci<len; ci++) ilinks[ilinks.length]=links[ci];
							for (var ci=0,len=ilinks.length; ci<len; ci++) {
								var	ilink=ilinks[ci];
								if (!ilink.href.match(/\?imgurl=(.*?)&imgrefurl=(.*?)&/)) continue;
								
								var	pnode=ilink.parentNode;
								if (PATCH_LDRIZE) pnode.id='tDataImage'+tDataImageCount;
								tDataImageCount++;
								
								var	imgUrl=RegExp.$1,siteUrl=RegExp.$2;
								imgUrl=imgUrl.replace(/%3F/i,'?');
								siteUrl=siteUrl.replace(/%3F/i,'?');
								if (imgUrl.match(/^http:\/\/img.*?\.blogs\.yahoo\.co\.jp\/.*\/(.*?)\/folder\//)) {
									imgUrl='http://blogs.yahoo.co.jp/'+RegExp.$1+'/GALLERY/showimage_v2.html?id='+imgUrl;
								}
								var	imgLink=patImgLink.cloneNode(true);
								imgLink.href=imgUrl;
								imgLink.appendChild(ilink.firstChild);
								pnode.insertBefore(imgLink,ilink);
								
								var	orgLink=patOrgLink.cloneNode(true);
								orgLink.href=siteUrl;
								pnode.appendChild(orgLink);
								
								ilink.target=LINK_TARGET;
								ilink.title='Google Frame Page';
								ilink.innerHTML='\u25a0'; // '■'
								ilink.className=(ilink.className||'')+' apg_google_frame';
							}
							break;
						}
						target=target.nextSibling;
					}
				};
				patch(d);
				
				var	addFilter=function(){
					if (!window.AutoPagerize||!window.AutoPagerize.addDocumentFilter) {
						setTimeout(addFilter,10);
						return;
					}
					window.AutoPagerize.addDocumentFilter(function(doc,url,info){
						patch(doc);
						// to delete cookie(PREF= .. GBV=1 ..)
						setTimeout(function(){
							XHRexec({
								method	:	'get'
							,	url		:	url.replace(/gbv=1/i,'gbv=2')
							,	onload	:	function(){}
							,	onerror	:	function(){}
							});
						},0);
					});
				};
				addFilter();
			}
		}
	,	onerror:function(xh){}
	});
	
	// === suppress rewriting on resize event
	w.addEventListener('resize',function(e){
		if (!fakeContent) return;
		while (fakeContent.firstChild) imgContent.removeChild(imgContent.firstChild);
	},false);
}
	
})();

後で見る。