Last active
October 20, 2015 17:32
-
-
Save Wizek/529611e27ee0c34d8cb5 to your computer and use it in GitHub Desktop.
consumerreports.org calculate $/points for each item in the row, to see which item provides the most value for the buck
This file contains hidden or 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 Consumer Reports -- most value for the money | |
| // @namespace http://your.homepage/ | |
| // @version 0.1 | |
| // @description enter something useful | |
| // @author You | |
| // @match http://www.consumerreports.org/cro/* | |
| // @grant none | |
| // ==/UserScript== | |
| setTimeout(function () { | |
| var saltClass = 'custom-augment' | |
| var s = saltClass | |
| $('.' + s).remove() | |
| var chooseA = true | |
| var valueUnit = chooseA | |
| ? "pts/$100" | |
| : "$/pts" | |
| function allRows () { | |
| // TODO use `or` here | |
| return $('#sort-table tbody > tr, .ratings-chart-row').not('.rowBl, .border_bot') | |
| } | |
| allRows().each(function(_, e) { | |
| var overbar = $('.overbar', e).parent() | |
| var score = parseInt(overbar.text()) | |
| var price = parseInt(overbar.prev().text().replace(/\$/ig, "")) | |
| var value = chooseA | |
| ? score / (price/100) | |
| : price / score | |
| var roundedValue = Math.round(value*10)/10 | |
| var valueText = roundedValue | |
| var td = $('<td class="value ' + s + '">') | |
| td.addClass(s) | |
| td.attr('data-sort', value) | |
| td.css('min-width', "33px") | |
| td.text(valueText) | |
| overbar.after(td) | |
| }) | |
| $('.header.overall-score').next().each(function() { | |
| var el = $(this) | |
| var newEl = el.clone() | |
| newEl.addClass(saltClass) | |
| newEl.addClass('value') | |
| newEl.css({fontWeight:400}) | |
| newEl.find('img:not(.info)').parent().text(valueUnit) | |
| newEl.find('dd.middle').html('<strong>Value</strong><br>A description will be added here.') | |
| //debugger | |
| var newId = saltClass + "-value" | |
| var info = newEl.find('img.info') | |
| var popup = info.next() | |
| var oldId = popup.attr('id') | |
| popup.attr('id', newId) | |
| info.attr('onmouseover', info.attr('onmouseover').replace(oldId, newId)) | |
| el.before(newEl) | |
| newEl.click(sortByValue) | |
| }) | |
| function sortByValue() { | |
| var oldRows = allRows() | |
| var header = $('.value.header') | |
| var up = header.is('.headerSortUp') | |
| $('.headerSortUp, .headerSortDown') | |
| .removeClass('headerSortUp') | |
| .removeClass('headerSortDown') | |
| header.addClass(up ? 'headerSortDown' : 'headerSortUp') | |
| var sorted = oldRows.sort(function (a, b) { | |
| function f (x) { | |
| return parseFloat($(x).find('.value').attr('data-sort'), 10) | |
| } | |
| return up | |
| ? f(a) - f(b) | |
| : f(b) - f(a) | |
| }) | |
| var parent = oldRows.eq(0).parent() | |
| oldRows.remove() | |
| parent.prepend(sorted) | |
| } | |
| sortByValue() | |
| },2000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment