Add $.queueTask.

This commit is contained in:
Nicolas Stepien 2012-08-26 03:06:39 +02:00
parent bb31bc3b93
commit 82a541b2d2
2 changed files with 49 additions and 4 deletions

View File

@ -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;

View File

@ -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: