Skip to content

Instantly share code, notes, and snippets.

@tomac4t
Last active January 30, 2020 14:15
Show Gist options
  • Save tomac4t/98c519db8f9d19bd84a61ca6c2b35cf4 to your computer and use it in GitHub Desktop.
Save tomac4t/98c519db8f9d19bd84a61ca6c2b35cf4 to your computer and use it in GitHub Desktop.
Google Connectivity Check
var connectivityFirstCheck = true;
var connectivityTestPassed;
// https://github.com/SukkaW/DisqusJS/blob/master/src/disqus.js#L235
const connectivityCheck = () => {
connectivityFirstCheck = false;
const img = new Image;
const timeout = setTimeout(() => {
img.onerror = img.onload = null;
connectivityTestPassed = false;
connectivityCallback();
},
3000);
img.onerror = () => {
clearTimeout(timeout);
connectivityTestPassed = false;
connectivityCallback();
}
img.onload = () => {
clearTimeout(timeout);
connectivityTestPassed = true;
connectivityCallback();
}
img.src = "https://www.google.com/images/hpp/icon-privacy-shield-rgb-120dp.png";
var recaptchaURL;
var recaptchaHost = "https://www.google.com";
var recaptchaHostMirror = "https://www.recaptcha.net";
var recaptchaPath = "/recaptcha/api.js?onload=anr_onloadCallback&render=explicit";
const scriptInjecting = () => {
var script = document.createElement('script');
script.type = "text/javascript";
script.src = recaptchaURL;
script.async=true;
script.defer=true;
document.getElementsByTagName('head')[0].appendChild(script);
}
const connectivityCallback = () => {
if(connectivityTestPassed == true){
recaptchaURL = recaptchaHost + recaptchaPath;
scriptInjecting();
} else {
recaptchaURL = recaptchaHostMirror + recaptchaPath;
scriptInjecting();
}
}
}
// Run the check.
$(".comment-form-comment>textarea").focus(function(){
if (connectivityFirstCheck == true) {
connectivityCheck();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment