|
68 | 68 | else return "";
|
69 | 69 | }
|
70 | 70 |
|
71 |
| - if (getCookie("is_china") === "") { |
72 |
| - $.ajax({ |
73 |
| - url: "https://ipinfo.io?token=796e43f4f146b1", |
74 |
| - dataType: "jsonp", |
75 |
| - success: function (response) { |
76 |
| - if (response.country == 'CN') { |
77 |
| - window.renderPageFindSearchResults() |
78 |
| - document.cookie = "is_china=true;" + path + expires |
79 |
| - } else { |
80 |
| - window.renderGoogleSearchResults() |
81 |
| - document.cookie = "is_china=false;" + path + expires; |
82 |
| - } |
83 |
| - }, |
84 |
| - error: function () { |
| 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 | + console.log("Google is blocked") |
| 94 | + document.cookie = "can_google=false;" + path + expires |
85 | 95 | window.renderPageFindSearchResults()
|
86 |
| - document.cookie = "is_china=true;" + path + expires; |
87 |
| - }, |
88 |
| - timeout: 3000 |
89 |
| - }); |
90 |
| - } else if (getCookie("is_china") == "true") { |
91 |
| - window.addEventListener('DOMContentLoaded', (event) => { |
| 96 | + } else { |
| 97 | + // Google is not blocked. |
| 98 | + console.log("Google is NOT blocked") |
| 99 | + document.cookie = "can_google=true;" + path + expires |
| 100 | + window.renderGoogleSearchResults() |
| 101 | + } |
| 102 | + } else if (getCookie("can_google") == "false") { |
92 | 103 | window.renderPageFindSearchResults()
|
93 |
| - }); |
94 |
| - } else { |
95 |
| - window.renderGoogleSearchResults() |
| 104 | + } else { |
| 105 | + window.renderGoogleSearchResults() |
| 106 | + } |
96 | 107 | }
|
97 | 108 |
|
| 109 | + window.onload = loadSearch; |
98 | 110 |
|
0 commit comments