eBay sort by max price

Sort the items by the to price instead of the from price because sellers use this to put their items at the head of the list even when the real item is more expensive.

  1. // ==UserScript==
  2. // @name eBay sort by max price
  3. // @description Sort the items by the to price instead of the from price because sellers use this to put their items at the head of the list even when the real item is more expensive.
  4. // @namespace kwhitefoot@hotmail.com
  5. // @include http://www.ebay.com/sch/*
  6. // @version 1.1
  7. // @resource license https://www.gnu.org/licenses
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /*
  12. Tested only on http://www.ebay.com/sch/*
  13.  
  14. Basic idea shamelessly ripped from the eBay Collection Sorter:
  15. https://greasyfork.org/en/scripts/6120-ebay-collection-sorter by
  16. Tophness: https://greasyfork.org/en/users/5680-tophness.
  17.  
  18. kwhitefoot@hotmail.com
  19.  
  20. Copyright 2017 Kevin Whitefoot
  21.  
  22. This program is free software: you can redistribute it and/or modify
  23. it under the terms of the GNU General Public License as published by
  24. the Free Software Foundation, either version 3 of the License, or
  25. (at your option) any later version.
  26.  
  27. This program is distributed in the hope that it will be useful,
  28. but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. GNU General Public License for more details.
  31.  
  32. You should have received a copy of the GNU General Public License
  33. along with this program. If not, see <http://www.gnu.org/licenses/>.
  34. */
  35.  
  36.  
  37. var debug = true;
  38. var TAG = 'ebsbmp: ';
  39. function dlog(msg) {
  40. if (debug) console.log(TAG + msg);
  41. }
  42.  
  43. dlog('start');
  44.  
  45. try {
  46. // var prevY = 0;
  47. // windowscroll = setInterval(doscroll, 800);
  48. // function doscroll() {
  49. // if (window.scrollMaxY !== prevY) {
  50. // prevY = window.scrollMaxY;
  51. // window.scrollTo(0, window.scrollMaxY);
  52. // } else {
  53. // clearInterval(windowscroll);
  54. // window.scrollTo(0, 0);
  55. // process();
  56. // }
  57. // }
  58.  
  59. process();
  60.  
  61. // Get the item with class="sresult lvresult clearfix li shic".
  62. // Then extract the price and shipping using
  63. // getElementsByClassName on the item.
  64. function getPrices() {
  65. dlog( 'getPrices');
  66. var prices = new Array();
  67. var items = document.getElementsByClassName("sresult lvresult clearfix li shic");
  68. for (var i = 0; i < items.length; ++i) {
  69. var item = items[i];
  70. var priceSpanParent = item.getElementsByClassName('lvprice prc')[0];
  71. var priceSpan = priceSpanParent.firstElementChild;
  72. var priceSpanText = priceSpan.innerText.trim();
  73. dlog('pst: <' + priceSpanText + '>');
  74. var re = /\s+/;
  75. var priceSpanArray = priceSpanText.trim().split(re);
  76. dlog('psa: ' + priceSpanArray.length);
  77. var priceIndex;
  78. if (priceSpanArray.length == 5) {
  79. priceIndex = 4;
  80. } else {
  81. priceIndex = 1;
  82. }
  83. dlog('pi: ' + priceIndex);
  84. var price = getNumber(priceSpanArray[priceIndex]);
  85. dlog('price: ' + price);
  86. //var itemElement = element.parentNode.parentNode;
  87. var descriptionElement = item.getElementsByClassName('vip');
  88. var priceShipping = 0.0;
  89. var priceShippingElements = item.getElementsByClassName('fee');
  90. if (priceShippingElements.length > 0) {
  91. var priceShippingElement = priceShippingElements[0];
  92. var priceShippingText = priceShippingElement.innerText.trim();
  93. var priceShippingArray = priceShippingText.split(re);
  94. priceShipping = getNumber(priceShippingArray[1]);
  95. dlog('priceShipping: ' + priceShipping);
  96. }
  97. prices.push(new Price(price,
  98. priceShipping,
  99. descriptionElement.textContent,
  100. item));
  101.  
  102. }
  103. return prices;
  104. }
  105.  
  106. // Expects a price with a decimal point. Copes with prices that
  107. // include a thousands separator.
  108. function getNumber(text) {
  109. return parseFloat(text.replace(',', ''));
  110. }
  111.  
  112.  
  113. function Price(price,
  114. priceShipping,
  115. description,
  116. item) {
  117. this.priceItem = price;
  118. this.priceShipping = priceShipping;
  119. this.description = description;
  120. this.listItem = item;
  121. this.price = function(withShipping) {
  122. if (withShipping) {
  123. dlog('price fun with ' + this.priceItem + ' ' + this.priceShipping);
  124. return this.priceItem + this.priceShipping;
  125. } else {
  126. dlog('price fun without ' + this.priceItem);
  127. return this.priceItem;
  128. }
  129. }
  130. }
  131.  
  132.  
  133. // sortType determines whether the sort will be asscending or
  134. // descending by multiplying the comparison result by plus of
  135. // minus one.
  136. function rearrange(prices,
  137. sortType) {
  138. dlog('rearrange');
  139. var order = sortType.order;
  140. var withShipping = sortType.withShipping;
  141. dlog('order: ' + order + ' type: ' + typeof order);
  142. dlog('withShipping: ' + withShipping + ' type: ' + typeof withShipping);
  143. prices.sort(function (a, b) {
  144. var result = a.price(withShipping) - b.price(withShipping);
  145. return result * order;
  146. });
  147. // var result = a.priceItem - b.priceItem;
  148. // if (result == 0) {
  149. // if (a.description < b.description) {
  150. // result = -1;
  151. // } else if (b.description < a.description) {
  152. // result = +1;
  153. // }
  154. // }
  155. // return result * order;
  156. // });
  157. // Now remove all the items from the list
  158. // var container = document.getElementById('ListViewInner');
  159. // for (var i = 0; i < prices.length; ++i) {
  160. // var item = prices[i].listItem;
  161. // dlog('remove item ' + prices[i].price(withShipping) + ' ' + item.className);
  162. // container.removeChild(item);
  163. // }
  164.  
  165. var container = document.getElementById('ListViewInner');
  166. removeItems(container);
  167. reinstate(prices, withShipping, container);
  168. }
  169.  
  170. function removeItems(container) {
  171. dlog('removeItems ' + container.id);
  172. while (container.firstChild) {
  173. container.removeChild(container.firstChild);
  174. }
  175. }
  176. function reinstate(prices, withShipping, container) {
  177. // And put them back in the correct order.
  178. dlog('reinstate');
  179. for (var j = 0; j < prices.length; j++) {
  180. var item = prices[j].listItem;
  181. dlog('reinstate item ' + prices[j].price(withShipping) + ' ' + item.className);
  182. container.appendChild(item);
  183. }
  184. }
  185. function getSortType() {
  186. var url = document.location.search;
  187. var pricePlusShippingLowestFirst = '_sop=15';
  188. var pricePlusShippingHighestFirst = '_sop=16';
  189. var priceHighestFirst = '_sop=3';
  190. if (url.includes(pricePlusShippingLowestFirst)) {
  191. return {order: +1,
  192. withShipping: true};
  193. } else if (url.includes(pricePlusShippingHighestFirst)) {
  194. return {order: -1,
  195. withShipping: true};
  196. } else if (url.includes(priceHighestFirst)) {
  197. return {order: -1,
  198. withShipping: false};
  199. }
  200. return {order: 0,
  201. withShipping: false};
  202. }
  203.  
  204. function process() {
  205. var sortType = getSortType();
  206. dlog('sortType: ' + sortType);
  207. if (sortType.order !== 0) {
  208. var prices = getPrices();
  209. dlog("ps: " + prices.length);
  210. rearrange(prices, sortType);
  211. }
  212. }
  213. } catch (ex){
  214. dlog("error: " + ex.message);
  215. }
  216.  
  217. dlog('finish');