Skip to content

Instantly share code, notes, and snippets.

@lepture
Last active April 13, 2024 11:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lepture/a9dd50e0688d5fe42a3ddb0e303377f1 to your computer and use it in GitHub Desktop.
Save lepture/a9dd50e0688d5fe42a3ddb0e303377f1 to your computer and use it in GitHub Desktop.
Is in China

Detecting if current browser is in China

Use the script below:

isInChina(function(inChina) {
  console.log('In china: ' + inChina);
})

If your page has a Content-Security-Policy header, add facebook url to it.

function isInChina(cb) {
var url = '//graph.facebook.com/feed?callback=h';
var xhr = new XMLHttpRequest();
var called = false;
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
called = true;
cb(false);
}
};
xhr.send();
// timeout 1s, this facebook API is very fast.
setTimeout(function() {
if (!called) {
xhr.abort();
cb(true);
}
}, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment