ebay Country Filter

Attempts to clear up unwanted items in your ebay search results

当前为 2016-03-25 提交的版本,查看 最新版本

// ==UserScript==
// @name         ebay Country Filter
// @namespace    https://greasyfork.org/
// @version      0.2.5
// @description  Attempts to clear up unwanted items in your ebay search results
// @author       Schabernack
// @match        http://www.ebay.com/sch/i.html*
// @grant        none
// ==/UserScript==

/*
ebay Country Filter

Attempts to clear up unwanted items in your ebay search results
by hiding items being sold from certain countries

A work in progress

Todo: 
	Support for languages other than English? 
	Sooooooo much more
*/

(function() {
	//print various debugging info, slightly more convienient than using the console sometimes
	var DEBUGON = true;

	//localStorage.setItem("ecfCountriesList", null);
	var totalHidden = 0;

	var countriesList = JSON.parse(localStorage.getItem("ecfCountriesList"));

	//localstorage ecfEnabled is a STRING. Need to convert it to a boolean
	var enabled = (localStorage.getItem("ecfEnabled") === "true");

	if (DEBUGON) {
		console.log("\ninitial storage values");
		console.log("ecfEnabled: ", enabled);
		console.log("ecfCountriesList:", countriesList);
	}


	//if getItem returns null then this is the first time the script it being used
	if (countriesList === null) {
		countriesList = [];
	}

	if (DEBUGON) {
		console.log("\nafter storage values");
		console.log("ecfEnabled: ", enabled);
		console.log("ecfCountriesList:", countriesList);
	}

	//insert the markup into the sidebar with the other default filters
	//the html here is complicated mostly because I wanted to keep the same aesthetics
	//and structure as the rest of the page
	//important so this plays nicely with other ebay userscripts
	var enableText = `
	<div id='ecf_controls'> 
		<div class='asp asp-left-nav'> 
			<div class='pnl-h'> 
				<h3>Country Filter - Enable</h3>
				<div class='pnl-b pad-bottom'>
					<div class='cbx'> 
						<span> 
							<input type='checkbox' name='ecf_enable' id='ecf_enable' 
								value='enable' class='cbx' ${enabled ? "checked" : ""} /> 
							<label for='ecf_enable'><span class='cbx'>Enabled</span></label> 
						</span> 
					</div> 
				</div> 
			</div> 
		</div> 
	</div> 
	<div id='ecf_countries' style='display: ${enabled ? "block" : "none"}'> 
		<div class='asp'> 
			<div class='pnl-h'> 
				<h3>Country Filter - List</h3>
				<div class='pnl-b pad-bottom'>
					<div id='ecf_countries_list'></div> 
					<div class='cbx'>
						<span class='cbx'> 
							<a id='ecf_add'>Add New Country</a> 
						</span>
					</div>
					<a id='ecf_apply'><input class='sprBtnSRP1 submit' type='button'>
					<span>Apply Changes<span></a>
				</div>
			</div>
		</div> 
	</div>`;

	//insert the markup into the sidebar
	$(".lct-lnks").eq(0).after(enableText);

	//display the list of country list checkboxes
	if (countriesList.length !== 0) {
		countriesList.forEach(function(country) {
			printCountry(country);
		});
	}

	$("#ecf_add").on("click", function() {
		var val = prompt("Enter country name");

		//attempt to add country to list and display it if it's valid
		if(addCountry(val)){
			printCountry(val);
		}
	});

	/*	
	adds country to master list

	@return true 	The country given is valid and could be added
	@return false 	The country given is empty/spaces
	*/
	var addCountry = function(country) {
		country = country.trim();
		
		//if user entered an empty string or a bunch of spaces, ignore it
		if (country === "") {
			return false;
		}

		countriesList.push(country);
		localStorage.setItem("ecfCountriesList", JSON.stringify(countriesList));

		if(DEBUGON){
			console.log("new country list: ", countriesList);
		}

		return true;
	};

	var removeCountry = function(country) {
		var index = countriesList.indexOf(country);
		countriesList.splice(index, 1);
		
		if(DEBUGON){
			console.log("new country list: ", countriesList);
		}

		localStorage.setItem("ecfCountriesList", JSON.stringify(countriesList));
	}

	//displays a given country on the page
	function printCountry(country) {
		var valStripped = country.replace(" ", "");
		$("#ecf_countries_list").append(`
		<div class='cbx'> 
			<span> 
				<input type='checkbox' name='ecf_${valStripped}' 
					id='ecf_${valStripped}' value='${country}' 
					class='cbx ecf_country_checkbox' checked />
				<label for='ecf_${valStripped}"'>
					<span class='cbx'>${country}</span></label>
			</span>
		</div>`);
	}

	$("#Results").on("click", ".ecf_expander", function() {
		$(this).next().slideToggle(550, "swing");
	});

	$("#ecf_enable").on("click", function() {
		if (enabled == true) {
			enabled = false;
			localStorage.setItem("ecfEnabled", false);
			$("#ecf_countries").hide();
			//hide the country filter - list div
		} else {
			enabled = true;
			localStorage.setItem("ecfEnabled", true);
			$("#ecf_countries").show();
			//show the country filter - list div
		}

		if (DEBUGON) {
			console.log("ecfEnabled: " + localStorage.getItem("ecfEnabled"));
		}

	});

	$("#ecf_apply").on("click", function(){
		location.reload();
	});


	$("#ecf_countries_list").on("click", ".ecf_country_checkbox", function(){
		var country = $(this).val();

		if(DEBUGON){
			console.log(country);
		}

		removeCountry(country);

		$(this).parent().parent().remove(); //structure: <div><span><input /> ...

	});

	if(enabled){
		//go through each item div on the page and hide it if it's from an unwanted country
		$(".lvresult ").each(function(index, obj) {
			var countriesListText = countriesList.join("|");
			console.log("countriestListText: ", countriesListText);
			//if the list of countries is empty or the user only added empty strings as countries
			if(countriesListText === ""){
				return false;
			}

			var locText = obj.textContent.trim();
			var regex = new RegExp("(?:From )(?:" + countriesList.join("|") + ")", "i");

			console.log("regex: ", regex);

			if (regex.test(locText)) {
				//add wrapper and expand button
				obj.innerHTML = "<div class='ecf_expander'>[+] Hidden - Click to expand</div><div class='ecf_wrapper'>" + obj.innerHTML + "</div>";
				$(this).addClass("ecf_hidden");
				totalHidden++;
			}
		});
	}

	if (DEBUGON) {
		console.log("Total Hidden: " + totalHidden + "\n");
	}


	var sheet = (function() {

		var style = document.createElement("style");

		//for webkit
		style.appendChild(document.createTextNode(""));

		document.head.appendChild(style);

		return style.sheet;
	})();

	sheet.addRule(".ecf_wrapper", "background: #333333; border: 5px solid brown; overflow: auto");
	sheet.addRule(".ecf_expander", "cursor: pointer; height: 1.5em; line-height: 1.5em; color: #555; background-color: #fafafa; border: ");
	sheet.addRule(".ecf_hidden .ecf_wrapper", "background: #cccccc; display: none;");
	sheet.addRule("#ecf_controls", "margin-bottom:5px;");
	sheet.addRule("#ecf_add", "display:block; margin-top: 6px");
	sheet.addRule("#ecf_apply", "display:block; margin-top: 15px;color:#333333");
	sheet.addRule("#ecf_apply input", "margin-right:5px;margin-top:-2px");
})();