-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheBay - Show Option Prices.user.js
223 lines (189 loc) · 9.06 KB
/
eBay - Show Option Prices.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// ==UserScript==
// @name eBay - Show Option Prices
// @namespace http://www.toraboka.com/~mrudat
// @description Inserts prices for all the extra options that may be included in the eBay page.
// @include http://*.ebay.tld/*sch/*
// @include http://*.ebay.tld/*i.html?*
// @include http://*.ebay.tld/itm/*
// @version 0.0.4
// ==/UserScript==
/* jshint esnext: true */
process();
function process() {
var currency = '\$';
var price = /\$([\d\,]*\.\d\d)/; // regexp to test for currency
var tld = location.host.split('.').reverse()[0];
switch (tld) {
case 'uk':
currency = '£';
price = /£([\d\,]*.\d\d)/;
break;
}
var itemPage = /^\/itm\//.test(location.pathname);
if (itemPage) {
var chosenVariation = document.querySelector('span#sel-msku-variation');
if (chosenVariation === null) {
return;
}
var shippingCost = document.querySelector('span#fshippingCost');
var shippingCostConverted = document.querySelector('span#convetedPriceId');
var shippingCostText = null;
if (shippingCostConverted !== null) {
shippingCostText = shippingCostConverted.textContent;
} else if (shippingCost !== null) {
shippingCostText = shippingCost.textContent;
}
var shippingPrice = -1;
if (/Free/i.test (shippingCostText) || (/Digital delivery/.test(shippingCostText))) {
shippingPrice = 0;
} else if (/Not specified/.test(shippingCostText)) {
shippingPrice = -1;
} else if (price.test(shippingCostText)){
shippingPrice = shippingCostText.match(price)[1].replace(',','');
}
var variationsPanel = chosenVariation.parentNode;
var request = new XMLHttpRequest();
request.open("GET", location.href);
request.onloadend = function(){
var rdata = request.responseText;
var varipoint = rdata.indexOf('"itmVarModel":{');
if (varipoint == -1){
return;
}
var varidata = rdata.substring(varipoint);
varidata = '{' + varidata.substring(0, varidata.indexOf('"unavailableVariationIds"')-1) + '}}';
var itmVarModel = JSON.parse(varidata).itmVarModel;
var menuModels = itmVarModel.menuModels;
var menuItemMap = itmVarModel.menuItemMap;
var itemVariations = Object.keys(itmVarModel.itemVariationsMap).map(k => itmVarModel.itemVariationsMap[k]);
itemVariations = itemVariations
.map(itemVariation => {
var buyItNowPrice = itemVariation.convertedPrice || itemVariation.price;
buyItNowPrice = buyItNowPrice.match(price)[1].replace(',','');
return [buyItNowPrice, itemVariation];
})
.sort((a,b) => a[0] - b[0])
.map(i => i[1]);
var finaldata = "<table><thead><tr>";
menuModels.forEach(menuModel => {
finaldata += "<th>" + menuModel.displayName + "</th>";
});
finaldata += "<th>Price</th></tr></thead><tbody>";
itemVariations.forEach(itemVariation => {
finaldata += "<tr>";
var traitValuesMap = itemVariation.traitValuesMap;
menuModels.forEach(menuModel => {
finaldata += "<td>" + menuItemMap[traitValuesMap[menuModel.name]].displayName + "</td>";
});
var buyItNowPrice = itemVariation.convertedPrice || itemVariation.price;
var priceCurrency = buyItNowPrice.substring(0, buyItNowPrice.indexOf(currency) + 1);
buyItNowPrice = buyItNowPrice.substring(buyItNowPrice.indexOf(currency) + 1);
finaldata += "<td>" + priceCurrency;
if (shippingPrice == -1) {
finaldata += buyItNowPrice + " + ?";
} else {
buyItNowPrice = (parseFloat(buyItNowPrice) + parseFloat(shippingPrice)).toFixed(2);
finaldata += buyItNowPrice;
}
finaldata += "</td></tr>";
});
finaldata += "</tbody></table>";
var el = document.createElement('div');
el.class = "vi-msku-cntr";
el.innerHTML = finaldata;
variationsPanel.appendChild(el);
};
request.send();
} else {
Array.prototype.forEach.call(document.querySelectorAll('li[listingid]'),rowElement => {
var shippingPrice = -1;
var lvprices = rowElement.querySelector('ul.lvprices');
var shipping = lvprices.querySelector('span.fee') || lvprices.querySelector("span.bfsp");
if (shipping !== null) {
var tc = shipping.textContent;
if (/Free/i.test (tc) || (/Digital delivery/.test(tc))) {
shippingPrice = 0;
} else if (/Not specified/.test(tc)) {
shippingPrice = -1;
} else if (price.test(tc)){
shippingPrice = tc.match(price)[1].replace(',','');
}
}
var buyItNow = lvprices.querySelector('li.lvprice span.bold') || lvprices.querySelector('li.lvprice');
if (buyItNow === null) {
return;
}
if (buyItNow.textContent.indexOf(" to ") == -1) {
// doesn't have variations
return;
}
var lvdetails = rowElement.querySelector('ul.lvdetails');
if (lvdetails === null) {
// couldn't find element to stick table into
return;
}
var request = new XMLHttpRequest();
var productUrl = rowElement.getElementsByTagName('a')[0].href;
request.open("GET", productUrl);
request.onloadend = function(){
var rdata = request.responseText;
var varipoint = rdata.indexOf('"itmVarModel":{');
if (varipoint == -1){
return;
}
var varidata = rdata.substring(varipoint);
varidata = '{' + varidata.substring(0, varidata.indexOf('"unavailableVariationIds"')-1) + '}}';
var itmVarModel = JSON.parse(varidata).itmVarModel;
var menuModels = itmVarModel.menuModels;
var menuItemMap = itmVarModel.menuItemMap;
var itemVariations = Object.keys(itmVarModel.itemVariationsMap).map(k => itmVarModel.itemVariationsMap[k]);
itemVariations = itemVariations
.filter(i => i.quantityAvailable >= 1)
.map(itemVariation => {
var convertedPrice = itemVariation.convertedPrice || itemVariation.price;
convertedPrice = convertedPrice.match(price)[1].replace(',','');
return [convertedPrice, itemVariation];
})
.sort((a,b) => a[0] - b[0])
.map(i => i[1]);
var truncated = false;
if (itemVariations.length > 10) {
itemVariations.length = 10;
truncated = true;
}
var finaldata = "<table><thead><tr>";
menuModels.forEach(menuModel => {
finaldata += "<th>" + menuModel.displayName + "</th>";
});
finaldata += "<th>Price</th></tr></thead><tbody>";
itemVariations.forEach(itemVariation => {
finaldata += "<tr>";
var traitValuesMap = itemVariation.traitValuesMap;
menuModels.forEach(menuModel => {
finaldata += "<td>" + menuItemMap[traitValuesMap[menuModel.name]].displayName + "</td>";
});
var convertedPrice = itemVariation.convertedPrice || itemVariation.price;
var priceCurrency = convertedPrice.substring(0, convertedPrice.indexOf(currency) + 1);
convertedPrice = convertedPrice.substring(convertedPrice.indexOf(currency) + 1);
var buyItNowPrice = convertedPrice;
finaldata += "<td>" + priceCurrency;
if (shippingPrice == -1) {
finaldata += buyItNowPrice + " + ?";
} else {
buyItNowPrice = (parseFloat(buyItNowPrice) + parseFloat(shippingPrice)).toFixed(2);
finaldata += buyItNowPrice;
}
finaldata += "</td></tr>";
});
if (truncated) {
finaldata += '<tr><td colspan="' + (menuModels.length + 1) + '">...</td></tr>';
}
finaldata += "</tbody></table>";
var el = document.createElement('li');
el.innerHTML = finaldata;
lvdetails.appendChild(el);
};
request.send();
});
}
}