-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScrapeProducts.js
71 lines (62 loc) · 2.06 KB
/
ScrapeProducts.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
(function() {
'use strict';
var boxes = document.getElementsByTagName('fieldset'),
summary = {},
products = [],
icon = new Image(),
iconURL,
canvas,
i,
first,
last,
box,
product;
function stringAtPath(path) {
return document.evaluate(path, box, null, XPathResult.STRING_TYPE, null).stringValue.trim();
}
function nodeAtPath(path) {
return document.evaluate(path, box, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
for (i = 0; i < boxes.length; i++) {
box = boxes[i];
first = (i === 0);
last = (i === (boxes.length - 1));
if (first || last) {
product = summary;
} else {
product = {};
products.push(product);
}
if (!last) {
product.name = stringAtPath('.//label');
}
if (first) {
iconURL = nodeAtPath('.//div').style['background-image'];
if (iconURL) {
product.iconDataURL = iconURL.substring(4, iconURL.length - 1);
}
} else if (!last) {
iconURL = nodeAtPath('.//img').src;
icon.src = iconURL;
canvas = document.createElement('canvas');
canvas.width = icon.width;
canvas.height = icon.height;
canvas.getContext('2d').drawImage(icon, 0, 0);
product.iconDataURL = canvas.toDataURL('image/png');
product.productURL = nodeAtPath('./a').href;
if (product.productURL.indexOf('https://cydia.saurik.com/connect/products/') !== 0) {
products.pop();
continue;
}
}
if (!first) {
product.incomeRate = stringAtPath('.//label//div//div');
product.delta = stringAtPath('.//div//div//span[2]');
product.direction = (nodeAtPath('.//div//div[last()]').style.color === 'red') ? '-' : '+';
}
}
return {
summary : summary,
products : products
};
})()