From 9daa9007935578ed1d12d46916d913d802816087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Wed, 29 Apr 2020 05:30:58 +0900 Subject: [PATCH] Refactor codes --- src/client/components/hcaptcha.vue | 20 ++++++-------------- src/client/pages/instance/settings.vue | 3 +-- src/server/api/private/signup.ts | 2 +- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/client/components/hcaptcha.vue b/src/client/components/hcaptcha.vue index 4711b303d7..f493366486 100644 --- a/src/client/components/hcaptcha.vue +++ b/src/client/components/hcaptcha.vue @@ -40,20 +40,12 @@ export default Vue.extend({ if (window.hcaptcha) { // loaded this.available = true; } else { - const alreadyLoading = document.getElementById('hcaptcha'); - - if (alreadyLoading) { // loading - alreadyLoading.addEventListener('load', () => this.available = true); - - return; - } // init - - const script = document.createElement('script'); - script.addEventListener('load', () => this.available = true); - script.async = true; - script.id = 'hcaptcha'; - script.src = 'https://hcaptcha.com/1/api.js?render=explicit'; - document.head.appendChild(script); + (document.getElementById('hcaptcha') || (x => document.head.appendChild(Object.assign(x, { + async: true, + id: 'hcaptcha', + src: 'https://hcaptcha.com/1/api.js?render=explicit', + })))(document.createElement('script'))) + .addEventListener('load', () => this.available = true); } }, diff --git a/src/client/pages/instance/settings.vue b/src/client/pages/instance/settings.vue index 088db8763e..1292316b22 100644 --- a/src/client/pages/instance/settings.vue +++ b/src/client/pages/instance/settings.vue @@ -371,11 +371,10 @@ export default Vue.extend({ } if (recaptchaLoaded) { // loaded - delete window.onRecaptchaLoad; renderRecaptchaPreview(); } else { // init window.onRecaptchaLoad = () => { - recaptchaLoaded = true; + recaptchaLoaded = delete window.onRecaptchaLoad; renderRecaptchaPreview(); }; const script = document.createElement('script'); diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index e23fe43583..04d0501b6c 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -14,7 +14,7 @@ export default async (ctx: Koa.Context) => { // ただしテスト時はこの機構は障害となるため無効にする if (process.env.NODE_ENV !== 'test' && instance.enableHcaptcha && instance.hcaptchaSecretKey) { const success = await verify(instance.hcaptchaSecretKey, body['hcaptcha-response']).then( - ({ 'error-codes': x }) => !x || !x.length, + ({ success }) => success, () => false, );