-
Notifications
You must be signed in to change notification settings - Fork 23
/
subscribe.js
89 lines (73 loc) · 3.57 KB
/
subscribe.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
document.getElementById('subscribe_form').addEventListener('submit', function (evt) {
evt.preventDefault();
// Hide the email field
document.getElementById('new_subscription').style.display = 'none';
// The reCAPTCHA may be on the page already if we've arrived here after an earlier error
if (!window['___grecaptcha_cfg']) {
// Write the reCAPTCHA script tag to the page
var reCaptchaScript = document.createElement('script');
reCaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js');
document.head.appendChild(reCaptchaScript);
// Write the reCAPTCHA element to the page
var reCaptchaDiv = document.createElement('div');
reCaptchaDiv.setAttribute('class', 'g-recaptcha');
reCaptchaDiv.setAttribute('data-sitekey', '6LdqYhoUAAAAADieTdwsCSDl0-zIpp6Ga-JzatIZ');
reCaptchaDiv.setAttribute('data-callback', 'reCaptchaSolved');
document.getElementById('confirm_captcha').appendChild(reCaptchaDiv);
} else {
grecaptcha.reset();
}
document.getElementById('confirm_captcha').style.display = 'inline-block';
});
function reCaptchaSolved() {
// Hide the button and show the loader
document.getElementById('subscribe_loading').style.display = 'block';
// Hide the CAPTCHA
document.getElementById('confirm_captcha').style.display = 'none';
var email = encodeURIComponent(document.getElementById('email_to_subscribe').value);
var sourceUrl = encodeURIComponent(window.location);
var emailCadence;
var radios = document.getElementsByName('email_cadence');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
emailCadence = radios[i].value;
break;
}
}
var reCaptchaResponse = document.getElementById('g-recaptcha-response').value;
var postData = 'Email=' + email +
'&EmailCadence=' + emailCadence +
'&SourceUrl=' + sourceUrl +
'&g-recaptcha-response=' + reCaptchaResponse;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'https://bloghelpers.troyhunt.com/api/subscribe', true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onload = function () {
document.getElementById('subscribe_loading').style.display = 'none';
if (this.status === 200) {
document.getElementById('subscribe_successful').style.display = 'block';
}
else {
var errorResponse = JSON.parse(this.responseText);
var errorMessage = errorResponse === 'FakeOrInvalid'
? 'Uh oh, apparently that\'s a fake or invalid email, sorry!'
: 'Uh oh, that didn\'t work.Try submitting it again.';
document.getElementById('subscribe_unsuccessful').innerHTML = '<p>' + errorMessage + '</p >';
document.getElementById('subscribe_unsuccessful').style.display = 'block';
document.getElementById('new_subscription').style.display = 'block';
}
};
xmlhttp.send(postData);
}
var floating_subscribe = document.getElementById("floating_subscribe")
if (document.cookie.indexOf('closeFloatingSubscribe') === -1) {
floating_subscribe.style.display = 'block';
}
function closeFloatingSubscribe() {
floating_subscribe.style.display = 'none';
document.cookie = "closeFloatingSubscribe=true; expires=1 Jan 2030 00:00:00 UTC; path=/";
};
var closeElement = document.getElementById("close_floating_subscribe");
closeElement.addEventListener('click', function(evt){closeFloatingSubscribe();evt.preventDefault();}, false);
var subscribeElement = document.getElementById("subscribe_link");
subscribeElement.addEventListener('click', function(){closeFloatingSubscribe()}, false);