Bing search for China users (#9845)
* Updates readme's site building instructions * Alters dockerfile to use extended hugo build This is necessary for the pipes used w/ TOCSS since node-sass was dropped * WIP for bing search implementation * Implements Bing search instead of google * Makes bing the default result provider and uses google and it's available * Adds pagination to bing and replaces the $.get call with a $.ajax call * front adjust * bing limit pagination * Adds break if there are less than 10 pages worth of results on bing search * Adds proper google script ref and proper config id * Removes console.log callpull/9882/head
parent
a2c3918a70
commit
fcc03c3474
|
@ -1775,3 +1775,37 @@ $feature-box-div-margin-bottom: 40px
|
|||
|
||||
.gsc-above-wrapper-area
|
||||
border-bottom: 0;
|
||||
|
||||
|
||||
|
||||
/* Bing Search */
|
||||
#bing-results-container
|
||||
margin-top: 30px
|
||||
margin-left: 20px
|
||||
|
||||
.bing-result
|
||||
margin-bottom: 20px
|
||||
|
||||
.bing-result-name a
|
||||
font-size: 16px
|
||||
color: #0000CC
|
||||
|
||||
|
||||
.bing-result-url
|
||||
color: #008000
|
||||
font-size: 13px
|
||||
|
||||
.bing-result-snippet
|
||||
color: #000
|
||||
font-size: 11px
|
||||
|
||||
#bing-pagination-container
|
||||
margin: 10px
|
||||
margin-left: 20px
|
||||
|
||||
.bing-page-anchor
|
||||
text-decoration: none!important
|
||||
cursor: pointer
|
||||
color: #0000CC
|
||||
margin-right: 8px
|
||||
|
||||
|
|
|
@ -1,23 +1,12 @@
|
|||
{{ define "content" }}
|
||||
<h1>{{ .Title }}</h1>
|
||||
<script type="text/javascript">
|
||||
document.write('<gcse:searchresults-only linktarget="_parent"></gcse:searchresults-only>');
|
||||
</script>
|
||||
|
||||
<script>
|
||||
(function() {
|
||||
var cx = '013288817511911618469:elfqqbqldzg';
|
||||
var gcse = document.createElement('script');
|
||||
gcse.type = 'text/javascript';
|
||||
gcse.async = true;
|
||||
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
|
||||
'//cse.google.com/cse.js?cx=' + cx;
|
||||
var s = document.getElementsByTagName('script')[0];
|
||||
s.parentNode.insertBefore(gcse, s);
|
||||
<gcse:searchresults-only linktarget="_parent">
|
||||
<div id="bing-results-container">Fetching results..</div>
|
||||
<div id="bing-pagination-container"></div>
|
||||
</gcse:searchresults-only>
|
||||
|
||||
document.querySelector('html').classList.add('search');
|
||||
})();
|
||||
</script>
|
||||
<script src="{{ "js/search.js" | relURL }}"></script>
|
||||
{{ end }}
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
window.renderGoogleSearchResults = () => {
|
||||
var cx = '013288817511911618469:elfqqbqldzg';
|
||||
var gcse = document.createElement('script');
|
||||
gcse.type = 'text/javascript';
|
||||
gcse.async = true;
|
||||
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cse.google.com/cse.js?cx=' + cx;
|
||||
var s = document.getElementsByTagName('script')[0];
|
||||
s.parentNode.insertBefore(gcse, s);
|
||||
// document.querySelector('html').classList.add('search');
|
||||
}
|
||||
|
||||
window.getPaginationAnchors = (pages) => {
|
||||
var pageAnchors = '', searchTerm = window.location.search.split("=")[1].split("&")[0];
|
||||
var currentPage = window.location.search.split("=")[2];
|
||||
currentPage = (!currentPage) ? 1 : currentPage.split("&")[0];
|
||||
|
||||
for(var i = 1; i <= 10; i++){
|
||||
if(i > pages) break;
|
||||
pageAnchors += '<a class="bing-page-anchor" href="/docs/search/?q='+searchTerm+'&page='+i+'">';
|
||||
pageAnchors += (currentPage == i) ? '<b>'+i+'</b>' : i;
|
||||
pageAnchors += '</a>';
|
||||
}
|
||||
return pageAnchors;
|
||||
}
|
||||
|
||||
window.getResultMarkupString = (ob) => {
|
||||
return '<div class="bing-result">'
|
||||
+ '<div class="bing-result-name"><a href="'+ob.url+'">'+ob.name+'</a></div>'
|
||||
+ '<div class="bing-result-url">'+ob.displayUrl+'</div>'
|
||||
+ '<div class="bing-result-snippet">'+ob.snippet+'</div>'
|
||||
+'</div>';
|
||||
}
|
||||
|
||||
window.renderBingSearchResults = () => {
|
||||
var searchTerm = window.location.search.split("=")[1].split("&")[0],
|
||||
page = window.location.search.split("=")[2],
|
||||
q = "site:kubernetes.io " + searchTerm;
|
||||
|
||||
page = (!page) ? 1 : page.split("&")[0];
|
||||
|
||||
var results = '', pagination = '', offset = (page - 1) * 10, ajaxConf = {};
|
||||
|
||||
ajaxConf.url = 'https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search';
|
||||
ajaxConf.data = { q: q, offset: offset, customConfig: '320659264' };
|
||||
ajaxConf.type = "GET";
|
||||
ajaxConf.beforeSend = function(xhr){ xhr.setRequestHeader('Ocp-Apim-Subscription-Key', '51efd23677624e04b4abe921225ea7ec'); };
|
||||
|
||||
$.ajax(ajaxConf).done(function(res) {
|
||||
var paginationAnchors = window.getPaginationAnchors(Math.ceil(res.webPages.totalEstimatedMatches / 10));
|
||||
res.webPages.value.map(ob => { results += window.getResultMarkupString(ob); })
|
||||
|
||||
if($('#bing-results-container').length > 0) $('#bing-results-container').html(results);
|
||||
if($('#bing-pagination-container').length > 0) $('#bing-pagination-container').html(paginationAnchors);
|
||||
});
|
||||
document.querySelector('html').classList.add('search');
|
||||
}
|
||||
|
||||
window.renderBingSearchResults();
|
||||
|
||||
$.ajax({
|
||||
url: "https://ipinfo.io",
|
||||
dataType: "jsonp",
|
||||
success: function (response) {
|
||||
if (response.country != 'CN') window.renderGoogleSearchResults();
|
||||
},
|
||||
error: function () {
|
||||
console.log('sorry...')
|
||||
},
|
||||
timeout: 5000
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue