CarKing001

CarKing001 - 车王二手车残值率

  1. // ==UserScript==
  2. // @name CarKing001
  3. // @namespace https://greasyfork.org/users/11909
  4. // @description CarKing001 - 车王二手车残值率
  5. // @include http://www.carking001.com/ershouche/*
  6. // @version 2016.03.08.01
  7. // @author OscarKoo
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. ;(function($) {
  12. var getKM = function(text) {
  13. var km = text.substr(0, text.length - 2);
  14. var index = km.indexOf('万');
  15. return parseFloat(index >= 0 ? (km.substr(0, km.length - 1) * 10000) : km);
  16. };
  17.  
  18. var getMonths = function(text) {
  19. if (!text || text == '-') return 1;
  20. var dates = text.split('/')
  21. var now = new Date();
  22. return (now.getFullYear() - parseInt(dates[0], 10)) * 12 + (now.getMonth() + 1 - parseInt(dates[1], 10));
  23. };
  24.  
  25. var getNewPrice = function(text) {
  26. return parseFloat(text.match(/¥(\d+\.\d*)万/)[1]);
  27. };
  28.  
  29. var getOriginalPrice = function(text) {
  30. return parseFloat(text.match(/新车价:(\d+\.*\d*)万/)[1]);
  31. };
  32.  
  33.  
  34. var calculate = function() {
  35. $('ul.carList>li>.fl').each(function() {
  36. try {
  37. var km, months, newPrice, originalPrice, p;
  38. $(this).children('p').filter(function(index) {
  39. return index == 0 || index == 1;
  40. }).each(function(index) {
  41. switch (index) {
  42. case 0:
  43. var span = $(this).children('span');
  44. km = getKM(span.eq(0).text());
  45. months = getMonths(span.eq(1).text());
  46. break;
  47. case 1:
  48. p = $(this);
  49. var t = p.text();
  50. newPrice = getNewPrice(t);
  51. originalPrice = getOriginalPrice(t)
  52. break;
  53. }
  54. });
  55. var priceDiff = originalPrice - newPrice;
  56. var rate = priceDiff / originalPrice * 100;
  57. p.append('<span style="border-left: 1px solid #ccc;">&nbsp;&nbsp;差价:' + priceDiff.toFixed(4) + '万元;跌幅:' + rate.toFixed(2) + '%;年限:' + months + '月;折合:' + (rate / (months / 12)).toFixed(2) + '%/年;' + (priceDiff / months * 10000).toFixed(2) + '元/月;' + (priceDiff / km * 10000).toFixed(2) + '元/公里</span>');
  58. } catch (e) {
  59. console.log(e);
  60. }
  61. });
  62. };
  63. calculate();
  64. })($);