-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (82 loc) · 3.2 KB
/
script.js
File metadata and controls
96 lines (82 loc) · 3.2 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
document.addEventListener('DOMContentLoaded', function() {
const mapsInput = document.getElementById('mapsInput');
const convertBtn = document.getElementById('convertBtn');
const resultContainer = document.getElementById('result');
const errorContainer = document.getElementById('error');
const coordinatesEl = document.getElementById('coordinates');
const wazeUrlEl = document.getElementById('wazeUrl');
const errorMessageEl = errorContainer.querySelector('.error-message');
convertBtn.addEventListener('click', async function() {
const mapsLink = mapsInput.value.trim();
if (!mapsLink) {
showError('Please enter a Google Maps link');
return;
}
hideMessages();
convertBtn.disabled = true;
convertBtn.textContent = 'Converting...';
try {
const response = await fetch('https://faas-fra1-afec6ce7.doserverless.co/api/v1/web/fn-2147b526-aa08-4de1-a083-670d2a13332a/default/gmap2waze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: {
chat: {
id: -1
},
text: mapsLink
}
})
});
const data = await response.json();
if (data.ok && data.coordinates && data.wazeUrl) {
showResult(data);
} else {
showError('Failed to convert the link. Please check if the URL is valid.');
}
} catch (error) {
showError('An error occurred while converting the link. Please try again.');
console.error('Error:', error);
} finally {
convertBtn.disabled = false;
convertBtn.textContent = 'Convert';
}
});
mapsInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
convertBtn.click();
}
});
function showResult(data) {
const { latitude, longitude } = data.coordinates;
coordinatesEl.textContent = `${latitude}, ${longitude}`;
wazeUrlEl.href = data.wazeUrl;
resultContainer.style.display = 'block';
errorContainer.style.display = 'none';
// Track conversion event in Google Analytics
if (typeof gtag !== 'undefined') {
gtag('event', 'link_converted_web', {
'event_category': 'conversion',
'event_label': 'web_converter'
});
}
}
function showError(message) {
errorMessageEl.textContent = message;
errorContainer.style.display = 'block';
resultContainer.style.display = 'none';
// Track failed conversion event in Google Analytics
if (typeof gtag !== 'undefined') {
gtag('event', 'web_conversion_failed', {
'event_category': 'conversion',
'event_label': 'web_converter_error'
});
}
}
function hideMessages() {
resultContainer.style.display = 'none';
errorContainer.style.display = 'none';
}
});