website/static/js/search.js

74 lines
3.0 KiB
JavaScript

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
});