Last active
May 30, 2022 11:25
-
-
Save leihuang23/0a6f8e3b008aedf1398352eaf13caec6 to your computer and use it in GitHub Desktop.
Detect if the user is from China or other countries where Google is blocked
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is mostly useless. I put it here for | |
// the purpose of entertainment... | |
function detectIfUserIsFromChina() { | |
const img = new Image() | |
img.setAttribute("src", `https://www.google.com/favicon.ico?t=${Date.now()}`) | |
img.setAttribute("style", "width:0;height:0;visibility:hidden;") | |
document.body.appendChild(img) | |
return new Promise(res => { | |
img.onerror = () => res(true) | |
img.onload = () => res(false) | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment