diff --git a/4chan_x.user.js b/4chan_x.user.js index a817538ec..6ce2579c2 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1222,26 +1222,34 @@ $.before(form, link); } g.callbacks.push(this.node); - iframe = $.el('iframe', { - id: 'iframe', - hidden: true, - src: 'http://sys.4chan.org/robots.txt' - }); - $.on(iframe, 'error', function() { - return this.src = this.src; - }); - loadChecking = function(iframe) { - if (!qr.status.ready) { - iframe.src = 'about:blank'; - return setTimeout((function() { - return iframe.src = 'http://sys.4chan.org/robots.txt'; - }), 250); - } - }; - $.on(iframe, 'load', function() { - if (this.src !== 'about:blank') return setTimeout(loadChecking, 500, this); - }); - $.add(d.body, iframe); + if (engine === 'webkit') { + qr.status({ + ready: true + }); + } else { + iframe = $.el('iframe', { + id: 'iframe', + hidden: true, + src: 'http://sys.4chan.org/robots.txt' + }); + $.on(iframe, 'error', function() { + return this.src = this.src; + }); + loadChecking = function(iframe) { + if (!qr.status.ready) { + iframe.src = 'about:blank'; + return setTimeout((function() { + return iframe.src = 'http://sys.4chan.org/robots.txt'; + }), 100); + } + }; + $.on(iframe, 'load', function() { + if (this.src !== 'about:blank') { + return setTimeout(loadChecking, 500, this); + } + }); + $.add(d.body, iframe); + } window.location = 'javascript:void(Recaptcha.focus_response_field=function(){})'; if (conf['Persistent QR']) { qr.dialog(); @@ -1738,7 +1746,6 @@ qr.status(); qr.cooldown.init(); qr.captcha.init(); - qr.message.init(); $.add(d.body, qr.el); e = d.createEvent('CustomEvent'); e.initEvent('QRDialogCreation', true, false); @@ -1817,6 +1824,10 @@ reader.readAsBinaryString(reply.file); return; } + if (engine === 'webkit') { + qr.message.post(post); + return; + } return qr.message.send(post); }, response: function(html) { @@ -1870,43 +1881,16 @@ return qr.resetFileInput(); }, message: { - init: function() { - var code, ready, script; - code = function(e) { - var data, host; - data = e.data; - if (!data.changeContext) return; - delete data.changeContext; - host = location.hostname; - if (host === 'boards.4chan.org') { - return document.getElementById('iframe').contentWindow.postMessage(data, '*'); - } else if (host === 'sys.4chan.org') { - return parent.postMessage(data, '*'); - } - }; - script = $.el('script', { - textContent: "window.addEventListener('message'," + code + ",false)" - }); - ready = function() { - $.add(d.documentElement, script); - if (location.hostname === 'sys.4chan.org') { - qr.message.send({ - req: 'status', - ready: true - }); - } - return $.rm(script); - }; - if (d.documentElement) { - return ready(); - } else { - return $.ready(ready); - } - }, send: function(data) { - data.changeContext = true; + var host, window; + if (engine === 'webkit') { + qr.message.receive(data); + return; + } data.qr = true; - return postMessage(data, '*'); + host = location.hostname; + window = host === 'boards.4chan.org' ? $.id('iframe').contentWindow : host === 'sys.4chan.org' ? parent : void 0; + return window.postMessage(data, '*'); }, receive: function(data) { var _ref; @@ -3349,7 +3333,7 @@ var src, thumb; if (root.hidden || !(thumb = $('img[md5]', root))) return; src = thumb.parentNode.href; - if (/gif$/.test(src)) return thumb.src = src; + if (/gif$/.test(src && !/^Spoiler/.test(thumb.alt))) return thumb.src = src; } }; @@ -3505,7 +3489,10 @@ $.on(window, 'message', Main.message); if (location.hostname === 'sys.4chan.org') { if (location.pathname === '/robots.txt') { - qr.message.init(); + qr.message.send({ + req: 'status', + ready: true + }); } else if (/report/.test(location.search)) { $.ready(function() { return $.on($('#recaptcha_response_field'), 'keydown', function(e) { @@ -3624,10 +3611,12 @@ message: function(e) { var data, version; data = e.data; + if (data.qr) { + qr.message.receive(data); + return; + } version = data.version; - if (data.qr && !data.changeContext) { - return qr.message.receive(data); - } else if (version && version !== VERSION && confirm('An updated version of 4chan X is available, would you like to install it now?')) { + if (version && version !== VERSION && confirm('An updated version of 4chan X is available, would you like to install it now?')) { return window.location = "https://raw.github.com/mayhemydg/4chan-x/" + version + "/4chan_x.user.js"; } }, diff --git a/script.coffee b/script.coffee index 91a590adb..00677a70a 100644 --- a/script.coffee +++ b/script.coffee @@ -900,18 +900,21 @@ qr = $.before form, link g.callbacks.push @node - iframe = $.el 'iframe', - id: 'iframe' - hidden: true - src: 'http://sys.4chan.org/robots.txt' - $.on iframe, 'error', -> @src = @src - # Greasemonkey ghetto fix - loadChecking = (iframe) -> - unless qr.status.ready - iframe.src = 'about:blank' - setTimeout (-> iframe.src = 'http://sys.4chan.org/robots.txt'), 250 - $.on iframe, 'load', -> unless @src is 'about:blank' then setTimeout loadChecking, 500, @ - $.add d.body, iframe + if engine is 'webkit' + qr.status ready: true + else + iframe = $.el 'iframe', + id: 'iframe' + hidden: true + src: 'http://sys.4chan.org/robots.txt' + $.on iframe, 'error', -> @src = @src + # Greasemonkey ghetto fix + loadChecking = (iframe) -> + unless qr.status.ready + iframe.src = 'about:blank' + setTimeout (-> iframe.src = 'http://sys.4chan.org/robots.txt'), 100 + $.on iframe, 'load', -> if @src isnt 'about:blank' then setTimeout loadChecking, 500, @ + $.add d.body, iframe # Prevent original captcha input from being focused on reload. window.location = 'javascript:void(Recaptcha.focus_response_field=function(){})' @@ -1309,7 +1312,6 @@ qr = qr.status() qr.cooldown.init() qr.captcha.init() - qr.message.init() $.add d.body, qr.el # Create a custom event when the QR dialog is first initialized. @@ -1397,6 +1399,9 @@ qr = reader.readAsBinaryString reply.file return + if engine is 'webkit' + qr.message.post post + return qr.message.send post response: (html) -> @@ -1450,35 +1455,18 @@ qr = qr.resetFileInput() message: - init: -> - # http://code.google.com/p/chromium/issues/detail?id=20773 - # Let content scripts see other frames (instead of them being undefined) - # To access the parent, we have to break out of the sandbox and evaluate - # in the global context. - code = (e) -> - {data} = e - return unless data.changeContext - delete data.changeContext - host = location.hostname - if host is 'boards.4chan.org' - document.getElementById('iframe').contentWindow.postMessage data, '*' - else if host is 'sys.4chan.org' - parent.postMessage data, '*' - script = $.el 'script', textContent: "window.addEventListener('message',#{code},false)" - ready = -> - $.add d.documentElement, script - if location.hostname is 'sys.4chan.org' - qr.message.send req: 'status', ready: true - $.rm script - # Chrome can access the documentElement on document-start - if d.documentElement - ready() - # other browsers will have to wait - else $.ready ready send: (data) -> - data.changeContext = true - data.qr = true - postMessage data, '*' + if engine is 'webkit' + qr.message.receive data + return + data.qr = true + host = location.hostname + window = + if host is 'boards.4chan.org' + $.id('iframe').contentWindow + else if host is 'sys.4chan.org' + parent + window.postMessage data, '*' receive: (data) -> switch data.req when 'abort' @@ -2764,7 +2752,7 @@ Main = if location.hostname is 'sys.4chan.org' if location.pathname is '/robots.txt' - qr.message.init() + qr.message.send req: 'status', ready: true else if /report/.test location.search $.ready -> $.on $('#recaptcha_response_field'), 'keydown', (e) -> @@ -2928,10 +2916,11 @@ Main = message: (e) -> {data} = e - {version} = data - if data.qr and not data.changeContext + if data.qr qr.message.receive data - else if version and version isnt VERSION and confirm 'An updated version of 4chan X is available, would you like to install it now?' + return + {version} = data + if version and version isnt VERSION and confirm 'An updated version of 4chan X is available, would you like to install it now?' window.location = "https://raw.github.com/mayhemydg/4chan-x/#{version}/4chan_x.user.js" node: (nodes, notify) ->