-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.html
More file actions
83 lines (76 loc) · 1.84 KB
/
background.html
File metadata and controls
83 lines (76 loc) · 1.84 KB
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
<html>
<body>
<script src="jquery.js"></script>
<script>
function onRequest(request, sender, callback) {
if (request.action == 'searchPrice') {
search(request.text, request.pId, callback);
}
}
chrome.extension.onRequest.addListener(onRequest);
function search(text, pId, cb) {
//console.log("search", arguments);
var a = {};
a.text = text;
a.url = "http://www.google.com/search?btnI=I'm Feeling Lucky&q=" + text
a.pId = pId;
a.cb = cb;
getUrl("http://www.google.com/search", {
/*btnI: "I'm Feeling Lucky",*/
q: text
}, bind(a, getPrice)
);
}
function getUrl(url, data, cb)
{
$.get(url, data, cb);
}
function bind(scope, fn) {
return function() {
fn.apply(scope, arguments);
};
}
function getPrice(data) {
if(data.indexOf(" - Google Search") !== -1)
{
var a = {};
var urls = $(".l", data);
urls = urls.filter(function() {
var newa = $("<a>").attr("href", $(this).attr("href"));
return newa[0].pathname.length > 1;
});
a.url = urls.first().attr('href');
a.text = this.text;
a.pId = this.pId;
a.cb = this.cb;
getUrl(a.url, {}, bind(a, getPrice));
}
else
{
data = data.substring(data.search(/<.*body/i));
var res = data.match(/^(?!\u00a2) \p{Sc}? (?!0,?\d) (\d{1,3} (\,\d{3})* |(\d+)) (\.\d{2})?$/g);
if(res && res.length) {
listPrice(res, this.text, this.url, this.pId, this.cb);
} else {
res = data.match(/\d+\.\d{2}/g);
if(res && res.length) {
listPrice(res, this.text, this.url, this.pId, this.cb);
}
}
}
}
function listPrice(res, text, url, pId, cb) {
var arr = $.grep(res, function(n, i){
n = n.replace(/[^0-9\.]+/g,"");
return (parseFloat(n) > 0 && parseFloat(n) < 200);
});
if(arr.length)
{
//console.log("Possible prices for " + text, arr, url, pId, cb);
var data = {pId: pId, url: url, price: arr[0]};
cb(data);
}
}
</script>
</body>
</html>