Skip to content

Commit 8e5f618

Browse files
committed
Improve China test
Signed-off-by: Chris Abraham <[email protected]>
1 parent 13cae23 commit 8e5f618

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

static/js/search.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,39 @@
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+
try {
73+
const response = await fetch(url, { method: 'HEAD', mode: 'no-cors' });
74+
// If we reach this point, the site is accessible (since mode: 'no-cors' doesn't allow us to check response.ok)
75+
return false;
76+
} catch (error) {
77+
// If an error occurs, it's likely the site is blocked
78+
return true;
79+
}
80+
}
81+
82+
async function runBlockedContentTest() {
83+
if (getCookie("in_china") === "") {
84+
const isBlocked = await checkBlockedSite("https://www.google.com/favicon.ico");
85+
if ( isBlocked ) {
86+
// Site is blocked so we think you are in China.
87+
console.log("We think you ARE in China")
88+
document.cookie = "in_china=true;" + path + expires
8589
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) => {
92-
window.renderPageFindSearchResults()
93-
});
94-
} else {
95-
window.renderGoogleSearchResults()
90+
} else {
91+
// Site isn't blocked so we think you are NOT in China.
92+
console.log("We think you are NOT in China")
93+
document.cookie = "in_china=false;" + path + expires
94+
window.renderGoogleSearchResults()
95+
}
96+
} else if (getCookie("in_china") == "true") {
97+
window.addEventListener('DOMContentLoaded', (event) => {
98+
window.renderPageFindSearchResults()
99+
});
100+
} else {
101+
window.renderGoogleSearchResults()
102+
}
96103
}
97104

105+
window.onload = runBlockedContentTest;
98106

0 commit comments

Comments
 (0)