-
Notifications
You must be signed in to change notification settings - Fork 0
/
hover.js
76 lines (67 loc) · 2.5 KB
/
hover.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
// Default values
var enabled = false;
var currentHoverLink = null;
var currentMousePos = { x: -1, y: -1 };
// Gets different parts of page URL
var fullUrl = window.location.href; // http://example.com/path/to/page.html
var pagePath = window.location.pathname; // /path/to/page.html
var domain = window.location.origin; // http://example.com
// Gets current cursor position for drawing popup
$(document).mousemove(function(event) {
currentMousePos.x = event.pageX;
currentMousePos.y = event.pageY;
});
// Get ext_enabled variable from storage and set local variable
// equal to stored value
chrome.storage.local.get(['ext_enabled'], function(result) {
console.log('Extension toggled: ' + result.ext_enabled);
enabled = result.ext_enabled;
});
$(document).ready(function(){
// Add popup to DOM body
$("body").append(getPopup());
// Whenever an anchor tag <a> is hovered over
$("a").hover(function(){
chrome.storage.local.get(['ext_enabled'], function(result) {
enabled = result.ext_enabled;
});
// Only show/hide popup if extension is enabled
if(enabled){
// Get link that cursor is hovering over
currentHoverLink = $(this).attr('href');
console.log(currentMousePos.x + ", " + currentMousePos.y);
console.log(currentHoverLink);
updatePopup(currentHoverLink, currentMousePos.x, currentMousePos.y);
// Fade in popup to prevent flickering
$("#wtlg_popup").stop(true, true).fadeIn();
}
}, function(){
if(enabled){
// Fade out popup to prevent flickering
$("#wtlg_popup").stop(true, true).fadeOut();
}
});
});
function getPopup() {
// Returns pop HTML
return "<div id='wtlg_popup' style='display: none; z-index: 1000; "
+ "background-color: #e3e3e3; position: absolute;"
+ "border: 2px solid black; left: 0px; top: 0px; font-size: 16pt;"
+ "'</div>";
}
function updatePopup(url, left, top) {
// Check if URL is on same domain
if(url.includes(domain) || url.charAt(0) == "/" || url.charAt(0) == "#" ) {
$("#wtlg_popup").css("background-color", "#4fe27b");
// Check if URL is shortened using bit.ly, goo.gl, or t.co
} else if(url.includes("http://bit.ly") || url.includes("https://bit.ly")
|| url.includes("http://goo.gl") || url.includes("https://goo.gl")
|| url.includes("http://t.co") || url.includes("https://t.co")) {
$("#wtlg_popup").css("background-color", "#f7f742");
} else {
$("#wtlg_popup").css("background-color", "#d8d8d8");
}
$("#wtlg_popup").css("left",left);
$("#wtlg_popup").css("top",top);
$("#wtlg_popup").html(url);
}