A Better eBay Filter

By Michael Devore Last update Apr 24, 2007 — Installed 406 times.
// abef.user.js
//
// Copyright 2007, Michael Devore
// This file is licensed under the terms of the Artistic License.
// See http://www.opensource.org/licenses/artistic-license.php for the license itself.
//
// This is a Greasemonkey script.
// See http://greasemonkey.mozdev.org/ for more information on Greasemonkey.
//
// ==UserScript==
// @name          A Better eBay Filter
// @namespace     http://www.devoresoftware.com/gm/abef
// @description	Allow filtering eBay searches based on additional criteria, Global site support
// @include       http://*listings*.ebay.*/*
// @include       http://*search*.ebay.*/*
// ==/UserScript==
//
// Work begun, February 2007
// Version 1.0, released April 2007
// Version 2.0, released late-April 2007

var defaultScoreThreshold = 50;
var defaultPercentThreshold = 98.5;
var defaultScoreMax = "";
var FILTER_SCORE_FLAG = 1;
var FILTER_PERCENT_FLAG = FILTER_SCORE_FLAG << 1;
var FILTER_SCOREMAX_FLAG = FILTER_PERCENT_FLAG << 1;
var filteredCount = 0;
var buttonText = "button";
var divText = "DIV";
var spanText = "SPAN";
var inputText = "INPUT";
var brText = "BR";
var noneText = "none";
var xText = "x";
var changedText = "\u00a0 (Values changed.)";
var changeNode = null;
var configNode = null;
var filteredNode = null;
var scoreThreshold = defaultScoreThreshold;
var percentThreshold = defaultPercentThreshold;
var scoreMax = defaultScoreMax;

var abefActiveControl = null;

function getAbefConfiguration()
{
	var abefParam = GM_getValue("scoreThreshold", xText);
	if (abefParam != xText)
	{
		scoreThreshold = abefParam - 0;
	}
	else
	{
		GM_setValue("scoreThreshold", scoreThreshold + "");
	}
	abefParam = GM_getValue("percentThreshold", xText);
	if (abefParam != xText)
	{
		percentThreshold = abefParam - 0;
	}
	else
	{
		GM_setValue("percentThreshold", percentThreshold + "");
	}
	abefParam = GM_getValue("scoreMax", xText);
	if (abefParam != xText)
	{
		if (isNaN(parseInt(abefParam)))
		{
			scoreMax = "";
		}
		else
		{
			scoreMax = abefParam - 0;
		}
	}
	else
	{
		GM_setValue("scoreMax", scoreMax + "");
	}
}

function updateAbefConfiguration()
{
	GM_setValue("scoreThreshold", scoreThreshold + "");
	GM_setValue("percentThreshold", percentThreshold + "");
	GM_setValue("scoreMax", scoreMax + "");
}

function showFilteredCount()
{
	filteredNode.nodeValue =
		"\u00a0 " + filteredCount + " entr"+ (filteredCount != 1 ? "ies" : "y") + " filtered from view";
	configNode.parentNode.appendChild(filteredNode);
}

function newFilterValues(event)
{
	if (event != null)
	{
		event.preventDefault();
		event.stopPropagation();
	}
	if (configNode.nextSibling == changeNode)
	{
		configNode.parentNode.removeChild(changeNode);
	}

	var fNode = document.getElementById("fbScore");
	scoreThreshold = fNode.value - 0;	// force numeric
	fNode = document.getElementById("fbPercent");
	percentThreshold = fNode.value - 0;
	fNode = document.getElementById("fbMax");
	if (isNaN(parseInt(fNode.value)))
	{
		scoreMax = "";
	}
	else
	{
		scoreMax = fNode.value - 0;
	}
	updateAbefConfiguration();
	walkTheListings();
}

function changedFilter(event)
{
	if (event != null)
	{
		event.preventDefault();
		event.stopPropagation();
	}
	if (configNode.nextSibling == changeNode)
	{
		return;
	}
	if (configNode.nextSibling == filteredNode)
	{
		configNode.parentNode.removeChild(filteredNode);
	}
	configNode.parentNode.appendChild(changeNode);
}

function getFeedbackScore(textNode)
{
	if (!textNode.nodeValue)
	{
		return xText;
	}
	var s = textNode.nodeValue;
	var result;
	if ((result = s.match(/:\s+\((-?[0-9]+)\)/)) != null)
	{
		return result[1] - 0;	// force numeric
	}
	return xText;
}

function getFeedbackPercent(textNode)
{
	if (!textNode.nodeValue)
	{
		return xText;
	}
	var s = textNode.nodeValue;
	var result;
	// globalize by accepting comma as a period
	if ((result = s.match(/([\.,0-9]+)%/)) != null)
	{
		// change comma, if any, to period
		result[1] = result[1].replace(/,/, ".");
		return result[1] - 0;	// force numeric
	}
	return xText;
}

