Skip to content

Instantly share code, notes, and snippets.

@leihuang23
Last active May 30, 2022 11:25
Show Gist options
  • Save leihuang23/0a6f8e3b008aedf1398352eaf13caec6 to your computer and use it in GitHub Desktop.
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 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