From 82a541b2d20b6b60b5b6ffd1cca46849eb9efd93 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 26 Aug 2012 03:06:39 +0200 Subject: [PATCH] Add $.queueTask. --- 4chan_x.user.js | 29 +++++++++++++++++++++++++++-- script.coffee | 24 ++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 14331ce3a..ef749b5aa 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -480,6 +480,30 @@ open: function(url) { return (GM_openInTab || window.open)(url, '_blank'); }, + queueTask: (function() { + var execTask, taskChannel, taskQueue; + taskQueue = []; + execTask = function() { + var args, func, task; + task = taskQueue.shift(); + func = task[0]; + args = Array.prototype.slice.call(task, 1); + return func.apply(func, args); + }; + if (window.MessageChannel) { + taskChannel = new MessageChannel(); + taskChannel.port1.onmessage = execTask; + return function() { + taskQueue.push(arguments); + return taskChannel.port2.postMessage(null); + }; + } else { + return function() { + taskQueue.push(arguments); + return setTimeout(execTask, 0); + }; + } + })(), globalEval: function(code) { var script; script = $.el('script', { @@ -489,9 +513,10 @@ return $.rm(script); }, unsafeWindow: window.opera && window || unsafeWindow || (function() { - d.createElement('p'); + var p; + p = d.createElement('p'); p.setAttribute('onclick', 'return window'); - return el.onclick(); + return p.onclick(); })(), shortenFilename: function(filename, isOP) { var threshold; diff --git a/script.coffee b/script.coffee index dd7d00a9d..7a00843ad 100644 --- a/script.coffee +++ b/script.coffee @@ -366,6 +366,26 @@ $.extend $, return open: (url) -> (GM_openInTab or window.open) url, '_blank' + queueTask: + # inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007 + (-> + taskQueue = [] + execTask = -> + task = taskQueue.shift() + func = task[0] + args = Array::slice.call task, 1 + func.apply func, args + if window.MessageChannel + taskChannel = new MessageChannel() + taskChannel.port1.onmessage = execTask + -> + taskQueue.push arguments + taskChannel.port2.postMessage null + else # XXX Firefox + -> + taskQueue.push arguments + setTimeout execTask, 0 + )() globalEval: (code) -> script = $.el 'script', textContent: code @@ -373,9 +393,9 @@ $.extend $, $.rm script # http://mths.be/unsafewindow unsafeWindow: window.opera and window or unsafeWindow or (-> - d.createElement 'p' + p = d.createElement 'p' p.setAttribute 'onclick', 'return window' - el.onclick() + p.onclick() )() shortenFilename: (filename, isOP) -> # FILENAME SHORTENING SCIENCE: