|
1 | 1 | document.querySelector('html').classList.add('search');
|
2 | 2 |
|
| 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 | + |
3 | 15 | window.renderGoogleSearchResults = () => {
|
4 | 16 | var cx = '013288817511911618469:elfqqbqldzg';
|
5 | 17 | var gcse = document.createElement('script');
|
|
33 | 45 | }
|
34 | 46 |
|
35 | 47 | 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 = {}; |
39 | 55 |
|
40 |
| - page = (!page) ? 1 : page.split("&")[0]; |
| 56 | + if (!searchTerm) return; |
41 | 57 |
|
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"; |
43 | 61 |
|
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 | + } |
48 | 67 |
|
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); }) |
53 | 71 |
|
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 | + }); |
57 | 75 | }
|
58 | 76 |
|
59 |
| - //China Verification |
| 77 | + // China Verification. |
60 | 78 | var path = "path=/;"
|
61 | 79 | d = new Date()
|
62 | 80 | d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000))
|
|
0 commit comments