CH&L script to sort by total cost
CH&L script to sort by total cost
Author
Discussion

wemorgan

Original Poster:

3,583 posts

199 months

Wednesday 25th October 2017
quotequote all
This script sorts the CH&L results by total cost:

Note: author Rory McCrossan

// ==UserScript==
// @name Contract Hire and Leasing Calculator
// @namespace http://rorymccrossan.co.uk
// @version 1.0
// @description Adds helpful information to each deal
// @author Rory McCrossan
// @match http://www.contracthireandleasing.com/personal/*
// @match http://www.contracthireandleasing.com/business/*
// @match https://www.contracthireandleasing.com/personal/*
// @match https://www.contracthireandleasing.com/business/*
// @grant none
// ==/UserScript==

$('<style />', {
text: '.overall-cost { text-align: right; padding: 0; position: absolute; top: 0; left: 0; right: 0; background-color: #DDD; }' +
'.deal .title-wrap { padding: 0 0 0 10px; }' +
'.all-price-wrap { padding: 0 10px 10px 0 }' +
'ul.amortised li { list-style: inherit; display: inline-block; position: relative; padding-left: 15px; text-align: left; margin: 0 5px 0 15px; }' +
'ul.amortised li:before { position: absolute; left: 0; content: "\u25b6"; }' +
'.manufacturer-banner, #ootwWindow, .right-panel, .leaderboard-wrap, .deal-panel h2, .crosslinks, .inline-srb-wrap, footer { display: none; }' +
'.content { margin-top: 0; padding: 5px 3%; }' +
'#btnClose { top: 33px; right: 0; border-left: 5px solid #FFF }' +
'.deal-panel .listing-text { width: 50%; float: left; padding: 7px 0; }' +
'.pagination.posts { width: 50%; clear: none; padding: 0 0 10px; margin: 0;}' +
'.search-button-wrap { width: 25%; float: right; position: relative; padding: 0 }' +
'.keywords-wrap { width: 75%; float: left; margin: 0 }' +
'.adv-options { padding: 0 }' +
'#alldeals .deal { color: #333; background-color: transparent; border: 1px solid #DDD; padding-top: 30px; }' +
'#alldeals .deal:first-child { color: #080; background-color: #ecffef; border-color: #88E886; }' +
'#alldeals .deal:first-child .deal-table { background-color: transparent }' +
'#alldeals .deal:first-child ul.amortised li { font-weight: bold; }' +
'#alldeals .deal:first-child .overall-cost { background-color: #88E886; }'
}).appendTo('head');

$('.pagination.posts').clone(true).insertAfter('.listing-text
$('.search-button-wrap').appendTo('.adv-hold');
$('<div class="deal-container"></div>').appendTo('#alldeals');

$('#alldeals .deal').each(function(i) {
var $deal = $(this);

// price calc
var monthlyPrice = $deal.find('.deal-price').text().replace).replace(',', '');
var initialPayment = parseFloat($deal.find('.deal-user').text().replace).replace(',', ''));
var profileText = $deal.find('.personal-deal-profile').text

var monthRegex = /(\d+)\+(\d+)/gi;
var monthMatches = monthRegex.exec(profileText);

if (isNaN(initialPayment)) {
initialPayment = monthMatches[1] * monthlyPrice;
}

var months = parseInt(monthMatches[2]);
var years = (months + 1) / 12;

var totalCost = ((monthlyPrice * months) + initialPayment).toFixed(2);
var yearlyCost = (totalCost / years).toFixed(2);
var $costDiv = $('<div class="overall-cost" />');

var $amortisedData = $('<ul class="amortised" />').appendTo($costDiv);
$('<li />', {
text: '£' + groupNumber((yearlyCost / 12).toFixed(2)) + ' / mo',
class: 'monthly'
}).prependTo($amortisedData);
$('<li />', {
text: '£' + groupNumber(yearlyCost) + ' / yr',
class: 'yearly'
}).prependTo($amortisedData);
$('<li />', {
text: '£' + groupNumber(totalCost) + ' total',
class: 'total'
}).prependTo($amortisedData);

$deal.data('yearly-costearlyCost).prepend($costDiv);
}).sort(function(a, b) {
return $(a).data('yearly-cost') - $(b).data('yearly-cost');
}).appendTo('.deal-container');

function toFloat(input) {
return parseFloat(input.replace(/[^0-9\.]+/g,""));
}

