-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
100 lines (88 loc) · 3.04 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
document.addEventListener('DOMContentLoaded', function () {
const lookupButton = document.getElementById('lookup-button')
const aboutButton = document.getElementById('about-button')
const closeButton = document.getElementById('close-button')
const resultsDiv = document.getElementById('results')
const callsignInput = document.getElementById('callsign-input')
function displayResults(data) {
const operator = data.hamdb.callsign
let name = `${operator.fname} ${operator.name}`
const grid = operator.grid
let status = operator.status.toLowerCase()
const location = `${operator.addr1}, ${operator.addr2}, ${operator.state} ${operator.zip}, ${operator.country}`
let level = operator.class.toLowerCase()
// Convert the class to a more readable format
if (level === "t") {
level = "Technician"
} else if (level === "a") {
level = "Advanced"
} else if (level === "g") {
level = "General"
} else if (level === "e") {
level = "Extra"
} else if (level === "" && operator.fname === "") {
level = "Club"
} else {
level = "Unknown"
}
// Convert the status to a more readable format
if (status === "a") {
status = "Active"
} else if (status === "i") {
status = "Inactive"
} else {
status = "Unknown"
}
resultsDiv.innerHTML = `
<p><strong>Call Sign:</strong> ${operator.call} (${level})</p>
<p><strong>Operator:</strong> ${name}</p>
<p><strong>Location:</strong> ${location}</p>
<p><strong>Grid:</strong> ${grid}</p>
<p><strong>Status:</strong> ${status}</p>
`
}
function displayError(error) {
resultsDiv.innerHTML = `<p class="error">${error}</p>`
}
function lookupCallsign(callsign) {
fetch(`https://api.hamdb.org/v1/${callsign}/json/hamCallLookupChromeExtension`)
.then(response => response.json())
.then(data => {
if (data.hamdb.messages.status === "OK") {
displayResults(data)
} else {
displayError("No results found.")
}
})
.catch(error => {
displayError("An error occurred while looking up the call sign.")
console.error(error)
})
}
callsignInput.addEventListener('keyup', (event) => {
if (event.key === "Enter") {
const callsign = callsignInput.value.trim().toUpperCase()
if (callsign.length === 0) {
displayError("Please enter a call sign.")
} else if (callsign.length >= 3) {
lookupCallsign(callsign)
}
}
})
lookupButton.addEventListener('click', () => {
const callsign = callsignInput.value.trim().toUpperCase()
if (callsign.length === 0) {
displayError("Please enter a call sign.")
} else {
lookupCallsign(callsign)
}
})
aboutButton.addEventListener('click', () => {
const aboutPopup = document.getElementById('about-popup')
aboutPopup.classList.remove('hidden')
})
closeButton.addEventListener('click', () => {
const aboutPopup = document.getElementById('about-popup')
aboutPopup.classList.add('hidden')
})
})