-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
448 lines (358 loc) · 13.8 KB
/
content.js
File metadata and controls
448 lines (358 loc) · 13.8 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//State of the preview
const PopupStates = {
Loading: 0,
Failed: 1,
Content: 2,
NotTrusted: 3
}
popupElement = null;
popupElementContent = null;
popupShowing = false;
prev_target = null //current target/what the preview is hovering over
prev_src = "" //previous link that was shown (do we need to reload?/send another request)
currentLink = "" //current link that is being previewed
current_target = null //What the mouse is hovering
currentTimeout = null
var httpRequest = null
var resizing = false //are we resizing the preview(called also popup)? If so don't hide the popup if the mouse leaves the popup region
//loads the preview when it loads onto the page
$.get(chrome.runtime.getURL('/popup.html'), function (data) {
//adds popup to page
$($.parseHTML(data)).appendTo('body');
popupElement = document.getElementById("HyperlinkPreview-Popup")
popupElementContent = document.getElementById("HyperlinkPreview-Popup-content")
//sets instructions for the trust buttons
$("#HyperlinkPreview-TrustButton").click(function (data) {
if (currentLink != "") {
chrome.storage.sync.get({
authroizedLinks: []
}, function (result) {
var authroizedLinks = result.authroizedLinks;
authroizedLinks.push({
link: currentLink,
authorized: true
});
chrome.storage.sync.set({
authroizedLinks: authroizedLinks
}).then(result => {
launchRequest(prev_target)
})
})
}
})
$("#HyperlinkPreview-DontTrustButton").click(function (data) {
if (currentLink != "") {
chrome.storage.sync.get({
authroizedLinks: []
}, function (result) {
var authroizedLinks = result.authroizedLinks;
authroizedLinks.push({
link: currentLink,
authorized: false
});
chrome.storage.sync.set({
authroizedLinks: authroizedLinks
}).then(result => {
hidePopup()
})
})
}
})
//sets the closebutton x image
$("#HyperlinkPreview-TopNav-CloseButton").attr("src", chrome.runtime.getURL("images/xIcon.svg"))
//hide popup if the close button is hit
$("#HyperlinkPreview-IconButton").click(function (data) {
hidePopup()
})
//get if the IFrame is allowed to run scripts
$("#HyperlinkPreview-ScriptingAuth").change(function () {
var scriptingAllowed = $("#HyperlinkPreview-ScriptingAuth").is(':checked')
setIFrameScripting(scriptingAllowed)
chrome.storage.sync.set({
scriptingAllowed: scriptingAllowed
})
})
//get initial state of scriptauth
chrome.storage.sync.get({
scriptingAllowed: false
}, function (result) {
var scriptingAllowed = result.scriptingAllowed;
$("#HyperlinkPreview-ScriptingAuth").prop("checked", scriptingAllowed)
setIFrameScripting(scriptingAllowed, false)
})
//loads jquery ui css
$("#HyperlinkPreview-JQueryUI").prop("href", chrome.runtime.getURL("thirdPartyScripts/jquery-ui.min.css"))
//set the states of popup
showLoading()
hideContent()
hideForbidden()
hideNotTrusted()
resetResizable("n", "e")
});
//reposition popup if user scrolls the page
document.addEventListener("scroll", function (e) {
if (popupShowing) {
positionPopup(prev_target)
}
})
//main event, looks at what the mouse is currently hovering and provides a popup if its a tag
document.addEventListener("mousemove", function (e) {
current_target = e.target
var currentTargetParent = current_target.closest("a")
//Hide preview box if mouse is too far away from preview
if (prev_target != null && currentTargetParent != null) {
var targetPos = currentTargetParent.getBoundingClientRect()
var selectionPos = prev_target.getBoundingClientRect()
if (currentTimeout != null && Math.sqrt(Math.pow(selectionPos.x - targetPos.x, 2) + Math.pow(selectionPos.y - targetPos.y, 2)) > 500 && !resizing) {
hideContent()
hidePopup()
}
}
//Makes sure the user can make the IFrame smaller without the IFrame from stealing the focus
if (resizing) {
$(popupElementContent).css("pointer-events", "none")
} else {
$(popupElementContent).css("pointer-events", "auto")
}
//clear the timeout if needbe (user must hover and not move mouse to see preview launch)
if (currentTimeout != null) {
clearTimeout(currentTimeout)
}
currentTimeout = setTimeout(function () {
//does the same above basically
var elementLoopParent = current_target.closest("a")
if (elementLoopParent == null && current_target.closest("#HyperlinkPreview-Popup") == null && !resizing) {
hidePopup()
}
if (elementLoopParent != null) {
//checks if the popup needs to be refreshed
if (elementLoopParent == prev_target) {
return;
}
prev_target = elementLoopParent
//checks again if a refresh is needed by seeing if the href link is the same (or is the same but just has data in the URL (# info))
if (elementLoopParent.href != prev_src && worthyPreview(elementLoopParent.href)) {
changeState(PopupStates.Loading)
positionPopup(elementLoopParent)
showPopup()
//Gets the base url and displays it
let baseURL = getBaseUrl(elementLoopParent.href)
currentLink = baseURL
$("#HyperlinkPreview-WebsiteViewingPrompt").html("Viewing: " + baseURL)
//Checks if the link has been authorized to use by the user
chrome.storage.sync.get("authroizedLinks", ({
authroizedLinks
}) => {
webObject = authroizedLinks.find(element => element.link == baseURL)
if (webObject == null || webObject.authorized != true) {
$("#HyperlinkPreview-Popup-trust-link").html(baseURL)
if (webObject != null && webObject.authorized == false) {
$("#HyperlinkPreview-Popup-trust-warning").css("display", "block")
} else {
$("#HyperlinkPreview-Popup-trust-warning").css("display", "none")
}
changeState(PopupStates.NotTrusted)
} else {
launchRequest(elementLoopParent)
}
prev_src = elementLoopParent.href
})
}
} else if (current_target.closest("#HyperlinkPreview-Popup") == null && !resizing) {
hidePopup()
currentTimeout = null
}
}, 750)
})
//checks if the mouse is down or up (determines resizing)
document.addEventListener("mousedown", setPrimaryButtonState);
document.addEventListener("mousemove", setPrimaryButtonState);
document.addEventListener("mouseup", setPrimaryButtonState);
function hidePopup() {
prev_target = null;
prev_src = ""
popupShowing = false
changeState(PopupStates.Loading)
popupElement.style["display"] = "none"
}
function showPopup() {
popupShowing = true
popupElement.style["display"] = "block"
}
function showLoading() {
$("#HyperlinkPreview-Popup-loading").css("display", "block");
}
function hideLoading() {
$("#HyperlinkPreview-Popup-loading").css("display", "none");
}
function showForbidden() {
$("#HyperlinkPreview-Popup-forbidden").css("display", "block");
}
function hideForbidden() {
$("#HyperlinkPreview-Popup-forbidden").css("display", "none");
}
function showContent() {
$("#HyperlinkPreview-Popup-ContentHelper").css("display", "block");
}
function hideContent() {
$("#HyperlinkPreview-Popup-ContentHelper").css("display", "none");
}
function showNotTrusted() {
$("#HyperlinkPreview-Popup-trust").css("display", "flex");
}
function hideNotTrusted() {
$("#HyperlinkPreview-Popup-trust").css("display", "none");
}
var currentState = PopupStates.Loading
function changeState(stateToChange, data = {}) {
switch (currentState) {
case PopupStates.Content: {
hideContent()
break;
}
case PopupStates.Loading: {
hideLoading()
break;
}
case PopupStates.Failed: {
hideForbidden()
break;
}
case PopupStates.NotTrusted: {
hideNotTrusted()
break;
}
}
switch (stateToChange) {
case PopupStates.Content: {
showContent()
break;
}
case PopupStates.Loading: {
showLoading()
break;
}
case PopupStates.Failed: {
showForbidden()
break;
}
case PopupStates.NotTrusted: {
showNotTrusted()
break;
}
}
currentState = stateToChange //updates state
if (prev_target != null) {
positionPopup(prev_target) //must be done as state changes the popup size
}
}
var primaryMouseButtonDown = false;
function setPrimaryButtonState(e) {
var flags = e.buttons !== undefined ? e.buttons : e.which;
primaryMouseButtonDown = (flags & 1) === 1;
if (primaryMouseButtonDown == false) {
resizing = false; //resets resizing cuz its not possible in this case
}
}
//what direction should the popup be in (prevents overflowing/going out of the page)
function positionPopup(target) {
var frontDirection = "n" //n or s
var sideDirection = "e" //w or e
boundBox = target.getBoundingClientRect()
popupSize = popupElement.getBoundingClientRect()
resultingX = boundBox.left //boundBox.right - popupSize.width/2 + boundBox.width
resultingY = window.innerHeight - boundBox.top
if (resultingY < 0 || resultingY > window.innerHeight - popupSize.height) {
popupElement.style["top"] = (boundBox.top + boundBox.height) + "px"
popupElement.style["bottom"] = ""
frontDirection = "s"
} else {
popupElement.style["top"] = ""
popupElement.style["bottom"] = (resultingY) + "px"
frontDirection = "n"
}
if (resultingX < 0 || resultingX > window.innerWidth - popupSize.width) {
popupElement.style["left"] = ""
popupElement.style["right"] = (window.innerWidth - boundBox.left - boundBox.width) + "px"
sideDirection = "w"
} else {
popupElement.style["left"] = (resultingX) + "px"
popupElement.style["right"] = ""
sideDirection = "e"
}
resetResizable(frontDirection, sideDirection)
}
//sets where the user can resize the popup (if the popup is above the link and to the right, than north east (ne))
function resetResizable(frontDirection, sideDirection) {
$("#HyperlinkPreview-Popup-ContentHelper").resizable({
handles: `${frontDirection}, ${sideDirection}, ${frontDirection+sideDirection}`,
resize: function (e, ui) {
resizing = true
}
})
}
//refreshes the iframe and writes in new html
function resetIframe(htmlText) {
popupElementContent.src = "about:blank";
popupElementContent.contentWindow.document.open();
try {
popupElementContent.contentWindow.document.write(htmlText);
} catch (e) {}
popupElementContent.contentWindow.document.close();
}
//checks if the preview should be refresehed based on if the new url is different
function worthyPreview(targetURL) {
var result = true
var baseURL = targetURL.replace(/(#.*)/g, "")
var documentURL = document.URL.replace(/(#.*)/g, "")
result = !(baseURL == documentURL || baseURL == "")
return result
}
function getBaseUrl(urlString) {
const url = new URL(urlString)
const baseUrl = `${url.hostname}`
return baseUrl
}
//laucnhes the html request for the page
function launchRequest(elementLoopParent) {
httpRequest = new XMLHttpRequest();
$(popupElementContent).prop("src", elementLoopParent.href) //done to send correct headers to the page to ensure same-origin policies work properly
httpRequest.open("GET", elementLoopParent.href) //used to get if the page can be loaded or not (provides the events needed to change states)
try {
httpRequest.send();
} catch (error) {}
httpRequest.addEventListener("load",
function (data) {
try {
$(popupElementContent).on("load", function () {
changeState(PopupStates.Content)
//here we get rid of navbars/headers which might clunk up the inital view (still a work in progress)
$(popupElementContent).contents().find("header").css("display", "none")
$(popupElementContent).contents().find("body").css("margin-top", "0px")
positionPopup(elementLoopParent)
})
} catch (e) {}
}
)
httpRequest.addEventListener("error", function (error) {
try {
changeState(PopupStates.Failed)
positionPopup(elementLoopParent)
httpRequest = new XMLHttpRequest();
} catch (e) {}
})
httpRequest.addEventListener('timeout', function (err) {
changeState(PopupStates.Failed)
})
}
//sets if the iframe should allow scripts (risky? yes, but it is at the users discretion to trust websites)
function setIFrameScripting(scripReloading, reloadIframe = true) {
if (scripReloading == true) {
$(popupElementContent).prop("sandbox", "allow-same-origin allow-scripts")
} else {
$(popupElementContent).prop("sandbox", "allow-same-origin")
}
if (reloadIframe) {
launchRequest(prev_target)
}
}