From 1246df80afff60fb934de9e8600f2b2eb1e10ac9 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 24 Feb 2013 18:49:35 +0100 Subject: [PATCH] Use Mutation Observers to observe captcha changes instead of Mutation Listeners when possible. --- 4chan_x.user.js | 11 +++++++++-- src/qr.coffee | 12 +++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index da2b3568f..1ff91b2ac 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -6276,7 +6276,7 @@ }), this.ready.bind(this)); }, ready: function() { - var imgContainer, inputContainer; + var MutationObserver, imgContainer, inputContainer, observer; imgContainer = $.el('div', { className: 'captchaimg', title: 'Reload', @@ -6293,9 +6293,16 @@ img: imgContainer.firstChild, input: inputContainer.firstChild }; + if (MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.OMutationObserver) { + observer = new MutationObserver(this.load.bind(this)); + observer.observe(this.nodes.challenge, { + childList: true + }); + } else { + $.on(this.nodes.challenge, 'DOMNodeInserted', this.load.bind(this)); + } $.on(imgContainer, 'click', this.reload.bind(this)); $.on(this.nodes.input, 'keydown', this.keydown.bind(this)); - $.on(this.nodes.challenge, 'DOMNodeInserted', this.load.bind(this)); $.sync('captchas', this.count.bind(this)); this.count($.get('captchas', [])); this.reload(); diff --git a/src/qr.coffee b/src/qr.coffee index 505899b76..b0b312a66 100644 --- a/src/qr.coffee +++ b/src/qr.coffee @@ -522,9 +522,15 @@ QR = img: imgContainer.firstChild input: inputContainer.firstChild - $.on imgContainer, 'click', @reload.bind @ - $.on @nodes.input, 'keydown', @keydown.bind @ - $.on @nodes.challenge, 'DOMNodeInserted', @load.bind @ + if MutationObserver = window.MutationObserver or window.WebKitMutationObserver or window.OMutationObserver + observer = new MutationObserver @load.bind @ + observer.observe @nodes.challenge, + childList: true + else + $.on @nodes.challenge, 'DOMNodeInserted', @load.bind @ + + $.on imgContainer, 'click', @reload.bind @ + $.on @nodes.input, 'keydown', @keydown.bind @ $.sync 'captchas', @count.bind @ @count $.get 'captchas', [] # start with an uncached captcha