function shouldFilterOut(trNode)
{
	var cNode = trNode.firstChild;
	var validScore = false;
	var validPercent = false;
	var feedbackScore = 0;
	var feedbackPercent = 0;
	while (!validScore && cNode)
	{
		if (cNode.nodeName === "TD" && cNode.getAttribute('class') == "ebcTtl")
		{
			var c2Node = cNode.firstChild;
			while (!validScore && c2Node)
			{

				if (c2Node.nodeName == "DIV")
				{
					var c3Node = c2Node.firstChild;
					while ((!validPercent || !validScore) && c3Node)
					{
						if (c3Node.nodeName === "#text")
						{
							// try for feedback score
							if ((feedbackScore = getFeedbackScore(c3Node)) != xText)
							{
								validScore = true;
							}
						}
						else if (c3Node.nodeName === "SPAN")
						{
							// try for feedback positive percentage
							var c4Node = c3Node.firstChild;
							while (!validPercent && c4Node)
							{
								if (c4Node.nodeName === "#text")
								{
									if ((feedbackPercent = getFeedbackPercent(c4Node)) != xText)
									{
										validPercent = true;
									}
								}
								c4Node = c4Node.nextSibling;
							}
						}
						c3Node = c3Node.nextSibling;
					}
				}

				c2Node = c2Node.nextSibling;
			}
		}
		cNode = cNode.nextSibling;
	}

	var retValue = 0;
	if (validScore && feedbackScore < scoreThreshold)
	{
		retValue |= FILTER_SCORE_FLAG;
	}
	if (validPercent && feedbackPercent < percentThreshold)
	{
		retValue |= FILTER_PERCENT_FLAG;
	}
	if (validScore && !isNaN(parseInt(scoreMax)) && feedbackScore > scoreMax)
	{
		retValue |= FILTER_SCOREMAX_FLAG;
	}
	if (retValue)
	{
		filteredCount++;
	}

	return retValue;
}

function walkTheListings()
{
	var xpath = "//table[contains(@class,'single')]/tbody/tr[contains(@class,'single')]";
	var trNodes = document.evaluate(
		xpath,
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null
	);

	filteredCount = 0;
	for (var loopVar = 0; loopVar < trNodes.snapshotLength; loopVar++)
	{
		var trNode = trNodes.snapshotItem(loopVar);
		var result;
		if (result = shouldFilterOut(trNode))
		{
			if (trNode.style.display != noneText)
			{
				trNode.style.display = noneText;
			}
		}
		else
		{
			trNode.style.display = "";
		}
	}
	showFilteredCount();
}

function buildControls()
{
	var xpath = "//div[@id='basicsearch']/div";
	var divNodes = document.evaluate(
		xpath,
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null
	);
	if (divNodes.snapshotLength <= 0)
	{
		return false;
	}
	changeNode = document.createTextNode(changedText);
	filteredNode = document.createTextNode("\u00a0 0 entries filtered from view");

	var divSibling = divNodes.snapshotItem(0);
	var newDivNode = document.createElement(divText);

	var span1Node = document.createElement(spanText);
	span1Node.style.fontWeight = "bold";
	var input1Node = document.createElement(inputText);
	input1Node.type = "text";
	input1Node.size = 4;
	input1Node.style.textAlign = "right";
	input1Node.setAttribute("id", "fbScore");
	input1Node.defaultValue = scoreThreshold;
	input1Node.addEventListener('change', changedFilter, false);
	span1Node.appendChild(input1Node);
	span1Node.appendChild(document.createTextNode(" Minimum feedback score\u00a0\u00a0\u00a0\u00a0"));

	newDivNode.appendChild(span1Node);
	var span2Node = document.createElement(spanText);
	span2Node.style.fontWeight = "bold";
	var input2Node = document.createElement(inputText);
	input2Node.type = "text";
	input2Node.size = 4;
	input2Node.style.textAlign = "right";
	input2Node.setAttribute("id", "fbPercent");
	input2Node.defaultValue = percentThreshold;
	input2Node.addEventListener('change', changedFilter, false);
	span2Node.appendChild(input2Node);
	span2Node.appendChild(document.createTextNode("% Minimum positive feedback\u00a0\u00a0\u00a0\u00a0"));
	newDivNode.appendChild(span2Node);

	var spanMaxNode = document.createElement(spanText);
	spanMaxNode.style.fontWeight = "bold";
	var inputMaxNode = document.createElement(inputText);
	inputMaxNode.type = "text";
	inputMaxNode.size = 4;
	inputMaxNode.style.textAlign = "right";
	inputMaxNode.setAttribute("id", "fbMax");
	inputMaxNode.defaultValue = scoreMax;
	inputMaxNode.addEventListener('change', changedFilter, false);
	spanMaxNode.appendChild(inputMaxNode);
	spanMaxNode.appendChild(document.createTextNode(" Maximum feedback score (blank to disable)"));
	newDivNode.appendChild(spanMaxNode);

	// second line
	newDivNode.appendChild(document.createElement(brText));
	configNode = document.createElement(buttonText);
	configNode.appendChild(document.createTextNode("Update Filter"));
	configNode.addEventListener('click', newFilterValues, false);
	newDivNode.appendChild(configNode);

	divSibling.parentNode.insertBefore(newDivNode, divSibling.nextSibling);

	return true;
}

function init()
{
	getAbefConfiguration();
	if (!buildControls())
	{
		return;
	}
	walkTheListings();
}

function main()
{
	if (!GM_setValue)
	{
		return;
	}
	init();
}

//window.addEventListener("load", main, true );
main();