-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
82 lines (70 loc) · 2.97 KB
/
popup.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
var listeningAlready = false;
function sendContentMessage(event) {
event = event || window.event //cross-browser event
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true)
var messageObject = {
opacity: document.getElementById("opacityControl"),
location: document.getElementById("geoControl")
};
var messageText = "hello";
console.log('Sending message: ['+messageText+']')
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: messageText}, function(response) {
console.log("[popup.js] Response from content: " + response.farewell);
});
});
};
function showPosition(position) {
var geoControl = document.getElementById("geoControl");
geoControl.value = position.coords.latitude + ", " + position.coords.longitude;
};
function errorHandler(error) {
switch(error.code)
{
case error.PERMISSION_DENIED:
console.log("Could not get position as permission was denied.");
var geoControl = document.getElementById("geoControl");
geoControl.value = "PERMISSION DENIED";
break;
case error.POSITION_UNAVAILABLE:
console.log("Could not get position as this information is not available at this time.");
var geoControl = document.getElementById("geoControl");
geoControl.value = "POSITION UNAVAILABLE";
break;
case error.TIMEOUT:
console.log("Attempt to get position timed out.");
var geoControl = document.getElementById("geoControl");
geoControl.value = "TIMEOUT";
break;
default:
console.log("Sorry, an error occurred. Code: "+error.code+" Message: "+error.message);
var geoControl = document.getElementById("geoControl");
geoControl.value = "ERROR: " + error.message;
break;
}
};
// On page load
window.addEventListener("load", function load(event){
window.removeEventListener("load", load, false); //remove listener, no longer needed
console.log("In page load code.");
var theButton = document.getElementById("clickMe");
if (theButton) {
// Add click listener for button
if (!listeningAlready) {
listeningAlready = true;
console.log("Adding button click event listener for the first time.")
window.debugButton = theButton;
theButton.addEventListener("click", sendContentMessage, false);
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,errorHandler);
}
else {
console.log("Sorry, your browser does not support geolocation services.");
}
} else {
console.log("Already added button click... skipping.")
}
} else {
console.log("Button not ready yet for event adding.")
}
},false);