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); | |
}; |