diff --git a/4chan_x.user.js b/4chan_x.user.js index 81aec7d17..9f1e668b2 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1494,7 +1494,7 @@ } }); qr.mimeTypes = mimeTypes.split(', '); - qr.el = ui.dialog('qr', 'top:0;right:0;', "
Quick Reply " + (g.REPLY ? '' : threads) + " x
+
Spoiler Image?
"); + qr.el = ui.dialog('qr', 'top:0;right:0;', "
Quick Reply " + (g.REPLY ? '' : threads) + " x
+
Spoiler Image?
"); if (!g.REPLY) { $.on($('select', qr.el), 'mousedown', function(e) { return e.stopPropagation(); @@ -1525,6 +1525,7 @@ }); } qr.captcha.init(); + qr.message.init(); return $.add(d.body, qr.el); }, submit: function(e) { @@ -1547,6 +1548,36 @@ if (conf['Remember Subject']) $.set("qr_sub", reply.sub); if (qr.replies.length === 1) new qr.reply().select(); return reply.rm(); + }, + message: { + init: function() { + var code, 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)" + }); + $.add(d.documentElement, script); + return $.rm(script); + }, + send: function(data) { + data.changeContext = true; + data.qr = true; + return postMessage(data, '*'); + }, + receive: function(data) { + return log('receive', location.hostname, data); + } } }; @@ -3051,18 +3082,19 @@ } else { g.PAGENUM = parseInt(temp) || 0; } + $.on(window, 'message', Main.message); if (location.hostname === 'sys.4chan.org') { - $.ready(function() { - if (/report/.test(location.search)) { + if (location.pathname === '/post') { + qr.message.init(); + } else if (/report/.test(location.search)) { + $.ready(function() { return $.on($('#recaptcha_response_field'), 'keydown', function(e) { if (e.keyCode === 8 && !e.target.value) { return window.location = 'javascript:Recaptcha.reload()'; } }); - } else { - - } - }); + }); + } return; } if (location.hostname === 'images.4chan.org') { @@ -3072,7 +3104,6 @@ return; } $.ready(options.init); - $.on(window, 'message', Main.message); now = Date.now(); if (conf['Check for Updates'] && $.get('lastUpdate', 0) < now - 6 * HOUR) { $.ready(function() { @@ -3159,9 +3190,12 @@ return $.on($('form[name=delform]'), 'DOMNodeInserted', Main.node); }, message: function(e) { - var version; - version = e.data.version; - if (version && version !== VERSION && confirm('An updated version of 4chan X is available, would you like to install it now?')) { + var data, version; + data = e.data; + 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?')) { return window.location = "https://raw.github.com/mayhemydg/4chan-x/" + version + "/4chan_x.user.js"; } }, diff --git a/script.coffee b/script.coffee index 33b280ed1..9d16c396d 100644 --- a/script.coffee +++ b/script.coffee @@ -1125,7 +1125,8 @@ qr =
Spoiler Image?
-" + +" unless g.REPLY $.on $('select', qr.el), 'mousedown', (e) -> e.stopPropagation() $.on $('#autohide', qr.el), 'click', qr.hide @@ -1147,6 +1148,7 @@ qr = # qr.inputs[match[1]].value = JSON.parse e.newValue qr.captcha.init() + qr.message.init() $.add d.body, qr.el submit: (e) -> @@ -1176,6 +1178,32 @@ qr = new qr.reply().select() if qr.replies.length is 1 reply.rm() + 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)" + $.add d.documentElement, script + $.rm script + send: (data) -> + data.changeContext = true + data.qr = true + postMessage data, '*' + receive: (data) -> + log 'receive', location.hostname, data + options = init: -> home = $ '#navtopr a' @@ -2333,13 +2361,15 @@ Main = else g.PAGENUM = parseInt(temp) or 0 + $.on window, 'message', Main.message + if location.hostname is 'sys.4chan.org' - $.ready -> - if /report/.test location.search + if location.pathname is '/post' + qr.message.init() + else if /report/.test location.search + $.ready -> $.on $('#recaptcha_response_field'), 'keydown', (e) -> window.location = 'javascript:Recaptcha.reload()' if e.keyCode is 8 and not e.target.value - else - # posting return if location.hostname is 'images.4chan.org' $.ready -> redirect.init() if d.title is '4chan - 404' @@ -2347,8 +2377,6 @@ Main = $.ready options.init - $.on window, 'message', Main.message - now = Date.now() if conf['Check for Updates'] and $.get('lastUpdate', 0) < now - 6*HOUR $.ready -> $.add d.head, $.el 'script', src: 'https://raw.github.com/mayhemydg/4chan-x/master/latest.js' @@ -2486,8 +2514,11 @@ Main = $.on $('form[name=delform]'), 'DOMNodeInserted', Main.node message: (e) -> - {version} = e.data - if version and version isnt VERSION and confirm 'An updated version of 4chan X is available, would you like to install it now?' + {data} = e + {version} = data + if data.qr and not data.changeContext + 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?' window.location = "https://raw.github.com/mayhemydg/4chan-x/#{version}/4chan_x.user.js" node: (e) ->