Last active
August 29, 2015 14:14
-
-
Save DaveKin/a3e5c334378513d1a257 to your computer and use it in GitHub Desktop.
User script for AT FPA fuel costs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name AT FPA fuel rating insert | |
// @namespace http://www.autotrader.co.uk/ | |
// @version 0.1 | |
// @description insert fuel ratings into search results | |
// @author Dave Kinsella | |
// @match http://www.autotrader.co.uk/classified/advert/* | |
// @grant none | |
// ==/UserScript== | |
(function(){ | |
var insertCostElement = function(mpgEl,fuel,mpg){ | |
console.log(arguments); | |
if(mpg){ | |
//calc cost here | |
var cost,price; | |
switch(fuel){ | |
case 'Petrol': | |
price = 106.45 * 4.54609; | |
break; | |
case 'Diesel': | |
price = 113.58 * 4.54609; | |
} | |
cost = (1000/mpg)*(price/100); | |
cost = Number(Math.round(cost+'e2')+'e-2'); | |
mpgEl.next('th').attr('title','Calculated using official manufacturer combined mpg figures and current average petrol/diesel prices based on 1000 miles per month.').html('Estimated monthly<br>fuel cost').next('td').text('£'+cost); | |
} | |
}; | |
var getCost = function(){ | |
var fueltype = $('.fpa-factslist > li:contains("Petrol"),.fpa-factslist > li:contains("Diesel")').first().text(); | |
var mpgEl = $('th:contains("Average mpg")').next('td'); | |
var mpg = mpgEl.text(); | |
var val = mpg.match(/\d+\.\d+/g); | |
if(val){ | |
val = val.join(""); | |
} | |
insertCostElement(mpgEl,fueltype,val); | |
}; | |
var init = function(){ | |
getCost(); | |
}; | |
var intervalInt = window.setInterval(function(){ | |
if(typeof jQuery !== 'undefined' && jQuery){ | |
window.clearInterval(intervalInt); | |
init(); | |
} | |
}, 100); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name AT Search fuel rating insert | |
// @namespace http://www.autotrader.co.uk/ | |
// @version 0.1 | |
// @description insert fuel ratings into search results | |
// @author Dave Kinsella | |
// @match http://www.autotrader.co.uk/search/* | |
// @grant none | |
// ==/UserScript== | |
(function(){ | |
var getResults = function(){ | |
return $('li.search-page__result'); | |
}; | |
var insertCostElement = function(result,fuel,mpg,tax){ | |
var display = $(result).find('.search-result__report-ad').html(''); | |
if(mpg){ | |
//calc cost here | |
var cost,price; | |
switch(fuel){ | |
case 'Petrol': | |
price = 106.45 * 4.54609; | |
break; | |
case 'Diesel': | |
price = 113.58 * 4.54609; | |
} | |
cost = (1000/mpg)*(price/100); | |
cost = Number(Math.round(cost+'e'+2)+'e-'+2); | |
display.append('<span title="Calculated using official manufacturer combined mpg figures and current average petrol/diesel prices based on 1000 miles per month."><span style="display: inline-block;background-image: url(http://myfireworksworld.com/wp-content/uploads/2010/06/Gas-Pump.png); background-size: contain; width: 1.5em; height: 1.5em; background-position: center; background-repeat: no-repeat; margin-bottom: -0.4em;"></span> £'+cost+' / month (estimated)</span>') | |
} | |
if(tax){ | |
display.append(' <span style="margin-left:1em;"><span style="display: inline-block;background-image: url(http://cdn.flaticon.com/png/256/60503.png); background-size: contain; width: 1.5em; height: 1.5em; background-position: center; background-repeat: no-repeat; margin-bottom: -0.4em;"></span> '+tax+' Annual Tax</span>'); | |
} | |
}; | |
var getCost = function(result){ | |
$result = $(result); | |
var fueltype = $result.find('.search-result__attributes > li:last').text(); | |
var link = $(result).find('.search-result__title > a').attr('href'); | |
$.ajax({url:link, dataType:'html'}).done(function(data){ | |
var mpg = $(data).find('th:contains("Average mpg")').next('td').text(); | |
var tax = $(data).find('th:contains("Annual Tax")').next('td').text(); | |
var val = mpg.match(/\d+\.\d+/g); | |
if(val){ | |
val = val.join(""); | |
} | |
insertCostElement(result,fueltype,val,tax); | |
}); | |
}; | |
var insertSearchFilter = function(){ | |
var newfilter = '<li><div class="flyout-menu js-search-option" data-field-name="fuel-cost">' | |
+'<button type="button" class="options-button js-button " data-button-for="fuel-type">' | |
+'<span class="options-button__inner">' | |
+'<span class="options-button__name">Average Fuel Cost</span>' | |
+'<span class=" options-button__value js-selected-value">Any</span>' | |
+'<span class="options-button__icon"><i class="icon-right-arrow js-button-icon"></i></span>' | |
+'</span>' | |
+'</button>' | |
+'</div></li>'; | |
$('.search-form__fields-list > li:eq(6)').after(newfilter); | |
} | |
var init = function(){ | |
insertSearchFilter(); | |
var $results = getResults(); | |
$results.each(function(){ | |
getCost(this); | |
}); | |
}; | |
var intervalInt = window.setInterval(function(){ | |
if(typeof jQuery !== 'undefined' && jQuery){ | |
window.clearInterval(intervalInt); | |
init(); | |
} | |
}, 100); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment