Merge branch 'v1-original'

This commit is contained in:
ccd0 2015-06-28 15:44:49 -07:00
commit 4b39229888
8 changed files with 110 additions and 78 deletions

View File

@ -456,11 +456,6 @@ Config =
'Show notifications on successful post creation or file uploading.' 'Show notifications on successful post creation or file uploading.'
1 1
] ]
'Use Recaptcha v1': [
false
'Use the old text version of Recaptcha.'
1
]
'Force Noscript Captcha': [ 'Force Noscript Captcha': [
false false
'Use the non-Javascript fallback captcha in the QR even if Javascript is enabled.' 'Use the non-Javascript fallback captcha in the QR even if Javascript is enabled.'
@ -485,6 +480,10 @@ Config =
true true
'Make captcha easier to use, especially with the keyboard.' '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': [ 'Use Recaptcha v2 in Reports': [
false false
'Use the image selection captcha in the report window.' 'Use the image selection captcha in the report window.'

View File

@ -15,14 +15,6 @@ Main =
$.ready -> Captcha.fixes.init() $.ready -> Captcha.fixes.init()
return 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.threads = new SimpleDict()
g.posts = new SimpleDict() g.posts = new SimpleDict()
@ -71,10 +63,13 @@ Main =
$.onExists doc, 'body', false, Main.initStyle $.onExists doc, 'body', false, Main.initStyle
initFeatures: -> 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");' $.globalEval 'document.documentElement.classList.add("js-enabled");'
switch location.hostname switch location.hostname
when 'www.4chan.org'
Captcha.replace.init()
return
when 'a.4cdn.org' when 'a.4cdn.org'
return return
when 'sys.4chan.org' when 'sys.4chan.org'
@ -123,7 +118,10 @@ Main =
$.ready Main.initReady $.ready Main.initReady
initStyle: -> initStyle: ->
$.addStyle Main.cssWWW if location.hostname is 'www.4chan.org'
return if !Main.isThisPageLegit() or $.hasClass doc, 'fourchan-x' return if !Main.isThisPageLegit() or $.hasClass doc, 'fourchan-x'
# disable the mobile layout # disable the mobile layout
$('link[href*=mobile]', d.head)?.disabled = true $('link[href*=mobile]', d.head)?.disabled = true
$.addClass doc, 'fourchan-x', 'seaweedchan' $.addClass doc, 'fourchan-x', 'seaweedchan'
@ -313,7 +311,7 @@ Main =
features: [ features: [
['Polyfill', Polyfill] ['Polyfill', Polyfill]
['Captcha Language', Captcha.language] ['Captcha Replacement', Captcha.replace]
['Redirect', Redirect] ['Redirect', Redirect]
['Header', Header] ['Header', Header]
['Catalog Links', CatalogLinks] ['Catalog Links', CatalogLinks]

View File

@ -3,15 +3,13 @@ Report =
init: -> init: ->
return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/ return unless /\bmode=report\b/.test(location.search) and match = location.search.match /\bno=(\d+)/
Captcha.language.fixPage() Captcha.replace.init()
@postID = +match[1] @postID = +match[1]
$.ready @ready $.ready @ready
ready: -> ready: ->
$.addStyle Report.css $.addStyle Report.css
Report.archive() if Conf['Archive Report'] 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' if Conf['Use Recaptcha v2 in Reports'] and $.hasClass doc, 'js-enabled'
new MutationObserver(-> Report.fit '.gc-bubbleDefault').observe d.body, new MutationObserver(-> Report.fit '.gc-bubbleDefault').observe d.body,
childList: true childList: true
@ -20,19 +18,6 @@ Report =
else else
Report.fit 'body' 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) -> fit: (selector) ->
return unless el = $ selector, doc return unless el = $ selector, doc
dy = el.getBoundingClientRect().bottom - doc.clientHeight + 8 dy = el.getBoundingClientRect().bottom - doc.clientHeight + 8

View File

@ -1,17 +0,0 @@
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
fixPage: ->
return unless Conf['captchaLanguage'].trim() and d.cookie.indexOf('pass_enabled=1') < 0
$.onExists doc, 'iframe', true, Captcha.language.fixIframe
fixIframe: (el) ->
return unless lang = Conf['captchaLanguage'].trim()
src = if /[?&]hl=/.test el.src
el.src.replace(/([?&]hl=)[^&]*/, '$1' + encodeURIComponent lang)
else
el.src + "&hl=#{encodeURIComponent lang}"
el.src = src unless el.src is src

View File

@ -3,7 +3,7 @@ Captcha.noscript =
init: -> init: ->
return if d.cookie.indexOf('pass_enabled=1') >= 0 return if d.cookie.indexOf('pass_enabled=1') >= 0
return unless @isEnabled = !!$.id 'g-recaptcha' return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt'
container = $.el 'div', container = $.el 'div',
className: 'captcha-img' className: 'captcha-img'

View File

@ -0,0 +1,59 @@
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']
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 and location.hostname isnt 'www.4chan.org'
$.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 $.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
Captcha.v1.create()
v2: ->
return unless old = $.id 'captchaContainerAlt'
container = $.el 'div',
className: 'g-recaptcha'
$.extend container.dataset,
sitekey: '<%= meta.recaptchaKey %>'
tabindex: 3
$.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
el.src.replace(/([?&]hl=)[^&]*/, '$1' + encodeURIComponent lang)
else
el.src + "&hl=#{encodeURIComponent lang}"
el.src = src unless el.src is src

View File

@ -1,15 +1,7 @@
Captcha.v1 = Captcha.v1 =
init: -> init: ->
return if d.cookie.indexOf('pass_enabled=1') >= 0 return if d.cookie.indexOf('pass_enabled=1') >= 0
return unless @isEnabled = !!$.id 'g-recaptcha' return unless @isEnabled = !!$ '#g-recaptcha, #captchaContainerAlt'
script = $.el 'script',
src: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js'
$.add d.head, script
captchaContainer = $.el 'div',
id: 'captchaContainer'
hidden: true
$.add d.body, captchaContainer
imgContainer = $.el 'div', imgContainer = $.el 'div',
className: 'captcha-img' className: 'captcha-img'
@ -38,10 +30,39 @@ Captcha.v1 =
QR.captcha.clear() QR.captcha.clear()
$.sync 'captchas', @sync $.sync 'captchas', @sync
new MutationObserver(@afterSetup).observe $.id('captchaContainer'), childList: true
@beforeSetup() @beforeSetup()
@setup() if Conf['Auto-load captcha'] @setup() if Conf['Auto-load captcha']
new MutationObserver(@afterSetup).observe $.id('captchaContainerAlt'), childList: true
@afterSetup() # reCAPTCHA might have loaded before the QR.
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 '''
(function() {
var container = document.getElementById("captchaContainerAlt");
if (container.firstChild) return;
var options = {theme: "clean"};
if (window.Recaptcha) {
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 %>", container, options);
}, false);
}
})();
'''
cb: cb:
focus: -> QR.captcha.setup false, true focus: -> QR.captcha.setup false, true
@ -67,20 +88,7 @@ Captcha.v1 =
setup: (focus, force) -> setup: (focus, force) ->
return unless @isEnabled and (@needed() or force) return unless @isEnabled and (@needed() or force)
$.globalEval ''' @create()
(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();
})()
'''
@nodes.input.focus() if focus @nodes.input.focus() if focus
afterSetup: -> afterSetup: ->
@ -110,9 +118,9 @@ Captcha.v1 =
QR.nodes.el.style.bottom = '0px' QR.nodes.el.style.bottom = '0px'
destroy: -> destroy: ->
return unless @isEnabled return unless @script
$.globalEval 'Recaptcha.destroy()' $.globalEval 'Recaptcha.destroy()'
@beforeSetup() @beforeSetup() if @nodes
sync: (captchas=[]) -> sync: (captchas=[]) ->
QR.captcha.captchas = captchas QR.captcha.captchas = captchas

View File

@ -3,7 +3,7 @@ Captcha.v2 =
init: -> init: ->
return if d.cookie.indexOf('pass_enabled=1') >= 0 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' if @noscript = Conf['Force Noscript Captcha'] or not $.hasClass doc, 'js-enabled'
@conn = new Connection null, "#{location.protocol}//www.google.com", @conn = new Connection null, "#{location.protocol}//www.google.com",
@ -136,7 +136,7 @@ Captcha.v2 =
return return
setupIFrame: (iframe) -> setupIFrame: (iframe) ->
Captcha.language.fixIframe iframe Captcha.replace.iframe iframe
$.addClass QR.nodes.el, 'captcha-open' $.addClass QR.nodes.el, 'captcha-open'
if QR.nodes.el.getBoundingClientRect().bottom > doc.clientHeight if QR.nodes.el.getBoundingClientRect().bottom > doc.clientHeight
QR.nodes.el.style.top = null QR.nodes.el.style.top = null