NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Cappone pizza search // @version 1.0.0 // @grant none // @include http://www.pizza-capone.cz/pizza/ // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @copyright 2018, Mr.Koci (https://openuserjs.org//users/Mr.Koci) // @license MIT // ==/UserScript== //console.log($('div[class^="pizzasortpolozka"]')); function unique(arr) { let u = {}; return arr.filter((v) => { return u[v] = !u.hasOwnProperty(v); }); } function pushIntoUniqueArray(arr, item) { if (!arr.includes(item)) { arr.push(item); return true; } return false; } function removeItem(list, item) { var index = list.indexOf(item); if (index !== -1) list.splice(index, 1); } function search() { $('#pizzalist').empty(); pizzaLoop: for(var p=0; p<pizzas.length; p++) { for(var i=0; i<selectedWant.length; i++) { if (!pizzas[p].ingredients.includes(selectedWant[i])) continue pizzaLoop; } for(var i=0; i<selectedDontWant.length; i++) { if (pizzas[p].ingredients.includes(selectedDontWant[i])) continue pizzaLoop; } $('#pizzalist').append('<div class="pizzasortpolozka1" style="margin-bottom: 15px;">'+pizzas[p].html+'</div>'); } } var pizzas = []; var ingredientList = []; var selectedWant = []; var selectedDontWant = []; $('div[class^="pizzasortpolozka"]').each(function(){ //console.log($(this).find(".pizzasorttext").text()); var istr = $(this).find(".pizzasorttext").text(); if (istr.includes(" (")) { var ingredients = istr.substring(0, istr.indexOf(" (")).replace(/\n|(¼ )/g, '').split(", "); //console.log(ingredients); for(var i=0; i<ingredients.length; i++) { //console.log(ingredients[i]); if (!ingredientList.includes(ingredients[i])) ingredientList.push(ingredients[i]); } pizzas.push({"ingredients": ingredients, "html": $(this).html()}); } }); ingredientList.sort(); console.log(ingredientList); console.log(pizzas); var selectWant = '<div style="float:left; padding-bottom: 20px;">Musí obsahovat: <select name="want">'; for(var i=0; i<ingredientList.length; i++) { selectWant += '<option name="'+ingredientList[i]+'">'+ingredientList[i]+'</option>'; } selectWant += '</select><div id="selectedWant"></div></div>'; var selectDontWant = '<div style="float:left; padding-bottom: 20px;">Nesmí obsahovat: <select name="dontwant">'; for(var i=0; i<ingredientList.length; i++) { selectDontWant += '<option name="'+ingredientList[i]+'">'+ingredientList[i]+'</option>'; } selectDontWant += '</select><div id="selectedDontWant"></div></div>'; $('.pizzasortdiv').remove(); $('.plochaodsazeni h1').after('<div id="pizzalist" style="clear:both;"></div>'); $('.plochaodsazeni h1').after('<div id="selectionWrapper" style="clear: both;"></div>'); $('#selectionWrapper').append(selectWant); $('#selectionWrapper').append(selectDontWant); search(); $('.plochaodsazeni').delegate('select').change(function(e) { var arr = e.target.name=='want'?selectedWant:selectedDontWant; if(pushIntoUniqueArray(arr, e.target.value)) { var container = e.target.name=='want'?$('#selectedWant'):$('#selectedDontWant'); container.empty(); for(var i=0; i<arr.length; i++) { container.append('<div><span style="cursor: pointer;">'+arr[i]+'</span></div>'); } } search(); }); $('.plochaodsazeni').delegate('#selectedWant span', 'click', function (e) { removeItem(selectedWant, e.target.textContent); e.target.remove(); search(); }); $('.plochaodsazeni').delegate('#selectedDontWant span', 'click', function (e) { removeItem(selectedDontWant, e.target.textContent); e.target.remove(); search(); });