Skip to content

Instantly share code, notes, and snippets.

@Maxdamantus

Maxdamantus/.js Secret

Created May 30, 2018 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maxdamantus/0aefa2c713afda4af04abdbb3a97ca89 to your computer and use it in GitHub Desktop.
Save Maxdamantus/0aefa2c713afda4af04abdbb3a97ca89 to your computer and use it in GitHub Desktop.
add_hook("window_initialize_early_hook", function(window){
window.addEventListener("click", function(event){
event.preventDefault();
}, true);
window.getBrowser().style.MozTransform = "rotate(90deg)";
window.addEventListener("mousedown", function(event){
function onmouseup(event){
dragger.up(event.clientX, event.clientY);
window.removeEventListener("mousemove", onmousemove, true);
window.removeEventListener("mouseup", onmouseup, true);
event.preventDefault();
}
function onmousemove(event){
dragger.move(event.clientX, event.clientY);
event.preventDefault();
}
dragger.set_target(event.target);
dragger.down(event.clientX, event.clientY);
window.addEventListener("mousemove", onmousemove, true);
window.addEventListener("mouseup", onmouseup, true);
event.preventDefault();
}, true);
function scrollLeftMax(el){
return el.scrollWidth - el.clientWidth;
}
function scrollTopMax(el){
return el.scrollHeight - el.clientHeight;
}
var dragger = function(){
var active = false;
var timer = null;
var target;
var lastX, lastY;
var xps, yps;
var before;
var fps = 30;
var self;
return self = {
set_target: function(t){
target = t;
},
down: function(x, y){
lastX = x;
lastY = y;
active = true;
if(timer != null)
window.clearTimeout(timer);
timer = null;
},
move: function(x, y){
if(active){
var now = Date.now();
xps = (x - lastX)*(1000/(now - before));
yps = (y - lastY)*(1000/(now - before));
before = now;
}
var dx = lastX - x, dy = lastY - y;
var xtarget = target, ytarget = target;
while(xtarget != null && dx != 0){
var sl = xtarget.scrollLeft + dx;
dx = 0;
if(sl < 0){
dx += sl;
sl = 0;
}else if(sl > scrollLeftMax(xtarget)){
dx += sl - scrollLeftMax(xtarget);
sl = scrollLeftMax(xtarget);
}
xtarget.scrollLeft = sl;
xtarget = xtarget.parentNode;
}
while(ytarget != null && dy != 0){
var st = ytarget.scrollTop + dy;
dy = 0;
if(st < 0){
dy += st;
st = 0;
}else if(st > scrollTopMax(ytarget)){
dy += st - scrollTopMax(ytarget);
st = scrollTopMax(ytarget);
}
ytarget.scrollTop = st;
ytarget = ytarget.parentNode;
}
lastX = x;
lastY = y;
},
up: function(){
active = false;
var x = lastX, y = lastY;
function keep_going(){
x += xps/fps;
y += yps/fps;
self.move(x, y);
xps *= 0.9;
yps *= 0.9;
timer = Math.abs(xps) > 1 || Math.abs(yps) > 1? window.setTimeout(keep_going, 1000/fps) : null;
}
keep_going();
},
abort: function(){
if(timer != null)
timer = null;
}
};
}();
}, false, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment