Source for "ebaySearchPictures"

By Johnny Bravo
Has 13 other scripts.


// ==UserScript==
// @name		ebaySearchPictures
// @namespace	http://au.geocities.com/jplsek2000/
// @description	Add pictures in search where no preview picture exists V5.0
// @include		http://*search*.ebay.*/*
// @include		http://*listings.ebay.*/*
// @include		http://my.ebay.*/ws/eBayISAPI.dll*
// @include		http://stores.ebay.*/*
// ==/UserScript==
// (c) 2005-2008 John Plsek, PC GraFix

function $i(s) { return document.getElementById(s); }
function $e(type, attributes){
	var node = document.createElement(type);
	for (var attr in attributes) if (attributes.hasOwnProperty(attr)){
		node.setAttribute(attr, attributes[attr]);
	}
	return node;
}
function $p(p) {
	var result = [], xpr = document.evaluate(p, document, null, null, null);
	while(next=xpr.iterateNext()) result.push(next);
	return result;
}
function $xGet(url, cb) {
	GM_xmlhttpRequest({
		method: "GET",
		headers: { "User-Agent":"Mozilla/5.0 Gecko","Accept":"text/html,text/xml,text/plain" },
		url: url,
		onload: function(xhr) { cb(xhr); }
	});
}
var $ev= {
	_reg: null,
	Init: function() {
		if (this._reg == null) {
			this._reg = [];
			$ev.Add(window, "_unload", this.CleanUp);
		}
	},
	Add: function(o, t, f, c) {
		this.Init();
		var r=(t=="_unload"?"unload":t);
		if (typeof o == "string") o = $i(obj);
		if (o == null || f == null) return false;
		if(o.addEventListener) o.addEventListener(r, f, c);
		this._reg.push({o:o, t:t, f:f, c:c});
		return true;
	},
	CleanUp: function() {
		$ev._reg.forEach(function(e) {
			if(e.t=="unload") e.f();
			if(e.o.removeEventListener) e.o.removeEventListener(e.t,e.f,e.c);
		});
		$ev._reg = null;
	}
};
var maxSize=80;
if(/http:\/\/my\.ebay\./i.test(document.location.href)) maxSize=64;
// if(/http:\/\/stores\.ebay\./i.test(document.location.href)) maxSize=64;
$ev.Add(document.body, 'load', function() {
	$p('//IMG').forEach(function(img) {	if (img.naturalHeight == 0) img.src=img.src; })
}, false);
function makeThumb(inX, inY, maxX, maxY) {
	var retVal={changed:false, widthChanged:false, heightChanged:false, width:inX, height:inY};
	if(inX>maxX || inY>maxY) {
		retVal.changed=true;
		var xRatio=1.0*inX/(1.0 * maxX);
		var yRatio=1.0*inY/(1.0 * maxY);
		if(yRatio>xRatio) {
			retVal.height=maxY;
			retVal.heightChanged=true;
			retVal.width=Math.ceil((1.0*inX)/yRatio);
		}
		else {
			retVal.width=maxX;
			retVal.widthChanged=true;
			retVal.height=Math.ceil((1.0*inY)/xRatio);
		}
	}
	return retVal;
}
function imageLoad(e) {
	var tImg=e.currentTarget;
	var img=$i(tImg.alt);
	if(img) {
		var dimension=makeThumb(tImg.width, tImg.height, maxSize, maxSize);
		if(dimension.changed) {
			if(dimension.widthChanged) {
				img.style.width=dimension.width+"px";
				img.style.height="auto";
			}
			if(dimension.heightChanged) {
				img.style.height=dimension.height+"px";
				img.style.width="auto";
			}
		}
		img.src=tImg.src;
		img.style.border="none";
	}
}
var tImgs=[];
function replaceImage(img, url) {
	var tImg=$e('IMG', { 'alt':img.id });
	tImgs.push(tImg);
	tImg.addEventListener("load", imageLoad, false);
	tImg.src=url;
	img.style.border='solid #00FF00 4px';
}
var noThumbsUrl = [
	'//IMG[@src="http://pics.ebaystatic.com/aw/pics/icon/iconPic_20x20.gif"',
	'@src="http://pics.ebaystatic.com/aw/pics/de/lst/_p__64x15.gif"',
	'@src="http://pics.ebaystatic.com/aw/pics/lst/_p__64x15.gif"'
].join(' or ')+']';
$p(noThumbsUrl).forEach(function(noThumb, i) {
	noThumb.id="Thumb"+i;
	noThumb.style.border="solid red 4px"
	var theUrl=noThumb.parentNode.href;
	$xGet(theUrl, function(resp) {
		if(resp.status=="200") {
			var txt=resp.responseText;
			var lnk=txt.indexOf('href="#stockphoto"');
			if(lnk<0) lnk=txt.indexOf('href="#ebayphotohosting"');
			if(lnk<0) return;
			var img=txt.indexOf('<img ',lnk);
			if(img<0) return;
			var src=txt.indexOf('src="', img);
			if(src<0) return;
			var srcEnd=txt.indexOf('"', src+5);
			if(srcEnd<0) return;
			replaceImage(noThumb, txt.substring(src+5,srcEnd));
		}
	});
});