qr part 1

This commit is contained in:
James Campos 2011-04-24 15:08:56 -07:00
parent 59633ea1dd
commit b46577756e
2 changed files with 81 additions and 28 deletions

View File

@ -230,6 +230,14 @@
return object;
};
$.extend($, {
globalEval: function(code) {
var script;
script = $.el('script', {
textContent: "(" + code + ")()"
});
$.append(d.head, script);
return $.remove(script);
},
get: function(url, cb) {
var r;
r = new XMLHttpRequest();
@ -931,8 +939,9 @@
name: 'iframe'
});
$.append(d.body, iframe);
$.bind(iframe, 'load', qr.cb.load);
$.bind(window, 'message', qr.cb.messageTop);
$.globalEval(qr.eval.load);
$.globalEval(qr.eval.messageTop);
$.bind(window, 'message2', qr.cb.messageTop);
return $('#recaptcha_response_field').id = '';
},
autohide: {
@ -945,6 +954,35 @@
return (_ref = $('#qr input[title=autohide]:checked')) != null ? _ref.click() : void 0;
}
},
eval: {
load: function() {
var load;
load = function(e) {
return e.target.contentWindow.postMessage('', '*');
};
return document.querySelector('iframe[name=iframe]').addEventListener('load', load, true);
},
messageTop: function() {
var message;
message = function(e) {
var e2;
e2 = document.createEvent('MessageEvent');
e2.initMessageEvent('message2', true, true, e.data, e.origin, '', null, null);
return window.dispatchEvent(e2);
};
return window.addEventListener('message', message, true);
},
messageIframe: function() {
var messageIframe;
messageIframe = function(e) {
var message, _ref;
message = ((_ref = document.querySelector('table font b')) != null ? _ref.firstChild.textContent : void 0) || '';
e.source.postMessage(message, '*');
return window.location = 'about:blank';
};
return window.addEventListener('message', messageIframe, true);
}
},
cb: {
autohide: function(e) {
var dialog;
@ -958,12 +996,6 @@
load: function(e) {
return e.target.contentWindow.postMessage('', '*');
},
messageIframe: function(e) {
var message, _ref;
message = ((_ref = $('table font b')) != null ? _ref.firstChild.textContent : void 0) || '';
e.source.postMessage(message, '*');
return window.location = 'about:blank';
},
messageTop: function(e) {
var data, dialog, error;
data = e.data;
@ -1134,17 +1166,19 @@
return qr.autohide.set();
},
sys: function() {
var board, html, id, recaptcha, thread, _, _ref, _ref2;
var board, c, id, recaptcha, thread, _, _ref, _ref2;
if (recaptcha = $('#recaptcha_response_field')) {
$.bind(recaptcha, 'keydown', Recaptcha.listener);
return;
}
$.bind(window, 'message', qr.cb.messageIframe);
html = $('b').innerHTML;
_ref = html.match(/<!-- thread:(\d+),no:(\d+) -->/), _ = _ref[0], thread = _ref[1], id = _ref[2];
if (thread === '0') {
_ref2 = $('meta', d).content.match(/4chan.org\/(\w+)\//), _ = _ref2[0], board = _ref2[1];
return window.location = "http://boards.4chan.org/" + board + "/res/" + id + "#watch";
$.globalEval(qr.eval.messageIframe);
c = $('b').lastChild;
if (c.nodeType === 8) {
_ref = c.textContent.match(/thread:(\d+),no:(\d+)/), _ = _ref[0], thread = _ref[1], id = _ref[2];
if (thread === '0') {
_ref2 = $('meta', d).content.match(/4chan.org\/(\w+)\//), _ = _ref2[0], board = _ref2[1];
return window.location = "http://boards.4chan.org/" + board + "/res/" + id + "#watch";
}
}
}
};

View File

@ -137,6 +137,11 @@ $.extend = (object, properties) ->
object
$.extend $,
globalEval: (code) ->
script = $.el 'script',
textContent: "(#{code})()"
$.append d.head, script
$.remove script
get: (url, cb) ->
r = new XMLHttpRequest()
r.onload = cb
@ -705,8 +710,9 @@ qr =
iframe = $.el 'iframe',
name: 'iframe'
$.append d.body, iframe
$.bind iframe, 'load', qr.cb.load
$.bind window, 'message', qr.cb.messageTop
$.globalEval qr.eval.load
$.globalEval qr.eval.messageTop
$.bind window, 'message2', qr.cb.messageTop
#hack - nuke id so it doesn't grab focus when reloading
$('#recaptcha_response_field').id = ''
@ -717,6 +723,23 @@ qr =
unset: ->
$('#qr input[title=autohide]:checked')?.click()
eval:
load: ->
load = (e) -> e.target.contentWindow.postMessage '', '*'
document.querySelector('iframe[name=iframe]').addEventListener('load', load, true)
messageTop: ->
message = (e) ->
e2 = document.createEvent 'MessageEvent'
# XXX using e.source for source arg results in security error in Firefox
e2.initMessageEvent 'message2', true, true, e.data, e.origin, '', null, null
window.dispatchEvent e2
window.addEventListener 'message', message, true
messageIframe: ->
messageIframe = (e) ->
message = document.querySelector('table font b')?.firstChild.textContent or ''
e.source.postMessage message, '*'
window.location = 'about:blank'
window.addEventListener 'message', messageIframe, true
cb:
autohide: (e) ->
dialog = $ '#qr'
@ -728,11 +751,6 @@ qr =
load: (e) ->
e.target.contentWindow.postMessage '', '*'
messageIframe: (e) ->
message = $('table font b')?.firstChild.textContent or ''
e.source.postMessage message, '*'
window.location = 'about:blank'
messageTop: (e) ->
{data} = e
dialog = $ '#qr'
@ -883,13 +901,14 @@ qr =
$.bind recaptcha, 'keydown', Recaptcha.listener
return
$.bind window, 'message', qr.cb.messageIframe
$.globalEval qr.eval.messageIframe
html = $('b').innerHTML
[_, thread, id] = html.match(/<!-- thread:(\d+),no:(\d+) -->/)
if thread is '0'
[_, board] = $('meta', d).content.match(/4chan.org\/(\w+)\//)
window.location = "http://boards.4chan.org/#{board}/res/#{id}#watch"
c = $('b').lastChild
if c.nodeType is 8 #comment node
[_, thread, id] = c.textContent.match(/thread:(\d+),no:(\d+)/)
if thread is '0'
[_, board] = $('meta', d).content.match(/4chan.org\/(\w+)\//)
window.location = "http://boards.4chan.org/#{board}/res/#{id}#watch"
threadHiding =
init: ->