This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
inject.js
116 lines (103 loc) · 3.12 KB
/
inject.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
114
115
116
var statusBar, el;
if (window.top === window) {
safari.self.addEventListener('message', function handleMessage(e) {
switch (e.name) {
case 'displayStatus':
displayStatus(e.message);
break;
case 'hideStatus':
hideStatus();
break;
}
}, false);
window.addEventListener('blur', function() {
if (statusBar)
statusBar.classList.add('blur');
});
window.addEventListener('focus', function() {
if (statusBar)
statusBar.classList.remove('blur');
});
}
if (
document.readyState === 'complete' ||
// Reader seems to report 'interactive' and not trigger DOMContentLoaded later.
location.protocol === 'safari-reader:' && document.readyState === 'interactive'
)
ready();
else
document.addEventListener('DOMContentLoaded', ready);
function ready() { document.body.addEventListener('mouseover', mouseover); }
function mouseover(e) {
el = e.target;
while (el && el.nodeName.toLowerCase() !== 'a')
el = el.parentNode;
if (el && el.attributes.href) {
safari.self.tab.dispatchMessage('mouseover', {
href: el.href,
hrefRelative: el.attributes.href.value,
target: el.target,
metaKey: e.metaKey,
ctrlKey: e.ctrlKey,
altKey: e.altKey,
shiftKey: e.shiftKey
});
} else {
safari.self.tab.dispatchMessage('mouseout');
}
}
function keyChanged(e) {
switch (e.keyCode) {
case 16:
safari.self.tab.dispatchMessage('mouseover', { shiftKey: e.shiftKey });
break;
case 17:
safari.self.tab.dispatchMessage('mouseover', { ctrlKey: e.ctrlKey });
break;
case 18:
safari.self.tab.dispatchMessage('mouseover', { altKey: e.altKey });
break;
case 91:
case 93:
safari.self.tab.dispatchMessage('mouseover', { metaKey: e.metaKey });
break;
}
}
function elementsIntersect(a, b) {
if (!a || !b) return;
a = a.getBoundingClientRect();
b = b.getBoundingClientRect();
var xIntersect = (a.left <= b.left && b.left <= a.right) ||
(b.left <= a.left && a.left <= b.right);
var yIntersect = (a.top <= b.top && b.top <= a.bottom) ||
(b.top <= a.top && a.top <= b.bottom);
return xIntersect && yIntersect;
}
function displayStatus(text) {
if (!statusBar) {
statusBar = document.createElement('div');
statusBar.id = 'com-fortnight-status-bar';
document.body.appendChild(statusBar);
}
statusBar.innerText = text;
setTimeout(function() {
statusBar.classList.add('active');
document.addEventListener('keydown', keyChanged);
document.addEventListener('keyup', keyChanged);
}, 1);
// If the statusbar overlaps the hovered element, try moving it to the
// right side of the viewport. If it still overlaps, give up and put it
// back.
statusBar.classList.remove('right-side');
if (elementsIntersect(statusBar, el)) {
statusBar.classList.add('right-side');
if (elementsIntersect(statusBar, el))
statusBar.classList.remove('right-side');
}
}
function hideStatus() {
if (!statusBar) return;
statusBar.classList.remove('active');
document.removeEventListener('keydown', keyChanged);
document.removeEventListener('keyup', keyChanged);
}