|
1 | | -/* |
2 | | -Copyright 2018 Google LLC |
3 | | -Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | -you may not use this file except in compliance with the License. |
5 | | -You may obtain a copy of the License at |
6 | | - https://www.apache.org/licenses/LICENSE-2.0 |
7 | | -Unless required by applicable law or agreed to in writing, software |
8 | | -distributed under the License is distributed on an "AS IS" BASIS, |
9 | | -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
10 | | -See the License for the specific language governing permissions and |
11 | | -limitations under the License. |
12 | | -*/ |
13 | | - |
14 | | -(function ($) { |
15 | | - "use strict"; |
16 | | - |
17 | | - var Search = { |
18 | | - init: function () { |
19 | | - $(document).ready(function () { |
20 | | - // Fill the search input form with the current search keywords |
21 | | - const searchKeywords = new URLSearchParams(location.search).get('q'); |
22 | | - if (searchKeywords !== null && searchKeywords !== '') { |
23 | | - const searchInput = document.querySelector('.td-search-input'); |
24 | | - searchInput.focus(); |
25 | | - searchInput.value = searchKeywords; |
26 | | - } |
| 1 | + document.querySelector('html').classList.add('search'); |
| 2 | + |
| 3 | + document.addEventListener('DOMContentLoaded', function() { |
| 4 | + let searchTerm = new URLSearchParams(window.location.search).get('q'); |
| 5 | + let fetchingElem = document.getElementById('bing-results-container'); |
27 | 6 |
|
28 | | - // Set a keydown event |
29 | | - $(document).on("keypress", ".td-search-input", function (e) { |
30 | | - if (e.keyCode !== 13) { |
31 | | - return; |
32 | | - } |
| 7 | + if (!searchTerm) { |
| 8 | + if (fetchingElem) fetchingElem.style.display = 'none'; |
| 9 | + } |
| 10 | + }); |
33 | 11 |
|
34 | | - var query = $(this).val(); |
35 | | - var searchPage = $(this).data('search-page') + "?q=" + query; |
36 | | - document.location = searchPage; |
| 12 | + window.renderGoogleSearchResults = () => { |
| 13 | + var cx = '013288817511911618469:elfqqbqldzg'; |
| 14 | + var gcse = document.createElement('script'); |
| 15 | + gcse.type = 'text/javascript'; |
| 16 | + gcse.async = true; |
| 17 | + gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//cse.google.com/cse.js?cx=' + cx; |
| 18 | + var s = document.getElementsByTagName('script')[0]; |
| 19 | + s.parentNode.insertBefore(gcse, s); |
| 20 | + } |
| 21 | + |
| 22 | + window.renderPageFindSearchResults = () => { |
| 23 | + let urlParams = new URLSearchParams(window.location.search); |
| 24 | + let searchTerm = urlParams.get("q") || ""; |
| 25 | + let sidebarSearch = document.querySelector('.td-sidebar__search'); |
| 26 | + if (sidebarSearch) { |
| 27 | + sidebarSearch.remove(); |
| 28 | + } |
| 29 | + document.getElementById('search').style.display = 'block'; |
| 30 | + pagefind = new PagefindUI({ element: "#search", showImages: false }); |
| 31 | + if (searchTerm) { |
| 32 | + pagefind.triggerSearch(searchTerm); |
| 33 | + } |
37 | 34 |
|
38 | | - return false; |
| 35 | + document.querySelector("#search input").addEventListener("input", function() { |
| 36 | + var inputValue = this.value; |
| 37 | + var queryStringVar = "q"; |
| 38 | + updateQueryString(queryStringVar, inputValue); |
39 | 39 | }); |
40 | | - }); |
41 | | - }, |
42 | | - }; |
| 40 | + } |
| 41 | + |
| 42 | + function updateQueryString(key, value) { |
| 43 | + var baseUrl = window.location.href.split("?")[0]; |
| 44 | + var queryString = window.location.search.slice(1); |
| 45 | + var urlParams = new URLSearchParams(queryString); |
| 46 | + |
| 47 | + if (urlParams.has(key)) { |
| 48 | + urlParams.set(key, value); |
| 49 | + } else { |
| 50 | + urlParams.append(key, value); |
| 51 | + } |
| 52 | + |
| 53 | + var newUrl = baseUrl + "?" + urlParams.toString(); |
| 54 | + // Update the browser history (optional) |
| 55 | + history.replaceState(null, '', newUrl); |
| 56 | + } |
| 57 | + |
| 58 | + // China Verification. |
| 59 | + var path = "path=/;" |
| 60 | + d = new Date() |
| 61 | + d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000)) |
| 62 | + expires = "expires=" + d.toUTCString() |
| 63 | + |
| 64 | + function getCookie(name) { |
| 65 | + var value = "; " + document.cookie; |
| 66 | + var parts = value.split("; " + name + "="); |
| 67 | + if (parts.length == 2) return parts.pop().split(";").shift(); |
| 68 | + else return ""; |
| 69 | + } |
| 70 | + |
| 71 | + async function checkBlockedSite(url) { |
| 72 | + const controller = new AbortController(); |
| 73 | + const timeout = setTimeout(() => { |
| 74 | + controller.abort(); |
| 75 | + }, 5000); // Timeout set to 5000ms (5 seconds) |
| 76 | + |
| 77 | + try { |
| 78 | + const response = await fetch(url, { method: 'HEAD', mode: 'no-cors', signal: controller.signal }); |
| 79 | + // If we reach this point, the site is accessible (since mode: 'no-cors' doesn't allow us to check response.ok) |
| 80 | + clearTimeout(timeout); |
| 81 | + return false; |
| 82 | + } catch (error) { |
| 83 | + // If an error occurs, it's likely the site is blocked |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + async function loadSearch() { |
| 89 | + if (getCookie("can_google") === "") { |
| 90 | + const isGoogleBlocked = await checkBlockedSite("https://www.google.com/favicon.ico"); |
| 91 | + if ( isGoogleBlocked ) { |
| 92 | + // Google is blocked. |
| 93 | + document.cookie = "can_google=false;" + path + expires |
| 94 | + window.renderPageFindSearchResults() |
| 95 | + } else { |
| 96 | + // Google is not blocked. |
| 97 | + document.cookie = "can_google=true;" + path + expires |
| 98 | + window.renderGoogleSearchResults() |
| 99 | + } |
| 100 | + } else if (getCookie("can_google") == "false") { |
| 101 | + window.renderPageFindSearchResults() |
| 102 | + } else { |
| 103 | + window.renderGoogleSearchResults() |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + window.onload = loadSearch; |
43 | 108 |
|
44 | | - Search.init(); |
45 | | -})(jQuery); |
|
0 commit comments