-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
339 lines (281 loc) · 13.1 KB
/
popup.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var BidBuddy = {
/**
* Empty settings variable to load settings into.
*
* @public
*/
settings: {},
/**
* The current active page. Can be changed via input.
*
* @private
*/
_page: 1,
/**
* The default settings if they are not present.
*
* @private
*/
_defaults: {
query: 'Macbook',
exclude: ['case', 'sleeve', 'upgrade', 'SSD', 'screen', 'harddrive', 'LCD', 'keyboard', 'cover', 'adapter', 'superdrive', 'backpack', 'sticker', 'VGA', 'lock', 'mouse']
},
/**
* Loads settings from localStorage.
*
* @public
*/
loadSettings: function() {
// Add defaults
for (var key in this._defaults) {
if (!localStorage.hasOwnProperty(key)) {
localStorage[key] = this._defaults[key];
}
}
// Load in settings
this.settings = localStorage;
//==DEBUG
//for(var key in this.settings){ console.log('settings['+key+'] = '+this.settings[key]); }
//==/DEBUG
},
/**
* Generates a search query URL based on the settings.
*
* @param string page The page number to query.
* @return The URL for the search query.
* @private
*/
_getQueryURL: function(page, key) {
var exclusions = '-' + this.settings.exclude.split(',').join(' -'), // Clones the array with .slice(0), as to not convert it to a string.
query = this.settings.query + ' ' + exclusions,
url = 'http://svcs.ebay.com/services/search/FindingService/v1?' +
'operation-name=findItemsByKeywords&' + // Search for items by keyboard.
'global-id=EBAY-GB&' + // Use the British version of eBay
'service-version=1.0.0&' + // Default service version.
'security-appname=' + key + '&' + // The application key.
'response-data-format=JSON&' + // The data format to return results in.
'rest-payload&' + // Use REST for request
'itemFilter.name=ListingType&' + // The item filter name
'itemFilter.value=Auction&' + // Filter by Auction only
'keywords=' + encodeURIComponent(query); // Search query
return url;
},
/**
* Convenience function to pad numbers to two digits.
*
* @param int number The number to pad.
* @return A padded string.
* @private
*/
_pad: function(number) {
return parseInt(number) > 9 ? number : '0' + number;
},
/**
* Parses timestamps to a suitable format for the table.
*
* @param int timestamp The Javascript Unix timestamp
* @return A string with an HTML-formatted date.
* @private
*/
_parseTime: function(timestamp) {
var date = new Date(timestamp),
now = new Date(),
dayMeasure = 24 * 60 * 60 * 1000, // H * M * S * MS
hourMeasure = 60 * 60 * 1000, // M * S * MS
minuteMeasure = 60 * 1000, // S * MS
secondMeasure = 1000, // MS
remainingDays = Math.round(Math.abs((date.getTime() - now.getTime()) / (dayMeasure))),
remainingHours = Math.round(Math.abs((date.getTime() - now.getTime()) / (hourMeasure))),
remainingMins = Math.round(Math.abs((date.getTime() - now.getTime()) / (minuteMeasure))),
remainingSecs = Math.round(Math.abs((date.getTime() - now.getTime()) / (secondMeasure))),
remaining = '';
if (remainingDays > 1) {
remaining = remainingDays + 'd';
} else if (remainingHours > 1) {
remaining = remainingHours + 'h';
} else if (remainingMins > 0) {
remaining = remainingMins + 'm';
} else {
remaining = remainingSecs + 's';
}
var month = 'January';
switch (date.getMonth()) {
case 0: month = 'January'; break;
case 1: month = 'February'; break;
case 2: month = 'March'; break;
case 3: month = 'April'; break;
case 4: month = 'May'; break;
case 5: month = 'June'; break;
case 6: month = 'July'; break;
case 7: month = 'August'; break;
case 8: month = 'September'; break;
case 9: month = 'October'; break;
case 10: month = 'November'; break;
case 11: month = 'December'; break;
}
var day = this._pad(date.getDate()),
hours = this._pad(date.getHours()),
minutes = this._pad(date.getMinutes())
return remaining + ' remaining' +
'<span class="date">' +
month +' ' + day + ', ' + hours + ':' + minutes +
'</span>';
},
/**
* Loads the search results into the DOM. This is the callback for "getSearchResults".
*
* @param {XMLHttpRequestProgressEvent} e The event parameter passed by the XMLHttpRequest.
* @private
*/
_loadSearchResults: function(e) {
var response = e.target.response,
json = JSON.parse(response),
fragment = document.createDocumentFragment(),
container = document.getElementById('results');
if (json.findItemsByKeywordsResponse[0].hasOwnProperty('errorMessage') &&
json.findItemsByKeywordsResponse[0].errorMessage.length > 0) {
container.innerHTML = '<tr><th>Error</th></tr>' +
'<tr><td>Uh oh! An error occurred. Please put this error message in a bug report:</td></tr>' +
'<tr><td>' + json.findItemsByKeywordsResponse[0].errorMessage[0].error[0].message[0] + '</td></tr>';
return;
}
var resultsCount = json.findItemsByKeywordsResponse[0].searchResult[0]['@count'],
results = json.findItemsByKeywordsResponse[0].searchResult[0].item;
if (resultsCount < 1) {
container.innerHTML = '<tr><th>No results found</th></tr>';
return;
}
//==DEBUG
//console.log(results);
//==/DEBUG
container.innerHTML = ''; // We don't want any other items now, do we?
for (var i = 0; i < results.length; i++) {
//==DEBUG
//console.log(results[i]);
//==/DEBUG
var itemJSON = results[i],
item = {
name: itemJSON.title[0],
href: itemJSON.viewItemURL[0],
image: itemJSON.galleryURL[0],
time: itemJSON.listingInfo[0].endTime[0],
price: itemJSON.sellingStatus[0].currentPrice[0].__value__ + '<small>Postage: £' + itemJSON.shippingInfo[0].shippingServiceCost[0].__value__ + '</small>'
},
row = document.createElement('tr'),
imageDiv = document.createElement('td'), // Note: div = divider in table.
titleDiv = document.createElement('td'),
priceDiv = document.createElement('td'),
timeDiv = document.createElement('td'),
itemLink = document.createElement('a'),
image = document.createElement('img');
// <td> Tags
imageDiv.setAttribute('class', 'thumbnail');
titleDiv.setAttribute('class', 'title');
priceDiv.setAttribute('class', 'price');
timeDiv.setAttribute('class', 'time');
// <td> Without Children
priceDiv.innerHTML = '£' + item.price;
timeDiv.innerHTML = this._parseTime(item.time);
// <td> With Children
image.src = item.image;
itemLink.href = item.href;
itemLink.textContent = item.name;
// Append <td> Children
titleDiv.appendChild(itemLink);
imageDiv.appendChild(image);
// Append <td>s to <tr>
row.appendChild(imageDiv);
row.appendChild(titleDiv);
row.appendChild(priceDiv);
row.appendChild(timeDiv);
fragment.appendChild(row);
}
container.appendChild(fragment);
},
/**
* Gets eBay page content from a search query built on the settings.
*
* @public
*/
getSearchResults: function(page) {
var keyRequest = new XMLHttpRequest();
keyRequest.onload = function(e) {
var request = new XMLHttpRequest();
request.onload = this._loadSearchResults.bind(this);
request.open('GET', this._getQueryURL(page, e.target.response), true);
request.send(null);
this._page = page;
}.bind(this);
keyRequest.open('GET', chrome.extension.getURL('/appkey.dat'), true);
keyRequest.send(null);
},
/**
* Registers all button event handlers related to BidBuddy.
*
* @public
*/
initButtons: function() {
var elements = document.querySelectorAll('a,button'),
buttons = [];
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.hasAttribute('data-action') && element.getAttribute('data-action').substring(0, 3) === 'lb:') {
var actionAttribute = element.getAttribute('data-action'),
actionString = actionAttribute.substring(3),
action = actionString.indexOf(':') > 0 ?
actionString.substring(0, actionString.indexOf(':')).toLowerCase() :
'',
actionProperty = actionString.substring(actionString.lastIndexOf(':') + 1).toLowerCase();
//==DEBUG
//console.log('(iteration) ' + actionAttribute + ', ' + actionString + ', ' + action + ', ' + actionProperty);
//==/DEBUG
if (action === 'save' && actionProperty.length > 0) {
if (document.getElementById(actionProperty) && this.settings.hasOwnProperty(actionProperty))
document.getElementById(actionProperty).value = this.settings[actionProperty];
element.addEventListener('click', function(e) {
//==DEBUG
//('(click) ' + this.actionAttribute + ', ' + this.actionString + ', ' + this.action + ', ' + this.actionProperty);
//==/DEBUG
// Save the setting
localStorage[this.actionProperty] = document.getElementById(this.actionProperty) ?
document.getElementById(this.actionProperty).value :
this._defaults.hasOwnProperty(this.actionProperty) ?
this._defaults[this.actionProperty] :
null;
this.self.loadSettings();
// Change the text to "Saved!" for 2 seconds.
var oldContent = e.target.innerHTML;
e.target.textContent = 'Saved!';
setTimeout(function() {
e.target.innerHTML = oldContent;
}, 2000);
// Reload search results
this.self.getSearchResults(1);
}.bind({ // Prevent using the last iteration
self: this,
actionProperty: actionProperty,
actionString: actionString,
action: action,
actionAttribute: actionAttribute
}));
} else if (action === 'page' && actionProperty.length > 0) {
element.addEventListener('click', function(e) {
this.self.getSearchResults(this.actionProperty == 'prev' ?
(this.self._page < 1 ? 0 : this.self._page - 1) :
(this.self._page + 1));
document.getElementById('page').textContent = this.self._page;
}.bind({ // Prevent using the last iteration
self: this,
actionProperty: actionProperty,
actionString: actionString,
action: action,
actionAttribute: actionAttribute
}));
}
}
}
}
};
BidBuddy.loadSettings();
BidBuddy.initButtons();
BidBuddy.getSearchResults(1);