From 4b7eb3d4582ffd73bc0e07d89f852378d9cef93c Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 16:23:05 +0200 Subject: [PATCH 1/6] on user exit without complete throw error --- src/recaptcha.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index 3448c41..d5b2c5e 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -35,11 +35,32 @@ export default class ReCAPTCHA extends React.Component { } executeAsync() { - return new Promise((resolve, reject) => { + const original = new Promise((resolve, reject) => { this.executionResolve = resolve; this.executionReject = reject; this.execute(); }); + if (!('MutationObserver' in window)) return original; + return new Promise((resolve, reject) => { + const frame = document + .querySelector('iframe[src*="google.com/recaptcha/api2/bframe"]') + .parentNode + .parentNode as HTMLElement; + const observer = new MutationObserver(() => { + if (Number(frame.style.opacity) === 0) { + reject(new Error('User clicked outside')); + observer.disconnect(); + } + }); + observer.observe(frame, { attributes: true, attributeFilter: ['style'] }); + return original.then((result) => { + observer.disconnect(); + resolve(result); + }).catch((e) => { + observer.disconnect(); + reject(e); + }); + }); } reset() { From 89f951d26fe38f80103cf0854d046e7434df16db Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 17:02:07 +0200 Subject: [PATCH 2/6] remove tipyngs --- src/recaptcha.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index d5b2c5e..1d9a8d9 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -45,7 +45,7 @@ export default class ReCAPTCHA extends React.Component { const frame = document .querySelector('iframe[src*="google.com/recaptcha/api2/bframe"]') .parentNode - .parentNode as HTMLElement; + .parentNode; const observer = new MutationObserver(() => { if (Number(frame.style.opacity) === 0) { reject(new Error('User clicked outside')); From 735c01d2a83bfef4024d40fca1ab54b60edb55ea Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 17:12:33 +0200 Subject: [PATCH 3/6] ad check if frame exists --- src/recaptcha.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index 1d9a8d9..50ca8b8 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -44,8 +44,11 @@ export default class ReCAPTCHA extends React.Component { return new Promise((resolve, reject) => { const frame = document .querySelector('iframe[src*="google.com/recaptcha/api2/bframe"]') - .parentNode - .parentNode; + ?.parentNode + ?.parentNode; + if (!frame) { + return original.then(resolve).catch(reject); + } const observer = new MutationObserver(() => { if (Number(frame.style.opacity) === 0) { reject(new Error('User clicked outside')); From f797f7fb9f072b61e9e06567a78bf3d6b7c05262 Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 17:14:48 +0200 Subject: [PATCH 4/6] fix lint --- src/recaptcha.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index 50ca8b8..d103885 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -40,10 +40,10 @@ export default class ReCAPTCHA extends React.Component { this.executionReject = reject; this.execute(); }); - if (!('MutationObserver' in window)) return original; + if (!("MutationObserver" in window)) return original; return new Promise((resolve, reject) => { const frame = document - .querySelector('iframe[src*="google.com/recaptcha/api2/bframe"]') + .querySelector("iframe[src*=\"google.com/recaptcha/api2/bframe\"]") ?.parentNode ?.parentNode; if (!frame) { @@ -55,7 +55,7 @@ export default class ReCAPTCHA extends React.Component { observer.disconnect(); } }); - observer.observe(frame, { attributes: true, attributeFilter: ['style'] }); + observer.observe(frame, { attributes: true, attributeFilter: ["style"] }); return original.then((result) => { observer.disconnect(); resolve(result); From cb91a10ed994da8c96ff87c1ff2b60737edab106 Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 17:15:48 +0200 Subject: [PATCH 5/6] fix lint --- src/recaptcha.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index d103885..342fd17 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -56,10 +56,10 @@ export default class ReCAPTCHA extends React.Component { } }); observer.observe(frame, { attributes: true, attributeFilter: ["style"] }); - return original.then((result) => { + return original.then(result => { observer.disconnect(); resolve(result); - }).catch((e) => { + }).catch(e => { observer.disconnect(); reject(e); }); From 269cc639ef3e7aa6172ed84521f0f27a0d15352e Mon Sep 17 00:00:00 2001 From: Alfredo Salzillo Date: Mon, 19 Oct 2020 17:16:38 +0200 Subject: [PATCH 6/6] Update recaptcha.js --- src/recaptcha.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/recaptcha.js b/src/recaptcha.js index 342fd17..40f997e 100644 --- a/src/recaptcha.js +++ b/src/recaptcha.js @@ -43,7 +43,7 @@ export default class ReCAPTCHA extends React.Component { if (!("MutationObserver" in window)) return original; return new Promise((resolve, reject) => { const frame = document - .querySelector("iframe[src*=\"google.com/recaptcha/api2/bframe\"]") + .querySelector('iframe[src*="google.com/recaptcha/api2/bframe"]') ?.parentNode ?.parentNode; if (!frame) {