diff --git a/4chan_x.user.js b/4chan_x.user.js index 2cce6f03c..cfbfb820d 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1638,29 +1638,31 @@ }, message: { init: function() { - var code; + var code, script; code = function(e) { - var data, host, i, o, u; + var data, host; data = e.data; if (!data.changeContext) return; delete data.changeContext; host = location.hostname; - o = {}; - for (i in data) { - u = data[i]; - o[i] = u; - } if (host === 'boards.4chan.org') { - return document.getElementById('iframe').contentWindow.postMessage(o, '*'); + return document.getElementById('iframe').contentWindow.postMessage(data, '*'); } else if (host === 'sys.4chan.org') { - return parent.postMessage(o, '*'); + return parent.postMessage(data, '*'); } }; - if (engine === 'gecko') { - return $.on(window, 'message', code); - } else { - return window.location = "javascript:window.addEventListener('message'," + code + ",false)"; + script = $.el('script', { + textContent: "window.addEventListener('message'," + code + ",false)" + }); + if (d.documentElement) { + $.add(d.documentElement, script); + $.rm(script); + return; } + return $.ready(function() { + $.add(d.head, script); + return $.rm(script); + }); }, send: function(data) { data.changeContext = true; diff --git a/script.coffee b/script.coffee index 18acbd8a7..6b98cc130 100644 --- a/script.coffee +++ b/script.coffee @@ -1262,18 +1262,20 @@ qr = return unless data.changeContext delete data.changeContext host = location.hostname - o = {} - for i, u of data - o[i] = u if host is 'boards.4chan.org' - document.getElementById('iframe').contentWindow.postMessage o, '*' + document.getElementById('iframe').contentWindow.postMessage data, '*' else if host is 'sys.4chan.org' - parent.postMessage o, '*' - # do not wait for 4chan to load - if engine is 'gecko' - $.on window, 'message', code - else - window.location = "javascript:window.addEventListener('message',#{code},false)" + parent.postMessage data, '*' + script = $.el 'script', textContent: "window.addEventListener('message',#{code},false)" + # Chrome can access the documentElement on document-start + if d.documentElement + $.add d.documentElement, script + $.rm script + return + # other browsers will have to wait + $.ready -> + $.add d.head, script + $.rm script send: (data) -> data.changeContext = true data.qr = true