eBay - Display Totals with Shipping Redux

Computes and displays the total price with shipping added.

  1. // ==UserScript==
  2. // @name eBay - Display Totals with Shipping Redux
  3. // @namespace http://www.toraboka.com/~mrudat , MrBrax
  4. // @description Computes and displays the total price with shipping added.
  5. // @include http://*.ebay.tld/*sch/*
  6. // @include http://*.ebay.tld/*i.html?*
  7. // @include http://*.ebay.tld/itm/*
  8. // @version 0.0.6
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. /* jshint esnext: true */
  13.  
  14. process();
  15.  
  16. function process() {
  17.  
  18. var countryText = {
  19. uk: 'United Kingdom'
  20. };
  21.  
  22. var taxCountries = ['China', 'Hong Kong', 'Japan', 'Malaysia'];
  23. var currency = '$';
  24. var price = /\$([\d\,]*\.\d\d)/; // regexp to test for currency
  25. var tld = location.host.split('.').reverse()[0];
  26. switch (tld) {
  27. case 'uk':
  28. currency = '£';
  29. price = /£([\d\,]*.\d\d)/;
  30. break;
  31. }
  32.  
  33. var itemPage = /^\/itm\//.test(location.pathname);
  34.  
  35. if (itemPage) {
  36. var buyItNowPrice = -1;
  37. var shippingPrice = -1;
  38.  
  39. //TODO display table of total price for 1 .. min(10,available) of item (presuming shipping cost differs)
  40. //FIXME only possible if we can work out conversion rate for extra shipping; possibly not worthwhile?
  41. //var priceTableTarget = document.querySelector('div.quantity').parentNode;
  42. //var shippingTable = document.querySelector('table.sh-tbl');
  43. var priceSummary = document.querySelector('span#prcIsum');
  44. var priceSummaryConverted = document.querySelector('span#convbinPrice');
  45. var priceSummaryConvertedContainer = document.querySelector('span#prcIsumConv');
  46.  
  47. var priceSummaryText = null;
  48. if (priceSummaryConverted !== null) {
  49. priceSummaryText = priceSummaryConverted.textContent;
  50. priceSummaryConverted.parentNode;
  51. } else if (priceSummary !== null) {
  52. priceSummaryText = priceSummary.textContent;
  53. }
  54. var priceSummaryCurrency = "";
  55. if (priceSummaryText !== null) {
  56. priceSummaryCurrency = priceSummaryText.substring(0, priceSummaryText.indexOf(currency) + 1);
  57. buyItNowPrice = priceSummaryText.match(price)[1].replace(',','');
  58. }
  59. var shippingCost = document.querySelector('span#fshippingCost');
  60. var shippingCostConverted = document.querySelector('span#convetedPriceId');
  61.  
  62. var shippingCostText = null;
  63. if (shippingCostConverted !== null) {
  64. shippingCostText = shippingCostConverted.textContent;
  65. } else if (shippingCost != null) {
  66. shippingCostText = shippingCost.textContent;
  67. }
  68. if (shippingCostText !== null) {
  69. if (/Free/.test (shippingCostText) || (/Digital delivery/.test(shippingCostText))) {
  70. shippingPrice = 0;
  71. } else if (/Not specified/.test(shippingCostText)) {
  72. shippingPrice = '?';
  73. } else if (price.test(shippingCostText)){
  74. shippingPrice = shippingCostText.match(price)[1].replace(',','');
  75. }
  76. }
  77.  
  78. if (buyItNowPrice != -1 && shippingPrice != -1) {
  79. var buyItNowTotal = "?";
  80. if (!isNaN(buyItNowPrice) && !isNaN(shippingPrice)){
  81. buyItNowTotal = (parseFloat(buyItNowPrice) + parseFloat(shippingPrice)).toFixed(2);
  82. }
  83. if (priceSummaryConverted !== null){
  84. priceSummaryConverted.parentNode.parentNode.removeChild(priceSummaryConverted.parentNode);
  85. }
  86. priceSummary.innerHTML = '<span title="With shipping" style="cursor:help;">' + priceSummaryCurrency + buyItNowTotal + '</span> <small title="Without shipping" style="font-size:80%; color: #666; cursor:help;">(<strong>' + priceSummaryCurrency + buyItNowPrice + '</strong>+' + priceSummaryCurrency + shippingPrice + ')</small>';
  87. }
  88.  
  89. } else {
  90. Array.prototype.forEach.call(document.querySelectorAll('li[listingid]'), rowElement => {
  91. var buyItNowPrice = -1;
  92. var shippingPrice = -1;
  93.  
  94. var lvprices = rowElement.querySelector('ul.lvprices');
  95.  
  96. // TODO what is this for?
  97. Array.prototype.forEach.call(lvprices.querySelectorAll('div.cmpat'),i => i.parentNode.removeChild(i));
  98.  
  99. var shipping = lvprices.querySelector('span.fee');
  100.  
  101. if (shipping !== null) {
  102. var tc = shipping.textContent;
  103. if (/Free/.test (tc) || (/Digital delivery/.test(tc))) {
  104. shippingPrice = 0;
  105. } else if (/Not specified/.test(tc)) {
  106. shippingPrice = '?';
  107. } else if (price.test(tc)){
  108. shippingPrice = tc.match(price)[1].replace(',','');
  109. }
  110. }
  111.  
  112. var buyItNow = lvprices.querySelector('li.lvprice');
  113.  
  114. var priceSummaryCurrency = "";
  115. if (buyItNow !== null) {
  116. var tc = buyItNow.textContent.trim();
  117. priceSummaryCurrency = tc.substring(0, tc.indexOf(currency) + 1).trim();
  118. buyItNowPrice = tc.match(price)[1].replace(',','');
  119. }
  120.  
  121. if (buyItNowPrice != -1 && shippingPrice != -1) {
  122. var buyItNowTotal = "?";
  123. if (!isNaN(buyItNowPrice) && !isNaN(shippingPrice)){
  124. buyItNowTotal = (parseFloat(buyItNowPrice) + parseFloat(shippingPrice)).toFixed(2);
  125. }
  126. buyItNow.innerHTML = buyItNow.innerHTML.substring(0, buyItNow.innerHTML.indexOf('</b>') + 4);
  127. buyItNow.innerHTML += '<span title="Total cost" class="bold" style="cursor:help;">' + priceSummaryCurrency + buyItNowTotal + '</span><br><small style="font-size:80%; color: #666; cursor:help;">(<strong title="Item price">' + priceSummaryCurrency + buyItNowPrice + '</strong>+<span title="Shipping">' + priceSummaryCurrency + shippingPrice + '</span>)</small>';
  128.  
  129. shipping.innerHTML = '';
  130.  
  131. }
  132.  
  133. var hasLocation = false;
  134.  
  135. var itemLocation = null;
  136.  
  137. var sellerIndex = -1;
  138.  
  139. var detailLines = rowElement.querySelectorAll('ul.lvdetails li');
  140. for(var i = 0; i < detailLines.length; i++){
  141. if( detailLines[i].innerText.trim().substr(0,4) == 'From' ){
  142. hasLocation = true;
  143. itemLocation = detailLines[i].innerText.trim().substr(5);
  144. }
  145.  
  146. if( detailLines[i].innerText.trim().substr(0,6) == 'Seller' ){
  147. sellerIndex = i;
  148. if(hasLocation) break;
  149. }
  150.  
  151. }
  152.  
  153. if(!hasLocation && sellerIndex != -1){
  154. var locLi = document.createElement('li');
  155. locLi.innerHTML = 'From ' + ( countryText[tld] ? countryText[tld] : tld ) + ' (probably)';
  156. detailLines[0].parentNode.insertBefore( locLi, detailLines[sellerIndex] );
  157. }
  158.  
  159. if( taxCountries.indexOf(itemLocation) !== -1 ){
  160. var addedCost = 0;
  161.  
  162. var cp = 0;
  163. var c1 = 0;
  164. var c2 = 0;
  165.  
  166. if (buyItNowPrice != -1) {
  167.  
  168. if(shippingPrice != -1) c2 = parseFloat(shippingPrice);
  169.  
  170. cp = parseFloat( buyItNowPrice );
  171. addedCost += cp + c2;
  172.  
  173. if(cp + c2 > 130){
  174. c1 = ( cp + c2 ) * 0.10; // toll
  175. addedCost += c1;
  176. }
  177. addedCost += ( cp + c2 + c1 ) * 0.25; // moms
  178.  
  179. addedCost += 13 // administrative;
  180.  
  181. }else{
  182.  
  183. }
  184.  
  185. buyItNow.innerHTML += ' <small title="With swedish fees" style="color:' + ( addedCost < 50 ? '#308927' : '#C12828' ) + '; cursor: help;">(~' + priceSummaryCurrency + Math.round(addedCost) + ')</small>';
  186.  
  187. if(addedCost >= 50){
  188. rowElement.style.backgroundColor = '#fff0f0';
  189. }
  190.  
  191. }
  192.  
  193. });
  194. }
  195. }