-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
200 lines (167 loc) · 5.54 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { ipcRenderer, webFrame } = require('electron')
const { createCursor, followCursor } = require('./js/cursor.js')
const { Link, Rectangle, QuadTree } = require('./js/quadtree')
const { throttle, isEqual } = require('lodash')
const { genId, isElementANavElement } = require('./js/utils')
const { markNavbars, passNavElementOnDwell } = require('./js/navbar-pattern')
const Config = require('./js/config')
var _browser_zoomLevel = 0
var _browser_maxZoom = 9
var _browser_minZoom = -8
ipcRenderer.on('zoomIn', () => {
if (_browser_maxZoom > _browser_zoomLevel) {
_browser_zoomLevel += 0.75
}
webFrame.setZoomLevel(_browser_zoomLevel)
})
ipcRenderer.on('zoomOut', () => {
if (_browser_minZoom < _browser_zoomLevel) {
_browser_zoomLevel -= 0.75
}
webFrame.setZoomLevel(_browser_zoomLevel)
})
ipcRenderer.on('zoomReset', () => {
_browser_zoomLevel = 0
webFrame.setZoomLevel(_browser_zoomLevel)
})
var c
let qTree
let boundary
let linkBounds
let links = []
let linksVisited = []
document.addEventListener('DOMContentLoaded', () => {
// Instantiate Cursor
createCursor('cursor')
c = document.getElementById('cursor')
// c.style['border-color'] = 'red'
// c.style['border-left'] = `${Config.rangeWidth/2}px`
// c.style['border-right'] = `${Config.rangeWidth/2}px`
// c.style['border-top'] = `${Config.rangeHeight/2}px`
// c.style['border-bottom'] = `${Config.rangeHeight/2}px`
// c.style['border-style'] = 'solid'
followCursor('cursor')
document.addEventListener('mouseout', () => {
c.style.visibility = 'hidden'
})
document.addEventListener('mouseover', () => {
c.style.visibility = 'visible'
})
if (!window.scrollUp) {
ipcRenderer.send('hideScrollUp')
}
document.addEventListener('scroll', () => {
if (!window.scrollY) {
ipcRenderer.send('hideScrollUp')
} else {
ipcRenderer.send('showScrollUp')
// ipcRenderer.send('showScrollDown')
}
})
instantiateQuadTree()
markLinks()
populateQuadTree()
var getLinksFromCursorRange = throttle(() => {
let cursorLoc = c.getBoundingClientRect()
getLinksFromQuadTree(cursorLoc)
}, Config.dwellTime, { 'leading': false })
document.addEventListener('mousemove', getLinksFromCursorRange)
document.addEventListener('mouseleave', () => {
document.removeEventListener('mousemove', getLinksFromCursorRange)
getLinksFromCursorRange.cancel()
})
document.addEventListener('mouseenter', () => {
document.addEventListener('mousemove', getLinksFromCursorRange)
})
// ---------- END HYPERLINK NAVIGATION PATTERN
// NAVIGATION BAR INTERACTION PATTERN
let navElements = markNavbars()
if (navElements) {
for (var i=0; i < navElements.length; i++) {
(function(i) {
navElements[i].addEventListener('mouseover', passNavElementOnDwell)
})(i)
}
}
// ---------- END NAVBAR NAVIGATION PATTERN
})
function instantiateQuadTree() {
// Instantiate Quad Tree
let clientHeight = Math.max(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
)
let clientWidth = document.documentElement.clientWidth
// let clientHeight = document.documentElement.scrollHeight
boundary = new Rectangle(clientWidth/2, clientHeight/2, clientWidth, clientHeight)
qTree = new QuadTree(boundary, 1)
}
function markLinks() {
// Populate Quad Tree
let anchors = document.getElementsByTagName('a')
for (var i = 0; i < anchors.length; i++) {
// Filtering through unneeded links
if (isElementANavElement(anchors[i])) {
continue
}
linkBounds = anchors[i].getBoundingClientRect()
if (linkBounds.x === 0 && linkBounds.y === 0 && linkBounds.width === 0 && linkBounds.height === 0) {
continue
}
if (!anchors[i].href ) {
continue
}
// var text = anchors[i].text.concat(anchors[i].title)
// if (!text.match(/[a-z]/i)) {
// continue
// }
anchors[i].classList.add('linkMark')
anchors[i].id = genId()
links.push(anchors[i])
}
}
function populateQuadTree() {
for (var i = 0; i < links.length; i++) {
const linkTitle = links[i].title ? links[i].title : links[i].text.trim()
linkBounds = links[i].getBoundingClientRect()
let link = new Link(
linkBounds.x,
linkBounds.y,
linkBounds.top,
linkBounds.left,
linkBounds.bottom,
linkBounds.right,
linkBounds.width,
linkBounds.height,
linkBounds.x + linkBounds.width/2,
linkBounds.y + linkBounds.height/2,
links[i].href,
linkTitle,
links[i].id
)
qTree.insert(link)
}
}
// Get Links which fall within the cursor's range from Quad Tree
function getLinksFromQuadTree(cursorLocation) {
let posX = window.scrollX + cursorLocation.x
let posY = window.scrollY + cursorLocation.y
let range = new Rectangle(posX, posY, Config.rangeWidth, Config.rangeHeight)
let points = qTree.query(range)
if (Array.isArray(points) && points.length) {
if (!isEqual(points, linksVisited)) {
if (linksVisited.length) {
for (let i=0; i < linksVisited.length; i++) {
document.getElementById(linksVisited[i]).classList.remove('linkVisualise')
}
linksVisited = []
}
for (let i=0; i < points.length; i++) {
document.getElementById(points[i].id).classList.add('linkVisualise')
linksVisited.push(points[i].id)
}
}
ipcRenderer.send('getLinks', points)
}
}