From 5d90cf0f286f02f2884728a4ce947d5e8217e8bd Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 18:46:13 -0700 Subject: [PATCH 01/10] Don't need to replace language if we're replacing the whole captcha. --- src/Miscellaneous/Report.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Miscellaneous/Report.coffee b/src/Miscellaneous/Report.coffee index 4f1ec3d3c..742e10ea7 100755 --- a/src/Miscellaneous/Report.coffee +++ b/src/Miscellaneous/Report.coffee @@ -3,7 +3,7 @@ Report = init: -> return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ - Captcha.language.fixPage() + Captcha.language.fixPage() unless Conf['Use Recaptcha v2 in Reports'] @postID = +match[1] $.ready @ready From c54cf1fe0e7d869ae6ca0d3cc3ea1919aa7d2de1 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 20:27:20 -0700 Subject: [PATCH 02/10] Move www.4chan.org init code to a more normal place. --- src/General/Main.coffee | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 108e9b43d..20e7e9936 100755 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -11,14 +11,6 @@ Main = $.ready -> Captcha.fixes.init() return - if location.hostname is 'www.4chan.org' - $.onExists d.documentElement, 'body', false, -> $.addStyle Main.cssWWW - Conf = {'captchaLanguage': Config.captchaLanguage} - $.get Conf, (items) -> - $.extend Conf, items - Captcha.language.fixPage() - return - g.threads = new SimpleDict() g.posts = new SimpleDict() @@ -71,6 +63,9 @@ Main = $.globalEval 'document.documentElement.classList.add("js-enabled");' switch location.hostname + when 'www.4chan.org' + Captcha.language.fixPage() + return when 'a.4cdn.org' return when 'sys.4chan.org' @@ -119,7 +114,10 @@ Main = $.ready Main.initReady initStyle: -> + $.addStyle Main.cssWWW if location.hostname is 'www.4chan.org' + return if !Main.isThisPageLegit() or $.hasClass doc, 'fourchan-x' + # disable the mobile layout $('link[href*=mobile]', d.head)?.disabled = true $.addClass doc, 'fourchan-x', 'seaweedchan' From bf461e1d6ad6afd8e4dea20ff3e85632de387bfa Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 20:54:17 -0700 Subject: [PATCH 03/10] Merge Captcha.language.fixPage into init. --- src/General/Main.coffee | 2 +- src/Miscellaneous/Report.coffee | 2 +- src/Posting/Captcha.language.coffee | 13 +++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 20e7e9936..22027b52d 100755 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -64,7 +64,7 @@ Main = switch location.hostname when 'www.4chan.org' - Captcha.language.fixPage() + Captcha.language.init() return when 'a.4cdn.org' return diff --git a/src/Miscellaneous/Report.coffee b/src/Miscellaneous/Report.coffee index 742e10ea7..ae6f96c6f 100755 --- a/src/Miscellaneous/Report.coffee +++ b/src/Miscellaneous/Report.coffee @@ -3,7 +3,7 @@ Report = init: -> return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ - Captcha.language.fixPage() unless Conf['Use Recaptcha v2 in Reports'] + Captcha.language.init() unless Conf['Use Recaptcha v2 in Reports'] @postID = +match[1] $.ready @ready diff --git a/src/Posting/Captcha.language.coffee b/src/Posting/Captcha.language.coffee index bd63efb72..92711d38a 100644 --- a/src/Posting/Captcha.language.coffee +++ b/src/Posting/Captcha.language.coffee @@ -1,12 +1,13 @@ Captcha.language = init: -> - return unless Conf['captchaLanguage'].trim() and d.cookie.indexOf('pass_enabled=1') < 0 and !Conf['Hide Original Post Form'] - $.onExists doc, '#captchaFormPart', true, (node) -> - $.onExists node, 'iframe', true, Captcha.language.fixIframe + return unless d.cookie.indexOf('pass_enabled=1') < 0 + return if location.hostname is 'boards.4chan.org' and Conf['Hide Original Post Form'] - fixPage: -> - return unless Conf['captchaLanguage'].trim() and d.cookie.indexOf('pass_enabled=1') < 0 - $.onExists doc, 'iframe', true, Captcha.language.fixIframe + if Conf['captchaLanguage'].trim() + if location.hostname is 'boards.4chan.org' + $.onExists doc, '#captchaFormPart', true, (node) -> $.onExists node, 'iframe', true, Captcha.language.fixIframe + else + $.onExists doc, 'iframe', true, Captcha.language.fixIframe fixIframe: (el) -> return unless lang = Conf['captchaLanguage'].trim() From 5dec4002674956ff85eb983881e6a7f9a1ad4a67 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 21:03:30 -0700 Subject: [PATCH 04/10] Captcha.language -> Captcha.replace --- src/General/Main.coffee | 4 ++-- src/Miscellaneous/Report.coffee | 2 +- .../{Captcha.language.coffee => Captcha.replace.coffee} | 8 ++++---- src/Posting/Captcha.v2.coffee | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) rename src/Posting/{Captcha.language.coffee => Captcha.replace.coffee} (78%) diff --git a/src/General/Main.coffee b/src/General/Main.coffee index 22027b52d..cc80cfe2c 100755 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -64,7 +64,7 @@ Main = switch location.hostname when 'www.4chan.org' - Captcha.language.init() + Captcha.replace.init() return when 'a.4cdn.org' return @@ -307,7 +307,7 @@ Main = features: [ ['Polyfill', Polyfill] - ['Captcha Language', Captcha.language] + ['Captcha Replacement', Captcha.replace] ['Redirect', Redirect] ['Header', Header] ['Catalog Links', CatalogLinks] diff --git a/src/Miscellaneous/Report.coffee b/src/Miscellaneous/Report.coffee index ae6f96c6f..2326bb3c4 100755 --- a/src/Miscellaneous/Report.coffee +++ b/src/Miscellaneous/Report.coffee @@ -3,7 +3,7 @@ Report = init: -> return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ - Captcha.language.init() unless Conf['Use Recaptcha v2 in Reports'] + Captcha.replace.init() unless Conf['Use Recaptcha v2 in Reports'] @postID = +match[1] $.ready @ready diff --git a/src/Posting/Captcha.language.coffee b/src/Posting/Captcha.replace.coffee similarity index 78% rename from src/Posting/Captcha.language.coffee rename to src/Posting/Captcha.replace.coffee index 92711d38a..110fc0e01 100644 --- a/src/Posting/Captcha.language.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -1,15 +1,15 @@ -Captcha.language = +Captcha.replace = init: -> return unless d.cookie.indexOf('pass_enabled=1') < 0 return if location.hostname is 'boards.4chan.org' and Conf['Hide Original Post Form'] if Conf['captchaLanguage'].trim() if location.hostname is 'boards.4chan.org' - $.onExists doc, '#captchaFormPart', true, (node) -> $.onExists node, 'iframe', true, Captcha.language.fixIframe + $.onExists doc, '#captchaFormPart', true, (node) -> $.onExists node, 'iframe', true, Captcha.replace.iframe else - $.onExists doc, 'iframe', true, Captcha.language.fixIframe + $.onExists doc, 'iframe', true, Captcha.replace.iframe - fixIframe: (el) -> + iframe: (el) -> return unless lang = Conf['captchaLanguage'].trim() src = if /[?&]hl=/.test el.src el.src.replace(/([?&]hl=)[^&]*/, '$1' + encodeURIComponent lang) diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index c3a67e675..8d1674c98 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -131,7 +131,7 @@ Captcha.v2 = return setupIFrame: (iframe) -> - Captcha.language.fixIframe iframe + Captcha.replace.iframe iframe $.addClass QR.nodes.el, 'captcha-open' if QR.nodes.el.getBoundingClientRect().bottom > doc.clientHeight QR.nodes.el.style.top = null From 12e44e0ca5dc66a5f99a620c63543eb1fa34e74c Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 21:15:39 -0700 Subject: [PATCH 05/10] Move 'Use Recaptcha v2 in Reports' into Captcha.replace. --- src/Miscellaneous/Report.coffee | 17 +---------------- src/Posting/Captcha.replace.coffee | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Miscellaneous/Report.coffee b/src/Miscellaneous/Report.coffee index 2326bb3c4..7ced583f5 100755 --- a/src/Miscellaneous/Report.coffee +++ b/src/Miscellaneous/Report.coffee @@ -3,15 +3,13 @@ Report = init: -> return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ - Captcha.replace.init() unless Conf['Use Recaptcha v2 in Reports'] + Captcha.replace.init() @postID = +match[1] $.ready @ready ready: -> $.addStyle Report.css Report.archive() if Conf['Archive Report'] - if Conf['Use Recaptcha v2 in Reports'] - Report.captchaV2() if Conf['Use Recaptcha v2 in Reports'] and $.hasClass doc, 'js-enabled' new MutationObserver(-> Report.fit '.gc-bubbleDefault').observe d.body, childList: true @@ -20,19 +18,6 @@ Report = else Report.fit 'body' - captchaV2: -> - return unless old = $.id 'captchaContainerAlt' - container = $.el 'div', - className: 'g-recaptcha' - container.dataset.sitekey = '<%= meta.recaptchaKey %>' - $.replace old, container - url = 'https://www.google.com/recaptcha/api.js' - if lang = Conf['captchaLanguage'].trim() - url += "?hl=#{encodeURIComponent lang}" - script = $.el 'script', - src: url - $.add d.head, script - fit: (selector) -> return unless el = $ selector, doc dy = el.getBoundingClientRect().bottom - doc.clientHeight + 8 diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index 110fc0e01..8a6f743fa 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -3,12 +3,29 @@ Captcha.replace = return unless d.cookie.indexOf('pass_enabled=1') < 0 return if location.hostname is 'boards.4chan.org' and Conf['Hide Original Post Form'] + if location.hostname is 'sys.4chan.org' and Conf['Use Recaptcha v2 in Reports'] + $.ready Captcha.replace.v2 + return + if Conf['captchaLanguage'].trim() if location.hostname is 'boards.4chan.org' $.onExists doc, '#captchaFormPart', true, (node) -> $.onExists node, 'iframe', true, Captcha.replace.iframe else $.onExists doc, 'iframe', true, Captcha.replace.iframe + v2: -> + return unless old = $.id 'captchaContainerAlt' + container = $.el 'div', + className: 'g-recaptcha' + container.dataset.sitekey = '<%= meta.recaptchaKey %>' + $.replace old, container + url = 'https://www.google.com/recaptcha/api.js' + if lang = Conf['captchaLanguage'].trim() + url += "?hl=#{encodeURIComponent lang}" + script = $.el 'script', + src: url + $.add d.head, script + iframe: (el) -> return unless lang = Conf['captchaLanguage'].trim() src = if /[?&]hl=/.test el.src From 7ea5bf79a1eb8887d922b7b7f23461f8b0a0eb67 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 22:19:55 -0700 Subject: [PATCH 06/10] Generalize Recaptcha.v1 script setup to something we can use outside the Quick Reply. --- src/Posting/Captcha.v1.coffee | 43 ++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index 2c6752726..961a7bfab 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -3,9 +3,7 @@ Captcha.v1 = return if d.cookie.indexOf('pass_enabled=1') >= 0 return unless @isEnabled = !!$.id 'g-recaptcha' - script = $.el 'script', - src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js' - $.add d.head, script + @setupScript() captchaContainer = $.el 'div', id: 'captchaContainer' hidden: true @@ -23,6 +21,7 @@ Captcha.v1 = @nodes = img: imgContainer.firstChild input: input + container: captchaContainer $.on input, 'blur', QR.focusout $.on input, 'focus', QR.focusin @@ -38,11 +37,32 @@ Captcha.v1 = QR.captcha.clear() $.sync 'captchas', @sync - new MutationObserver(@afterSetup).observe $.id('captchaContainer'), childList: true + new MutationObserver(@afterSetup).observe captchaContainer, childList: true @beforeSetup() @setup() if Conf['Auto-load captcha'] + setupScript: -> + return if @script + unless @script = $ 'script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]', d.head + @script = $.el 'script', + src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js' + $.add d.head, @script + $.globalEval ''' + document.addEventListener('captcha:setup', function(e) { + if (e.target.firstChild) return; + var options = {theme: "clean"}; + if (window.Recaptcha) { + window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); + } else { + var script = document.head.querySelector('script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]'); + script.addEventListener('load', function() { + window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); + }, false); + } + }, false); + ''' + cb: focus: -> QR.captcha.setup false, true @@ -67,20 +87,7 @@ Captcha.v1 = setup: (focus, force) -> return unless @isEnabled and (@needed() or force) - $.globalEval ''' - (function() { - var captchaContainer = document.getElementById("captchaContainer"); - if (captchaContainer.firstChild) return; - function setup() { - if (window.Recaptcha) { - Recaptcha.create(recaptchaKey, captchaContainer, {theme: "clean"}); - } else { - setTimeout(setup, 25); - } - } - setup(); - })() - ''' + $.event 'captcha:setup', null, @nodes.container @nodes.input.focus() if focus afterSetup: -> From 244d40cd9315989d93b01d6df261af66b08fa209 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 23:15:39 -0700 Subject: [PATCH 07/10] Implement replacement of captchas with v1 everywhere. --- src/General/Config.coffee | 9 ++++----- src/General/Main.coffee | 2 +- src/Posting/Captcha.noscript.coffee | 2 +- src/Posting/Captcha.replace.coffee | 19 ++++++++++++++++++- src/Posting/Captcha.v1.coffee | 2 +- src/Posting/Captcha.v2.coffee | 2 +- 6 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/General/Config.coffee b/src/General/Config.coffee index 894a16d7e..14c9727ce 100755 --- a/src/General/Config.coffee +++ b/src/General/Config.coffee @@ -456,11 +456,6 @@ Config = 'Show notifications on successful post creation or file uploading.' 1 ] - 'Use Recaptcha v1': [ - false - 'Use the old text version of Recaptcha.' - 1 - ] 'Force Noscript Captcha': [ false 'Use the non-Javascript fallback captcha in the QR even if Javascript is enabled.' @@ -485,6 +480,10 @@ Config = true 'Make captcha easier to use, especially with the keyboard.' ] + 'Use Recaptcha v1': [ + false + 'Use the old text version of Recaptcha.' + ] 'Use Recaptcha v2 in Reports': [ false 'Use the image selection captcha in the report window.' diff --git a/src/General/Main.coffee b/src/General/Main.coffee index cc80cfe2c..793e98958 100755 --- a/src/General/Main.coffee +++ b/src/General/Main.coffee @@ -59,7 +59,7 @@ Main = $.onExists doc, 'body', false, Main.initStyle initFeatures: -> - if location.hostname in ['boards.4chan.org', 'sys.4chan.org'] + if location.hostname in ['boards.4chan.org', 'sys.4chan.org', 'www.4chan.org'] $.globalEval 'document.documentElement.classList.add("js-enabled");' switch location.hostname diff --git a/src/Posting/Captcha.noscript.coffee b/src/Posting/Captcha.noscript.coffee index 6edd8d378..70cef6409 100644 --- a/src/Posting/Captcha.noscript.coffee +++ b/src/Posting/Captcha.noscript.coffee @@ -3,7 +3,7 @@ Captcha.noscript = init: -> return if d.cookie.indexOf('pass_enabled=1') >= 0 - return unless @isEnabled = !!$.id 'g-recaptcha' + return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' container = $.el 'div', className: 'captcha-img' diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index 8a6f743fa..1a2b9f6ee 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -3,16 +3,33 @@ Captcha.replace = return unless d.cookie.indexOf('pass_enabled=1') < 0 return if location.hostname is 'boards.4chan.org' and Conf['Hide Original Post Form'] - if location.hostname is 'sys.4chan.org' and Conf['Use Recaptcha v2 in Reports'] + jsEnabled = $.hasClass doc, 'js-enabled' + + if location.hostname is 'sys.4chan.org' and Conf['Use Recaptcha v2 in Reports'] and jsEnabled $.ready Captcha.replace.v2 return + if Conf['Use Recaptcha v1'] and jsEnabled + $.ready Captcha.replace.v1 + return + if Conf['captchaLanguage'].trim() if location.hostname is 'boards.4chan.org' $.onExists doc, '#captchaFormPart', true, (node) -> $.onExists node, 'iframe', true, Captcha.replace.iframe else $.onExists doc, 'iframe', true, Captcha.replace.iframe + v1: -> + return unless old = $.id 'g-recaptcha' + container = $.el 'div', + id: 'captchaContainerAlt' + $.replace old, container + Captcha.v1.setupScript() + if link = $ '#togglePostFormLink > a, #form-link' + $.on link, 'click', -> $.event 'captcha:setup', null, container + else + $.event 'captcha:setup', null, container + v2: -> return unless old = $.id 'captchaContainerAlt' container = $.el 'div', diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index 961a7bfab..afaa24e71 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -1,7 +1,7 @@ Captcha.v1 = init: -> return if d.cookie.indexOf('pass_enabled=1') >= 0 - return unless @isEnabled = !!$.id 'g-recaptcha' + return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' @setupScript() captchaContainer = $.el 'div', diff --git a/src/Posting/Captcha.v2.coffee b/src/Posting/Captcha.v2.coffee index 8d1674c98..ec5eb5989 100644 --- a/src/Posting/Captcha.v2.coffee +++ b/src/Posting/Captcha.v2.coffee @@ -3,7 +3,7 @@ Captcha.v2 = init: -> return if d.cookie.indexOf('pass_enabled=1') >= 0 - return unless @isEnabled = !!$.id 'g-recaptcha' + return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' if @noscript = Conf['Force Noscript Captcha'] or not $.hasClass doc, 'js-enabled' @conn = new Connection null, "#{location.protocol}//www.google.com", From 9e5daa700a80e8b3f843f7e930f130d8a7a4a957 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 23:28:03 -0700 Subject: [PATCH 08/10] Recaptcha v1 is not yet accepted on /feedback or /banned. --- src/Posting/Captcha.replace.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index 1a2b9f6ee..418efb6cb 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -9,7 +9,7 @@ Captcha.replace = $.ready Captcha.replace.v2 return - if Conf['Use Recaptcha v1'] and jsEnabled + if Conf['Use Recaptcha v1'] and jsEnabled and location.hostname isnt 'www.4chan.org' $.ready Captcha.replace.v1 return From f27b240b6e2d570f96900236fd14a2be00c008e2 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Mon, 22 Jun 2015 23:43:57 -0700 Subject: [PATCH 09/10] This ought to set the tabindex on the captcha, but for some reason it doesn't. --- src/Posting/Captcha.replace.coffee | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index 418efb6cb..d98aa709c 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -34,7 +34,9 @@ Captcha.replace = return unless old = $.id 'captchaContainerAlt' container = $.el 'div', className: 'g-recaptcha' - container.dataset.sitekey = '<%= meta.recaptchaKey %>' + $.extend container.dataset, + sitekey: '<%= meta.recaptchaKey %>' + tabindex: 3 $.replace old, container url = 'https://www.google.com/recaptcha/api.js' if lang = Conf['captchaLanguage'].trim() From 1a19b54c497dca17e18b82dbd60867060c8cf579 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Tue, 23 Jun 2015 01:06:09 -0700 Subject: [PATCH 10/10] Share captcha with original post form. --- src/Posting/Captcha.replace.coffee | 21 ++++++++++------- src/Posting/Captcha.v1.coffee | 37 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/Posting/Captcha.replace.coffee b/src/Posting/Captcha.replace.coffee index d98aa709c..e118edded 100644 --- a/src/Posting/Captcha.replace.coffee +++ b/src/Posting/Captcha.replace.coffee @@ -20,15 +20,20 @@ Captcha.replace = $.onExists doc, 'iframe', true, Captcha.replace.iframe v1: -> - return unless old = $.id 'g-recaptcha' - container = $.el 'div', - id: 'captchaContainerAlt' - $.replace old, container - Captcha.v1.setupScript() - if link = $ '#togglePostFormLink > a, #form-link' - $.on link, 'click', -> $.event 'captcha:setup', null, container + return unless $.id 'g-recaptcha' + Captcha.v1.replace() + if link = $.id 'form-link' + $.on link, 'click', -> Captcha.v1.create() + else if location.hostname is 'boards.4chan.org' + form = $.id 'postForm' + form.addEventListener 'focus', (-> Captcha.v1.create()), true + form.addEventListener 'blur', -> + $.queueTask -> + unless form.contains(document.activeElement) or $.id('qr')?.contains(document.activeElement) + Captcha.v1.destroy() + , true else - $.event 'captcha:setup', null, container + Captcha.v1.create() v2: -> return unless old = $.id 'captchaContainerAlt' diff --git a/src/Posting/Captcha.v1.coffee b/src/Posting/Captcha.v1.coffee index afaa24e71..a9d488b9a 100644 --- a/src/Posting/Captcha.v1.coffee +++ b/src/Posting/Captcha.v1.coffee @@ -3,12 +3,6 @@ Captcha.v1 = return if d.cookie.indexOf('pass_enabled=1') >= 0 return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt' - @setupScript() - captchaContainer = $.el 'div', - id: 'captchaContainer' - hidden: true - $.add d.body, captchaContainer - imgContainer = $.el 'div', className: 'captcha-img' title: 'Reload reCAPTCHA' @@ -21,7 +15,6 @@ Captcha.v1 = @nodes = img: imgContainer.firstChild input: input - container: captchaContainer $.on input, 'blur', QR.focusout $.on input, 'focus', QR.focusin @@ -37,30 +30,38 @@ Captcha.v1 = QR.captcha.clear() $.sync 'captchas', @sync - new MutationObserver(@afterSetup).observe captchaContainer, childList: true - @beforeSetup() @setup() if Conf['Auto-load captcha'] + new MutationObserver(@afterSetup).observe $.id('captchaContainerAlt'), childList: true + @afterSetup() # reCAPTCHA might have loaded before the QR. - setupScript: -> + replace: -> return if @script unless @script = $ 'script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]', d.head @script = $.el 'script', src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js' $.add d.head, @script + if old = $.id 'g-recaptcha' + container = $.el 'div', + id: 'captchaContainerAlt' + $.replace old, container + + create: -> + @replace() $.globalEval ''' - document.addEventListener('captcha:setup', function(e) { - if (e.target.firstChild) return; + (function() { + var container = document.getElementById("captchaContainerAlt"); + if (container.firstChild) return; var options = {theme: "clean"}; if (window.Recaptcha) { - window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); + window.Recaptcha.create("<%= meta.recaptchaKey %>", container, options); } else { var script = document.head.querySelector('script[src="//www.google.com/recaptcha/api/js/recaptcha_ajax.js"]'); script.addEventListener('load', function() { - window.Recaptcha.create("<%= meta.recaptchaKey %>", e.target, options); + window.Recaptcha.create("<%= meta.recaptchaKey %>", container, options); }, false); } - }, false); + })(); ''' cb: @@ -87,7 +88,7 @@ Captcha.v1 = setup: (focus, force) -> return unless @isEnabled and (@needed() or force) - $.event 'captcha:setup', null, @nodes.container + @create() @nodes.input.focus() if focus afterSetup: -> @@ -117,9 +118,9 @@ Captcha.v1 = QR.nodes.el.style.bottom = '0px' destroy: -> - return unless @isEnabled + return unless @script $.globalEval 'Recaptcha.destroy()' - @beforeSetup() + @beforeSetup() if @nodes sync: (captchas=[]) -> QR.captcha.captchas = captchas