Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 370
popup.js
const statusCard = document.getElementById('status-card');
const statusIcon = document.getElementById('status-icon');
const statusText = document.getElementById('status');
const locationDetails = document.getElementById('location-details');
const latitudeElement = document.getElementById('latitude');
const longitudeElement = document.getElementById('longitude');
const accuracyElement = document.getElementById('accuracy');
const mapLink = document.getElementById('map-link');
const copyButton = document.getElementById('copy-button');
const refreshButton = document.getElementById('refresh-button');
let currentCoordinates = '';
document.addEventListener('DOMContentLoaded', getLocation);
refreshButton.addEventListener('click', getLocation);
copyButton.addEventListener('click', copyCoordinates);
function getLocation() {
locationDetails.classList.add('hidden');
statusCard.className = 'status-card loading';
statusIcon.textContent = '◌';
statusText.textContent = 'Detecting your location...';
refreshButton.disabled = true;
refreshButton.textContent = 'Locating...';
if (!navigator.geolocation) {
showError('Location detection is not supported by this browser.');
return;
}
navigator.geolocation.getCurrentPosition(
showLocation,
showLocationError,
{
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 0
}
);
}
function showLocation(position) {
const { latitude, longitude, accuracy } = position.coords;
const formattedLatitude = latitude.toFixed(6);
const formattedLongitude = longitude.toFixed(6);
currentCoordinates = `${formattedLatitude}, ${formattedLongitude}`;
latitudeElement.textContent = formattedLatitude;
longitudeElement.textContent = formattedLongitude;
accuracyElement.textContent = `About ${Math.round(accuracy)} meters`;
mapLink.href = `https://www.google.com/maps?q=${latitude},${longitude}`;
statusCard.className = 'status-card success';
statusIcon.textContent = '✓';
statusText.textContent = 'Location found successfully.';
locationDetails.classList.remove('hidden');
refreshButton.disabled = false;
refreshButton.textContent = 'Get New Location';
}
function showLocationError(error) {
console.error('Failed to detect location:', error);
const messages = {
1: 'Location permission was denied.',
2: 'Your location could not be determined.',
3: 'Location detection timed out.'
};
showError(messages[error.code] || 'Unable to detect your location.');
}
function showError(message) {
statusCard.className = 'status-card error';
statusIcon.textContent = '!';
statusText.textContent = message;
refreshButton.disabled = false;
refreshButton.textContent = 'Try Again';
}
async function copyCoordinates() {
if (!currentCoordinates) {
return;
}
try {
await navigator.clipboard.writeText(currentCoordinates);
const originalText = copyButton.textContent;
copyButton.textContent = 'Copied!';
setTimeout(() => {
copyButton.textContent = originalText;
}, 1500);
} catch (error) {
console.error('Failed to copy coordinates:', error);
copyButton.textContent = 'Copy failed';
}
}