This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
search-script.js
96 lines (81 loc) · 2.48 KB
/
search-script.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
var _prevIndex = 0;
var _nextIndex = 0;
var _resultsPerPage = 10;
var _pageNumber = 1;
$(function ()
{
$('#btnSearch').show().click(function () { Search($("#txtSearchTerm").val(),0);});
$('#lnkPrev').click(function () { Search($("#txtSearchTerm").val(),-1); });
$('#lnkNext').click(function () { Search($("#txtSearchTerm").val(),1); });
});
function Search(term, direction)
{
var startIndex = 1;
if (direction === -1)
{
startIndex = _prevIndex;
_pageNumber--;
}
if (direction === 1)
{
startIndex = _nextIndex;
_pageNumber++;
}
if (direction === 0)
{
startIndex = 1;
_pageNumber = 1;
}
var url = "https://www.googleapis.com/customsearch/v1?key="
+ mGoogleApiKey + "&num=10&cx=" + mGoogleCustomSearchKey + "&start=" + startIndex + "&q=" + escape(term) + "&callback=?";
// url = "http://hahndorf/ws/dummy.aspx?q=" + escape(term) + "&start=" + startIndex + "&callback=?";
$.getJSON(url, '', SearchCompleted);
}
function SearchCompleted(response)
{
var html = "";
$("#searchResult").html("");
if (response.items == null)
{
$("#searchResult").html("No matching pages found");
return;
}
if (response.items.length === 0)
{
$("#searchResult").html("No matching pages found");
return;
}
$("#searchResult").html(response.queries.request[0].totalResults + " pages found for <b>" +response.queries.request[0].searchTerms+ "</b>");
if (response.queries.nextPage != null)
{
_nextIndex = response.queries.nextPage[0].startIndex;
$("#lnkNext").show();
}
else
{
$("#lnkNext").hide();
}
if (response.queries.previousPage != null)
{
_prevIndex = response.queries.previousPage[0].startIndex;
$("#lnkPrev").show();
}
else
{
$("#lnkPrev").hide();
}
if (response.queries.request[0].totalResults > _resultsPerPage){
$("#lblPageNumber").show().html(_pageNumber);
}
else{
$("#lblPageNumber").hide();
}
for (var i = 0; i < response.items.length; i++){
var item = response.items[i];
var title = item.htmlTitle;
html += "<p><a class='searchLink' href='" + item.link + "'> " + title + "</a><br>";
html += item.snippet + "<br>";
html += item.link + " - <a href='http://www.google.com/search?q=cache:"+item.cacheId+":"+item.displayLink+"'>Cached</a></p>";
}
$("#output").html(html);
}