Skip to content

Commit 52857c5

Browse files
committed
Update Bing Search to use Bing APIs not Cognitive Services
Signed-off-by: James Hunt <[email protected]>
1 parent ee45ad0 commit 52857c5

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

assets/scss/_custom.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,16 @@ div.alert > em.javascript-required {
997997
#bing-results-container {
998998
padding: 1em;
999999
}
1000+
.bing-result {
1001+
margin-bottom: 1em;
1002+
}
1003+
.bing-result-url {
1004+
font-size: 14px;
1005+
}
1006+
.bing-result-snippet {
1007+
color: #666666;
1008+
font-size: 14px;
1009+
}
10001010
#bing-pagination-container {
10011011
padding: 1em;
10021012
margin-bottom: 1em;

layouts/_default/search.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</form>
2525
</div>
2626
<div class="col-12 col-md-8 offset-md-2">
27-
<h2 class="ml-4">{{ .Title }}</h2>
27+
<h2 class="search-title ml-3">{{ .Title }}</h2>
2828
{{ if .Site.Params.gcs_engine_id }}
2929
<script>
3030
(function() {

static/js/search.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
document.querySelector('html').classList.add('search');
22

3+
document.addEventListener('DOMContentLoaded', function() {
4+
let searchTerm = new URLSearchParams(window.location.search).get('q');
5+
let fetchingElem = document.getElementById('bing-results-container');
6+
let searchTitle = document.querySelector('.search-title');
7+
8+
if (!searchTerm) {
9+
if (fetchingElem) fetchingElem.style.display = 'none';
10+
if (searchTitle) searchTitle.style.display = 'none';
11+
12+
}
13+
});
14+
315
window.renderGoogleSearchResults = () => {
416
var cx = '013288817511911618469:elfqqbqldzg';
517
var gcse = document.createElement('script');
@@ -33,30 +45,36 @@
3345
}
3446

3547
window.renderBingSearchResults = () => {
36-
var searchTerm = window.location.search.split("=")[1].split("&")[0].replace(/%20/g,' '),
37-
page = window.location.search.split("=")[2],
38-
q = "site:kubernetes.io " + searchTerm;
48+
let urlParams = new URLSearchParams(window.location.search);
49+
let searchTerm = urlParams.get("q") || "";
50+
let page = urlParams.get("page") || 1;
51+
let q = searchTerm;
52+
let results = '';
53+
let offset = (page - 1) * 10;
54+
let ajaxConf = {};
3955

40-
page = (!page) ? 1 : page.split("&")[0];
56+
if (!searchTerm) return;
4157

42-
var results = '', pagination = '', offset = (page - 1) * 10, ajaxConf = {};
58+
ajaxConf.url = 'https://kubernetes-io-search.azurewebsites.net/api/bingsearchproxy';
59+
ajaxConf.data = { q: q, offset: offset };
60+
ajaxConf.type = "GET";
4361

44-
ajaxConf.url = 'https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search';
45-
ajaxConf.data = { q: q, offset: offset, customConfig: '320659264' };
46-
ajaxConf.type = "GET";
47-
ajaxConf.beforeSend = function(xhr){ xhr.setRequestHeader('Ocp-Apim-Subscription-Key', '51efd23677624e04b4abe921225ea7ec'); };
62+
$.ajax(ajaxConf).done(function(res) {
63+
if (res.status === 500) {
64+
console.log('Server Error');
65+
return;
66+
}
4867

49-
$.ajax(ajaxConf).done(function(res) {
50-
if (res.webPages == null) return; // If no result, 'webPages' is 'undefined'
51-
var paginationAnchors = window.getPaginationAnchors(Math.ceil(res.webPages.totalEstimatedMatches / 10));
52-
res.webPages.value.map(ob => { results += window.getResultMarkupString(ob); })
68+
if (res.webPages == null) return; // If no result, 'webPages' is 'undefined'
69+
var paginationAnchors = window.getPaginationAnchors(Math.ceil(res.webPages.totalEstimatedMatches / 10));
70+
res.webPages.value.map(ob => { results += window.getResultMarkupString(ob); })
5371

54-
if($('#bing-results-container').length > 0) $('#bing-results-container').html(results);
55-
if($('#bing-pagination-container').length > 0) $('#bing-pagination-container').html(paginationAnchors);
56-
});
72+
if($('#bing-results-container').length > 0) $('#bing-results-container').html(results);
73+
if($('#bing-pagination-container').length > 0) $('#bing-pagination-container').html(paginationAnchors);
74+
});
5775
}
5876

59-
//China Verification
77+
// China Verification.
6078
var path = "path=/;"
6179
d = new Date()
6280
d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000))

0 commit comments

Comments
 (0)