From c93a996ec025d6179ab04e1af6b6e17c03147862 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Tue, 19 Jun 2012 16:25:57 +0200 Subject: [PATCH] $.event --- 4chan_x.user.js | 22 +++++++++++----------- script.coffee | 16 +++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 050ccaad1..f6c47540e 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -466,6 +466,12 @@ open: function(url) { return (GM_openInTab || window.open)(location.protocol + url, '_blank'); }, + event: function(el, name, type) { + var e; + e = d.createEvent(type || 'CustomEvent'); + e.initEvent(name, true, false); + return el.dispatchEvent(e); + }, globalEval: function(code) { var script; script = $.el('script', { @@ -1265,16 +1271,14 @@ return key; }, tags: function(tag, ta) { - var e, range, selEnd, selStart, value; + var range, selEnd, selStart, value; value = ta.value; selStart = ta.selectionStart; selEnd = ta.selectionEnd; ta.value = value.slice(0, selStart) + ("[" + tag + "]") + value.slice(selStart, selEnd) + ("[/" + tag + "]") + value.slice(selEnd); range = ("[" + tag + "]").length + selEnd; ta.setSelectionRange(range, range); - e = d.createEvent('Event'); - e.initEvent('input', true, false); - return ta.dispatchEvent(e); + return $.event(ta, 'input', 'Event'); }, img: function(thread, all) { var thumb; @@ -1584,9 +1588,7 @@ range += text.match(/\n/g).length; } ta.setSelectionRange(range, range); - e = d.createEvent('Event'); - e.initEvent('input', true, false); - return ta.dispatchEvent(e); + return $.event(ta, 'input', 'Event'); }, drag: function(e) { var i; @@ -1931,7 +1933,7 @@ } }, dialog: function() { - var e, fileInput, id, mimeTypes, name, spoiler, ta, thread, threads, _i, _j, _len, _len1, _ref, _ref1; + var fileInput, id, mimeTypes, name, spoiler, ta, thread, threads, _i, _j, _len, _len1, _ref, _ref1; QR.el = UI.dialog('qr', 'top:0;right:0;', '\
\ Quick Reply \ @@ -2028,9 +2030,7 @@ QR.cooldown.init(); QR.captcha.init(); $.add(d.body, QR.el); - e = d.createEvent('CustomEvent'); - e.initEvent('QRDialogCreation', true, false); - return QR.el.dispatchEvent(e); + return $.event(QR.el, 'QRDialogCreation'); }, submit: function(e) { var callbacks, captcha, captchas, challenge, err, m, opts, post, reply, response, threadID; diff --git a/script.coffee b/script.coffee index cfb66e5ae..3d9cdfd67 100644 --- a/script.coffee +++ b/script.coffee @@ -351,6 +351,10 @@ $.extend $, return open: (url) -> (GM_openInTab or window.open) location.protocol + url, '_blank' + event: (el, name, type) -> + e = d.createEvent type or 'CustomEvent' + e.initEvent name, true, false + el.dispatchEvent e globalEval: (code) -> script = $.el 'script', textContent: code $.add d.head, script @@ -931,9 +935,7 @@ Keybinds = ta.setSelectionRange range, range # Fire the 'input' event - e = d.createEvent 'Event' - e.initEvent 'input', true, false - ta.dispatchEvent e + $.event ta, 'input', 'Event' img: (thread, all) -> if all @@ -1175,9 +1177,7 @@ QR = ta.setSelectionRange range, range # Fire the 'input' event - e = d.createEvent 'Event' - e.initEvent 'input', true, false - ta.dispatchEvent e + $.event ta, 'input', 'Event' drag: (e) -> # Let it drag anything from the page. @@ -1519,9 +1519,7 @@ QR = # Create a custom event when the QR dialog is first initialized. # Use it to extend the QR's functionalities, or for XTRM RICE. - e = d.createEvent 'CustomEvent' - e.initEvent 'QRDialogCreation', true, false - QR.el.dispatchEvent e + $.event QR.el, 'QRDialogCreation' submit: (e) -> e?.preventDefault()