function groupNumber(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

Defcon5

6,459 posts

212 months

Wednesday 25th October 2017
quotequote all
Am I the only person thinking how good that is, but doesn’t have a clue what to do with all the above?

cambb

66 posts

139 months

Wednesday 25th October 2017
quotequote all
Ive install Tamper Monkey and imported the script, but now stuck with what to do with it?

cambb

66 posts

139 months

Wednesday 25th October 2017
quotequote all
Scrub that. Got it working. Wow what a great addon

wemorgan

Original Poster:

3,583 posts

199 months

Wednesday 25th October 2017
quotequote all
Accelebrate said:
This is my working version, I think PH messes up some of the syntax:

https://gist.github.com/ianhampton/3a1760df294bec2...

wemorgan

Original Poster:

3,583 posts

199 months

Wednesday 1st November 2017
quotequote all
Has anyone else seen that the script is not working 100% correctly?

It sorts all the cars by price only for the cars listed on that specific page, but not all pages.
Go to page 2 and I see some cheaper cars on page 2 than page 1.


PenelopaPitstop

2,308 posts

154 months

Monday 26th March 2018
quotequote all
Script stopped sorting results by the price since weekend. Any help with updating?

mrunderhill

49 posts

114 months

Monday 26th March 2018
quotequote all
I changed the version from github, on line 42 where it says .deal-user change it to say .personal-deal-upfront

i.e.
instead of:-
var initialPayment = parseFloat($deal.find('.deal-user').text().replace('£', '').replace(',', ''));
have the line
var initialPayment = parseFloat($deal.find('.personal-deal-upfront').text().replace('£', '').replace(',', ''));

add1ct3dd

127 posts

139 months

Monday 26th March 2018
quotequote all
mrunderhill said:
I changed the version from github, on line 42 where it says .deal-user change it to say .personal-deal-upfront

i.e.
instead of:-
var initialPayment = parseFloat($deal.find('.deal-user').text().replace('£', '').replace(',', ''));
have the line
var initialPayment = parseFloat($deal.find('.personal-deal-upfront').text().replace('£', '').replace(',', ''));
Close, should be this:

var initialPayment = parseFloat($deal.find('.personal-deal-upfront').text().replace(',', ''));

PenelopaPitstop

2,308 posts

154 months

Monday 26th March 2018
quotequote all
Thanks.

ArsE92

21,145 posts

208 months

Tuesday 27th March 2018
quotequote all
PP - thanks for emailing me your script. I still can't get it to work though. laugh

You can see it's enabled through Tampermonkey but my results aren't being sorted or anything.



Anyone else having issues?

This is the code I'm using:

$('<style />', {
text: '.overall-cost { text-align: right; padding: 0; position: absolute; top: 0; left: 0; right: 0; background-color: #DDD; }' +
'.deal .title-wrap { padding: 0 0 0 10px; }' +
'.all-price-wrap { padding: 0 10px 10px 0 }' +
'ul.amortised li { list-style: inherit; display: inline-block; position: relative; padding-left: 15px; text-align: left; margin: 0 5px 0 15px; }' +
'ul.amortised li:before { position: absolute; left: 0; content: "\u25b6"; }' +
'.manufacturer-banner, #ootwWindow, .right-panel, .leaderboard-wrap, .deal-panel h2, .crosslinks, .inline-srb-wrap, footer { display: none; }' +
'.content { margin-top: 0; padding: 5px 3%; }' +
'#btnClose { top: 33px; right: 0; border-left: 5px solid #FFF }' +
'.deal-panel .listing-text { width: 50%; float: left; padding: 7px 0; }' +
'.pagination.posts { width: 50%; clear: none; padding: 0 0 10px; margin: 0;}' +
'.search-button-wrap { width: 25%; float: right; position: relative; padding: 0 }' +
'.keywords-wrap { width: 75%; float: left; margin: 0 }' +
'.adv-options { padding: 0 }' +
'#alldeals .deal { color: #333; background-color: transparent; border: 1px solid #DDD; padding-top: 30px; }' +
'#alldeals .deal:first-child { color: #080; background-color: #ecffef; border-color: #88E886; }' +
'#alldeals .deal:first-child .deal-table { background-color: transparent }' +
'#alldeals .deal:first-child ul.amortised li { font-weight: bold; }' +
'#alldeals .deal:first-child .overall-cost { background-color: #88E886; }'
}).appendTo('head');

$('.pagination.posts').clone(true).insertAfter('.listing-text');
$('.search-button-wrap').appendTo('.adv-hold');
$('<div class="deal-container"></div>').appendTo('#alldeals');

$('#alldeals .deal').each(function(i) {
var $deal = $(this);

// price calc
var monthlyPrice = $deal.find('.deal-price').text().replace('�', '').replace(',', '');
var initialPayment = parseFloat($deal.find('.personal-deal-upfront').text().replace(',', ''));
var profileText = $deal.find('.personal-deal-profile').text();

var monthRegex = /(\d+)\+(\d+)/gi;
var monthMatches = monthRegex.exec(profileText);

if (isNaN(initialPayment)) {
initialPayment = monthMatches[1] * monthlyPrice;
}

var months = parseInt(monthMatches[2]);
var years = (months + 1) / 12;

var totalCost = ((monthlyPrice * months) + initialPayment).toFixed(2);
var yearlyCost = (totalCost / years).toFixed(2);
var $costDiv = $('<div class="overall-cost" />');

var $amortisedData = $('<ul class="amortised" />').appendTo($costDiv);
$('<li />', {
text: '�' + groupNumber((yearlyCost / 12).toFixed(2)) + ' / mo',
class: 'monthly'
}).prependTo($amortisedData);
$('<li />', {
text: '�' + groupNumber(yearlyCost) + ' / yr',
class: 'yearly'
}).prependTo($amortisedData);
$('<li />', {
text: '�' + groupNumber(totalCost) + ' total',
class: 'total'
}).prependTo($amortisedData);

$deal.data('yearly-cost', yearlyCost).prepend($costDiv); }).sort(function(a, b) {
return $(a).data('yearly-cost') - $(b).data('yearly-cost'); }).appendTo('.deal-container');

function toFloat(input) {
return parseFloat(input.replace(/[^0-9\.]+/g,""));
}

function groupNumber(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }

PenelopaPitstop

2,308 posts

154 months

Tuesday 27th March 2018
quotequote all
I use Greasemonkey on Firefox and don't have any other special settings. Patched the script with the suggestion from yesterday on both my computers and it works. Except as stated above by Wemorgan, sometimes can find lower prices on second page. But the more filters applied, the better results.

Even yesterday, it worked ok when I filtered by brand and model. Just didn't work when I had too many results without filters.

silentbrown

10,293 posts

137 months

Sunday 1st July 2018
quotequote all
Heads up that the tampermonkey script for contracthireandleasing needs tweaking as they've redirected the domain .

Add the following to the top of the file below the other '@match' lines.

// @match *://leasing.com/personal/*
// @match *://leasing.com/business/*

Christian85

902 posts

159 months

Sunday 1st July 2018
quotequote all
Can someone please dumb down how I do this on my phone, I have zero idea what you guys are talking about but I want it.

Christian85

902 posts

159 months

Sunday 1st July 2018
quotequote all
Double post

silentbrown

10,293 posts

137 months

Sunday 1st July 2018
quotequote all
Christian85 said:
Can someone please dumb down how I do this on my phone, I have zero idea what you guys are talking about but I want it.
What phone? What browser?

Magic_Mike

2 posts

101 months

Sunday 1st July 2018
quotequote all
Anyone else having issues with this since the change to the website? I've added the new address and Chrome has found a matching script, however it doesn't seem to be doing anything. Thanks

silentbrown

10,293 posts

137 months

Sunday 1st July 2018
quotequote all
Magic_Mike said:
Anyone else having issues with this since the change to the website? I've added the new address and Chrome has found a matching script, however it doesn't seem to be doing anything. Thanks
No issues with the changes I posted earlier. Make sure you haven't used "www.leasing.com" by mistake - There's no "www." prefix now.

Noz85

96 posts

130 months

Monday 2nd July 2018
quotequote all
Looks like a really powerful tool, but there's some instructions that us mere mortals are missing here I think?

I have copied and pasted the script, in full from the OP from the first // to the last }, with the addition of the two new..

// @match *://leasing.com/personal/*
// @match *://leasing.com/business/*

..links and I have saved it. But I am not sure what to do now? How do you 'launch', or 'run' or 'open' it? I have tried opening the 'homepage' once I saved it but it just presents me with an error.

Using Chrome

Edited by Noz85 on Monday 2nd July 08:53


Edited by Noz85 on Monday 2nd July 08:54

Skodaman1

19 posts

115 months

Monday 2nd July 2018
quotequote all
Hi, i've got this to run on the page, but it isn't returning the summary:
Could someone post a known good script as of today please?



{ // ==UserScript==
// @name Contract Hire and Leasing Calculator
// @namespace http://rorymccrossan.co.uk
// @version 1.0
// @description Adds helpful information to each deal
// @author Rory McCrossan
// @match *://leasing.com/personal/*
// @match *://leasing.com/business/*
// @grant none
// ==/UserScript==

$('<style />', {
text: '.overall-cost { text-align: right; padding: 0; position: absolute; top: 0; left: 0; right: 0; background-color: #DDD; }' +
'.deal .title-wrap { padding: 0 0 0 10px; }' +
'.all-price-wrap { padding: 0 10px 10px 0 }' +
'ul.amortised li { list-style: inherit; display: inline-block; position: relative; padding-left: 15px; text-align: left; margin: 0 5px 0 15px; }' +
'ul.amortised li:before { position: absolute; left: 0; content: "\u25b6"; }' +
'.manufacturer-banner, #ootwWindow, .right-panel, .leaderboard-wrap, .deal-panel h2, .crosslinks, .inline-srb-wrap, footer { display: none; }' +
'.content { margin-top: 0; padding: 5px 3%; }' +
'#btnClose { top: 33px; right: 0; border-left: 5px solid #FFF }' +
'.deal-panel .listing-text { width: 50%; float: left; padding: 7px 0; }' +
'.pagination.posts { width: 50%; clear: none; padding: 0 0 10px; margin: 0;}' +
'.search-button-wrap { width: 25%; float: right; position: relative; padding: 0 }' +
'.keywords-wrap { width: 75%; float: left; margin: 0 }' +
'.adv-options { padding: 0 }' +
'#alldeals .deal { color: #333; background-color: transparent; border: 1px solid #DDD; padding-top: 30px; }' +
'#alldeals .deal:first-child { color: #080; background-color: #ecffef; border-color: #88E886; }' +
'#alldeals .deal:first-child .deal-table { background-color: transparent }' +
'#alldeals .deal:first-child ul.amortised li { font-weight: bold; }' +
'#alldeals .deal:first-child .overall-cost { background-color: #88E886; }'
}).appendTo('head');

$('.pagination.posts').clone(true).insertAfter('.listing-text');
$('.search-button-wrap').appendTo('.adv-hold');
$('<div class="deal-container"></div>').appendTo('#alldeals');

$('#alldeals .deal').each(function(i) {
var $deal = $(this);

// price calc
var monthlyPrice = $deal.find('.deal-price').text().replace('£', '').replace(',', '');
var initialPayment = parseFloat($deal.find('.deal-user').text().replace('£', '').replace(',', ''));
var profileText = $deal.find('.deal-profile').text

var monthRegex = /(\d+)\+(\d+)/gi;
var monthMatches = monthRegex.exec(profileText);

if (isNaN(initialPayment)) {
initialPayment = monthMatches[1] * monthlyPrice;
}

var months = parseInt(monthMatches[2]);
var years = (months + 1) / 12;

var totalCost = ((monthlyPrice * months) + initialPayment).toFixed(2);
var yearlyCost = (totalCost / years).toFixed(2);
var $costDiv = $('<div class="overall-cost" />');

var $amortisedData = $('<ul class="amortised" />').appendTo($costDiv);
$('<li />', {
text: '£' + groupNumber((yearlyCost / 12).toFixed(2)) + ' / mo',
class: 'monthly'
}).prependTo($amortisedData);
$('<li />', {
text: '£' + groupNumber(yearlyCost) + ' / yr',
class: 'yearly'
}).prependTo($amortisedData);
$('<li />', {
text: '£' + groupNumber(totalCost) + ' total',
class: 'total'
}).prependTo($amortisedData);

$deal.data('yearly-cost', yearlyCost).prepend($costDiv);
}).sort(function(a, b) {
return $(a).data('yearly-cost') - $(b).data('yearly-cost');
}).appendTo('.deal-container');

function toFloat(input) {
return parseFloat(input.replace(/[^0-9\.]+/g,""));
}

function groupNumber(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} }