-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (50 loc) · 1.58 KB
/
script.js
File metadata and controls
60 lines (50 loc) · 1.58 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
// API Functions
const API_KEY = "EFwBT80Puc8F6LcUaEeLv2JzeVW8BGOd6zYGf6RR";
// DOM variables
const button = document.querySelector(".chart-button");
const input = document.querySelector(".ticker-input");
const tickerInfoColumn = document.querySelector("#ticker-components-col");
const template = document.querySelector(".info-template");
async function getChart(ticker) {
const url = `https://yfapi.net/v8/finance/chart/${ticker}`;
const options = {
method: "GET",
withCredentials: true,
headers: {
"x-api-key": API_KEY,
"Content-Type": "application/json"
}
};
try {
const response = await fetch(url, options);
const chart = await response.json();
return chart.chart.result[0].meta;
} catch (err) {
return null;
}
}
async function handleButton() {
// Get text in input field
const ticker = input.value.trim();
// If empty, do nothing
if (ticker === "") return;
const result = await getChart(ticker);
// If ticker cannot be found
if (result === null) {
alert(`Ticker ${ticker} cannot be found`);
} else {
let symbol = result.symbol;
let currency = result.currency;
let chartPreviousClose = result.chartPreviousClose;
// Clone tempate
let newTickerInfo = template.content.cloneNode(true);
// Set values
newTickerInfo.querySelector(".symbol").textContent = symbol;
newTickerInfo.querySelector(".currency").textContent = currency;
newTickerInfo.querySelector(".chartPreviousClose").textContent = chartPreviousClose;
// Append to column
tickerInfoColumn.appendChild(newTickerInfo);
}
}
// Add event listener
button.addEventListener("click", handleButton);