Skip to content

Commit

Permalink
fix iframes loop bug and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Oct 16, 2021
1 parent 916c7d0 commit b956bf1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/models/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function ip2int(ip) {
}

function now(){
return parseInt(Date.now() / 1000)
return Math.round(Date.now() / 1000)
}

//Creates a signed ticket to guarantee the validity of the user's request.
Expand Down
27 changes: 8 additions & 19 deletions src/views/embedded/api.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,7 @@ const nanodrop = {

//Sometimes we need to wait for a response before deleting the iframe
function resetIframe(id){
const iframe = document.getElementById(id)
if (typeof(iframe) != 'undefined' && iframe != null){
iframe.style.display = "none"
iframe.id += "_old"
setTimeout(function () {
iframe.remove()
}, 60000);
}
document.getElementById(id).remove()
}

function isNanoAddress(address) {
Expand All @@ -156,17 +149,15 @@ function listenMessages(){
document.getElementById('nanoDropCheckbox').contentWindow.postMessage(e.data, '*')

if (e.data.from == "challenge") {
if ("success" in e.data && e.data.success == true) {
if ("success" in e.data && e.data.success === true) {
resetIframe("nanoDrop_challenge")
} else if ("success" in e.data && e.data.success == false) {
} else if ("success" in e.data && e.data.success === false) {
resetIframe("nanoDrop_challenge")
}
} else if (e.data.from == "oauth") {
if ("loaded" in e.data && e.data.loaded == "oauth") {
//
} else if ("success" in e.data && e.data.success == true) {
if ("success" in e.data && e.data.success === true) {
resetIframe("nanoDrop_oAuth")
} else if ("success" in e.data && e.data.success == false){
} else if ("success" in e.data && e.data.success === false){
resetIframe("nanoDrop_oAuth")
}
}
Expand All @@ -179,9 +170,9 @@ function listenMessages(){
}
} else if("loaded" in e.data && e.data.loaded == "checkbox") {
nanodrop.rendered = true, rendered = true
} else if ("loaded" in e.data && e.data.loaded == false){
} else if ("loaded" in e.data && e.data.loaded === false){
nanodrop.rendered = false
} else if ("success" in e.data && e.data.success == true) {
} else if ("success" in e.data && e.data.success === true) {
if (config.onsuccessCallback !== null && config.onsuccessCallback != "") {
if (window[config.onsuccessCallback] == undefined) {
alert("onsuccessCallback '" + config.onsuccessCallback + "' does not exist")
Expand All @@ -193,10 +184,9 @@ function listenMessages(){
}
}
}
resetIframe("nanoDrop_oAuth")
} else if ("success" in e.data && e.data.success == false){
if (config.onerrorCallback !== null && config.onerrorCallback != "") {
if (window[config.onerrorCallback] == undefined) {
if (window[config.onerrorCallback] === undefined) {
alert("onerrorCallback '" + config.onerrorCallback + "' does not exist")
} else {
if (typeof(window[config.onerrorCallback]) === "function"){
Expand All @@ -206,7 +196,6 @@ function listenMessages(){
}
}
}
resetIframe("nanoDrop_oAuth")
}
}
}
Expand Down
69 changes: 44 additions & 25 deletions src/views/embedded/challenge.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,56 @@
}
}
let oAuth = { token: "" }
let submited = false;
let recaptcha_v2_widget, recaptcha_v3_widget;
let submited = false, recaptcha_v2_widget, recaptcha_v3_widget, recaptcha_v2_token
function expiredRecaptcha() {
if (!submited) window.top.postMessage({ target: "checkbox", from: "challenge", success: false, error: "expired recaptcha" }, '*')
if (!submited) window.top.postMessage({
target: "checkbox",
from: "challenge",
success: false,
error: "expired recaptcha"
}, '*')
}
function errorRecaptcha(err) {
if (typeof (err) === 'undefined') err = "google recaptcha error"
console.error(err)
window.top.postMessage({ target: "checkbox", from: "challenge", success: false, error: err }, '*')
window.top.postMessage({
target: "checkbox",
from: "challenge",
success: false,
error: err
}, '*')
}
function onloadCallback() {
function execV3(v2_token = undefined) {
recaptcha_v2_token = v2_token
recaptcha_v3_widget = grecaptcha.render({
'sitekey': google_reCaptcha.v3.siteKey,
'callback': onSubmit,
'size': 'invisible'
});
recaptcha_v3_widget = grecaptcha.render({
'sitekey': google_reCaptcha.v3.siteKey,
'callback': onSubmit,
'size': 'invisible'
});
function execV3() {
window.recaptchaSolved = true
grecaptcha.execute(recaptcha_v3_widget)
}
function onSubmit(recaptchaV3_token) {
submited = true
window.top.postMessage({
target: "checkbox",
from: "challenge",
success: true,
recaptchaV2_token: recaptcha_v2_token,
recaptchaV3_token: recaptchaV3_token
}, '*')
}
// Renders the HTML element with id 'example1' as a reCAPTCHA widget.
// The id of the reCAPTCHA widget is assigned to 'widgetId1'.
recaptcha_v2_widget = grecaptcha.render("g-recaptcha", {
Expand All @@ -80,18 +102,8 @@
'callback': execV3
});
function onSubmit(recaptchaV3_token) {
submited = true
grecaptcha.reset();
try {
const recaptchaV2_token = grecaptcha.getResponse(recaptcha_v2_widget)
window.top.postMessage({ target: "checkbox", from: "challenge", success: true, recaptchaV2_token: recaptchaV2_token, recaptchaV3_token: recaptchaV3_token, oAuth_token: oAuth.token }, '*')
} catch (err) {
window.top.postMessage({ target: "checkbox", from: "challenge", success: false, error: err }, '*')
}
}
grecaptcha.execute(recaptcha_v2_widget)
if (!window.recaptchaCloseListener_loaded) recaptchaCloseListener()
};
Expand All @@ -109,7 +121,10 @@
// when the opacity has been changed to 0 we know that
// the window has been closed
new MutationObserver(x => recaptchaWindow.style.opacity == 0 && recatpchaOnClose())
.observe(recaptchaWindow, { attributes: true, attributeFilter: ['style'] })
.observe(recaptchaWindow, {
attributes: true,
attributeFilter: ['style']
})
} catch (err) {
setTimeout(function () {
Expand All @@ -121,7 +136,12 @@
function recatpchaOnClose() {
if (!window.recaptchaSolved) {
console.warn('recaptcha window has been closed')
window.top.postMessage({ target: "checkbox", from: "challenge", success: false, error: "recaptcha window has been closed" }, '*')
window.top.postMessage({
target: "checkbox",
from: "challenge",
success: false,
error: "recaptcha window has been closed"
}, '*')
}
}
Expand All @@ -134,7 +154,6 @@
}, '*')
}
</script>

</head>
Expand Down
6 changes: 5 additions & 1 deletion src/views/embedded/oauth.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<meta name="theme-color" content="#ffffff">

<style>
html, body {
margin: 0;
background: transparent;
}
.modal {
background-image: linear-gradient(rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.75) 100.2%);
}
Expand Down Expand Up @@ -65,7 +69,7 @@
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
const auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
Expand Down
17 changes: 12 additions & 5 deletions src/views/embedded/scripts/checkbox.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@
function msgListener() {
addListener(function (e) {
if ("loaded" in e.data && e.data.loaded == "challenge") {
console.info("recaptcha challenge loaded")
} else if (e.data.from == "challenge") {
if (e.data.from == "challenge") {
if ("success" in e.data && e.data.success == true) {
dropMe(e.data)
} else if ("success" in e.data && e.data.success == false) {
Expand Down Expand Up @@ -215,6 +213,9 @@
checkboxError(res.error)
window.top.postMessage({ from: "checkbox", success: false, error: res.error }, '*')
}
} else if ("error" in res) {
checkboxError(JSON.stringify(res.error).slice(0, 40))
window.top.postMessage({ from: "checkbox", success: false, error: res.error }, '*')
} else {
checkboxError("Invalid response from server")
window.top.postMessage({ from: "checkbox", success: false, error: "Invalid response from server" }, '*')
Expand All @@ -231,9 +232,11 @@
}
}
let challengeLoading = false
function enableCheckbox() {
document.getElementById("nd-anchor-container").querySelector(".nd-anchor-content").addEventListener("click", function (e) {
if (e.isTrusted) {
const recaptcha_content = document.getElementById("nd-anchor-container").querySelector(".nd-anchor-content")
recaptcha_content.addEventListener("click", function (e) {
if (e.isTrusted && !challengeLoading) {
checkboxReset()
playSound("pending")
if (account === null) {
Expand All @@ -242,9 +245,13 @@
checkboxReset()
}, 5000)
} else {
challengeLoading = true
checkboxLoader()
setTimeout(function () {
window.top.postMessage({ from: "checkbox", action: "challenge" }, '*')
addListener((e) => {
if ("loaded" in e.data && e.data.loaded == "challenge") challengeLoading = false
})
}, 200)
}
}
Expand Down

1 comment on commit b956bf1

@vercel
Copy link

@vercel vercel bot commented on b956bf1 Apr 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.