-
Notifications
You must be signed in to change notification settings - Fork 4
/
jquery.bingsearch.js
155 lines (139 loc) · 6.69 KB
/
jquery.bingsearch.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
/*
* The MIT License (MIT)
* Copyright (c) 2013 Chris Benard
* http://github.com/cbenard/jquery-bingsearch
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/*! jQuery-bingsearch | (c) 2013 Chris Benard | http://github.com/cbenard/jquery-bingsearch#license */
/*jshint laxbreak: true, forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, unused:true, curly:true, browser:true, jquery:true, indent:4, maxerr:50, devel:true, nomen:false, onevar:false, white:false */
(function($) {
"use strict";
$.bingSearch = function(options) {
var defaultUrlBase = 'https://api.datamarket.azure.com/Bing/Search/v1/Web';
var settings = $.extend({
pageNumber: 1,
pageSize: 10,
debug: false,
urlBase: defaultUrlBase
}, options);
if (!settings.appKey && settings.urlBase === defaultUrlBase) {
console.error('Bing Search: Missing app key which is required if you are not overriding urlBase');
}
if (!settings.query) {
console.error('Bing Search: Missing query');
}
if (!(settings.fail
|| settings.beforeSearchResults
|| settings.searchResultIterator
|| settings.afterSearchResults)) {
console.error('Bing Search: Missing at least one result function');
}
if ((settings.appKey || settings.urlBase !== defaultUrlBase)
&& settings.query
&& (settings.fail
|| settings.beforeSearchResults
|| settings.searchResultIterator
|| settings.afterSearchResults))
{
if (settings.limitToSite) {
settings.query += ' site:' + settings.limitToSite;
}
var url = settings.urlBase + '?$format=json&$top='
+ parseInt(settings.pageSize, 10)
+ '&$Skip=' + (parseInt(settings.pageSize, 10) * (settings.pageNumber - 1))
+ "&Query='" + settings.query + "'";
var authHeader = "Basic " + $.base64.encode(settings.appKey + ":" + settings.appKey);
if (settings.debug) {
console.log('Bing Search: Using URL: ' + url);
console.log('Bing Search: Sending Authorization header: ' + authHeader);
}
// Allow cross-site
$.support.cors = true;
$.ajax({
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", authHeader);
},
url: url,
error: function(xhr, statusText, ex) {
if (settings.fail) {
settings.fail(ex.message);
}
else {
console.error(ex.message);
}
},
dataType: 'json',
success: function(data) {
if (!data || !data.d || !data.d.results) {
var errorMessage = "Bing Search: Data was empty or didn't contain results.";
if (settings.fail) {
settings.fail(errorMessage);
}
else {
console.error(errorMessage);
}
return;
}
var beforeClientData = {
hasMore: data.d.__next !== undefined,
resultBatchCount: data.d.results.length
};
if (settings.beforeSearchResults) {
settings.beforeSearchResults(beforeClientData);
}
if (settings.searchResultIterator) {
for (var i = 0; i < data.d.results.length; i++) {
var cur = data.d.results[i];
var curMetadata = null;
if (cur.__metadata && cur.__metadata.type) {
curMetadata = { 'ResultType': cur.__metadata.type };
}
// Map to DTO in case Bing changes the API
var curClient = {
'ID': cur.ID,
'Title': cur.Title,
'Description': cur.Description,
'Url': cur.Url,
'DisplayUrl': cur.DisplayUrl,
'Metadata': curMetadata
};
if (settings.debug) {
console.log("Bing Search: Result: \nID: " + (curClient.ID || 'null')
+ "\nTitle: " + (curClient.Title || 'null')
+ "\nDescription: " + (curClient.Description || 'null')
+ "\nUrl: " + (curClient.Url || 'null')
+ "\nDisplayUrl: " + (curClient.DisplayUrl || 'null')
+ "\nMetadata.ResultType: " + (curClient.Metadata && curClient.Metadata.ResultType ? curClient.Metadata.ResultType : 'null'));
}
settings.searchResultIterator(curClient);
}
}
if (settings.afterSearchResults) {
settings.afterSearchResults(beforeClientData);
}
}
});
}
return this;
};
}( jQuery ));