-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy-link-laser-0.1.js
113 lines (92 loc) · 2.34 KB
/
lazy-link-laser-0.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//http://css-tricks.com/examples/AnythingZoomer/use.php
//Draw a mark somewhere for tracking anchor hit boxes
function drawMark(x, y){
}
//Get minimum distance between an anchor and the cursor
function getDistance(cursorX, cursorY, element){
return getDistanceHypotenuse(
cursorX,
cursorY,
getCenterX(element),
getCenterY(element)
);
}
function getDistanceHypotenuse(x1, y1, x2, y2){
return Math.sqrt(
((x1 - x2) * (x1 - x2))
+
((y1 - y2) * (y1 - y2))
);
}
function getCenterX(element){
return getMinX(element) + ($(element).width() / 2);
}
function getCenterY(element){
return getMinY(element) + ($(element).height() / 2);
}
function getMinX(element){
return element.offsetLeft;
}
function getMaxX(element){
return getMinY(element) + $(element).width();
}
function getMinY(element){
return element.offsetTop;
}
function getMaxY(element){
return getMinY(element) + $(element).height();
}
function handleMouse(event){
if(!event.ctrlKey){
return;
}
// console.log(event);
// console.log("Which: " + event.which);
var minDistance = null;
var minElement = null;
$('a').each(function(){
$(this).css('color', 'black');
var distance = getDistance(event.pageX, event.pageY, this);
if(minDistance === null || distance < minDistance){
minDistance = distance;
minElement = this;
}
});
// console.log("Closest element is: ");
// console.log(minElement);
var destination = $(minElement).attr('href');
if(event.which == 1){
if(destination !== undefined && destination.length){
window.location = destination;
}
}
else if(event.which == 0){
//Turn the nearest one red and all others nothing
$(minElement).css('color', 'red');
}
}
function getTextNodeHeight(textNode) {
var height = 0;
if (document.createRange) {
var range = document.createRange();
range.selectNodeContents(textNode);
if (range.getBoundingClientRect) {
var rect = range.getBoundingClientRect();
if (rect) {
height = rect.bottom - rect.top;
}
}
}
return height;
}
$(document).ready(function(){
console.log("loading lazy link laser");
$(document).mousemove(function(e){
handleMouse(e);
});
$(document).click(function(e){
handleMouse(e);
});
$('<p>Press and hold the ctrl-key while moving the mouse to activate the lazy link laser</p>').prependTo('body');
console.log("done loading lazy link laser");
});