2018-08-31 19:08:34 +00:00
|
|
|
document.querySelector('html').classList.add('search');
|
|
|
|
|
2018-08-16 18:47:07 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.getPaginationAnchors = (pages) => {
|
2020-06-16 15:43:46 +00:00
|
|
|
var pageAnchors = '', searchTerm = window.location.search.split("=")[1].split("&")[0].replace(/%20/g, ' ');
|
2018-08-16 18:47:07 +00:00
|
|
|
var currentPage = window.location.search.split("=")[2];
|
|
|
|
currentPage = (!currentPage) ? 1 : currentPage.split("&")[0];
|
|
|
|
|
|
|
|
for(var i = 1; i <= 10; i++){
|
|
|
|
if(i > pages) break;
|
2020-08-11 23:23:07 +00:00
|
|
|
pageAnchors += '<a class="bing-page-anchor" href="/search/?q='+searchTerm+'&page='+i+'">';
|
2018-08-16 18:47:07 +00:00
|
|
|
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 = () => {
|
2020-06-16 15:43:46 +00:00
|
|
|
var searchTerm = window.location.search.split("=")[1].split("&")[0].replace(/%20/g,' '),
|
2018-08-16 18:47:07 +00:00
|
|
|
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) {
|
2020-06-16 15:43:46 +00:00
|
|
|
if (res.webPages == null) return; // If no result, 'webPages' is 'undefined'
|
2018-08-16 18:47:07 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-31 19:08:34 +00:00
|
|
|
//China Verification
|
|
|
|
var path = "path=/;"
|
|
|
|
d = new Date()
|
|
|
|
d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000))
|
|
|
|
expires = "expires=" + d.toUTCString()
|
2018-08-16 18:47:07 +00:00
|
|
|
|
2018-08-31 19:08:34 +00:00
|
|
|
function getCookie(name) {
|
|
|
|
var value = "; " + document.cookie;
|
|
|
|
var parts = value.split("; " + name + "=");
|
|
|
|
if (parts.length == 2) return parts.pop().split(";").shift();
|
|
|
|
else return "";
|
|
|
|
}
|
2018-08-16 18:47:07 +00:00
|
|
|
|
2018-08-31 19:08:34 +00:00
|
|
|
if (getCookie("is_china") === "") {
|
|
|
|
$.ajax({
|
|
|
|
url: "https://ipinfo.io?token=796e43f4f146b1",
|
|
|
|
dataType: "jsonp",
|
|
|
|
success: function (response) {
|
|
|
|
if (response.country == 'CN') {
|
|
|
|
window.renderBingSearchResults()
|
|
|
|
document.cookie = "is_china=true;" + path + expires
|
|
|
|
} else {
|
|
|
|
window.renderGoogleSearchResults()
|
|
|
|
document.cookie = "is_china=false;" + path + expires;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
window.renderBingSearchResults()
|
|
|
|
document.cookie = "is_china=true;" + path + expires;
|
|
|
|
},
|
|
|
|
timeout: 3000
|
|
|
|
});
|
|
|
|
} else if (getCookie("is_china") === "true") {
|
|
|
|
window.renderBingSearchResults()
|
|
|
|
} else {
|
|
|
|
window.renderGoogleSearchResults()
|
|
|
|
}
|
2018-08-16 18:47:07 +00:00
|
|
|
|
|
|
|
|