Skip to content

Commit f477d1c

Browse files
authored
Merge pull request #47128 from cjyabraham/cn
Improve the test to decide which search results to serve
2 parents 88481af + 4c79d66 commit f477d1c

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

static/js/search.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,43 @@
6868
else return "";
6969
}
7070

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
8595
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") {
92103
window.renderPageFindSearchResults()
93-
});
94-
} else {
95-
window.renderGoogleSearchResults()
104+
} else {
105+
window.renderGoogleSearchResults()
106+
}
96107
}
97108

109+
window.onload = loadSearch;
98110

0 commit comments

Comments
 (0)