forked from kartikvirendrar/stocksearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
156 lines (135 loc) · 4.59 KB
/
fetch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
let stocks = [];
let stockls;
document.getElementById("searchBar").addEventListener("keyup", (e) => {
let stock1 = stocks;
let a = 0;
for (const i in stock1) {
if (stock1[i].symbol.toLowerCase() != e.target.value.toLowerCase()) {
a += 1;
}
}
async function fetchapi() {
if (a == stock1.length) {
await fetch(
`https://stock-data-yahoo-finance-alternative.p.rapidapi.com/v6/finance/quote?symbols=${e.target.value}`,
{
method: "GET",
headers: {
"x-rapidapi-host":
"stock-data-yahoo-finance-alternative.p.rapidapi.com",
"x-rapidapi-key":
"a5d0d6aeebmshb035568326931a4p160069jsn76fdafc068cd",
},
}
)
.then((response) => {
return response.json();
})
.then((dataobj) => {
if (dataobj.quoteResponse.result.length != 0) {
stock1.push(dataobj.quoteResponse.result[0]);
}
})
.catch((err) => {
console.error(err);
});
}
const filterStocks = stock1.filter((stock) => {
if (stock.quoteType == "EQUITY") {
return (
stock.longName.toLowerCase().includes(e.target.value.toLowerCase()) ||
stock.symbol.toLowerCase().includes(e.target.value.toLowerCase())
);
}
});
displayStocks(filterStocks);
}
fetchapi().catch((e) => {
console.log(e);
});
});
fetch(
"https://stock-data-yahoo-finance-alternative.p.rapidapi.com/ws/screeners/v1/finance/screener/predefined/saved?scrIds=day_gainers&count=250",
{
method: "GET",
headers: {
"x-rapidapi-host": "stock-data-yahoo-finance-alternative.p.rapidapi.com",
"x-rapidapi-key": "a5d0d6aeebmshb035568326931a4p160069jsn76fdafc068cd",
},
}
)
.then((response) => {
return response.json();
})
.then((dataobj) => {
console.log(dataobj);
stocks = dataobj.finance.result[0].quotes;
displayStocks(stocks);
})
.catch((err) => {
console.error(err);
});
const displayStocks = (stocks) => {
let x = -1;
stockls = stocks;
const htmlString = stocks
.map((stocks) => {
x += 1;
return `
<div class="stocks" id="a${x}" onclick="let str = JSON.stringify(stockls[${x}]);
str = str.replace(/'/i, '');
str = str.replace(/{/i, '');
str = str.replace(/}/i, '');
var arr = str.split(',');
document.getElementById('details').innerHTML = arr.join('\\n');
document.getElementById('details').style.color = 'white'; ">
<div class="box1">
<h1 style="color:${
stocks.twoHundredDayAverageChangePercent.toFixed(2) >= 0
? "green"
: "#c73030eb"
};" >${stocks.symbol}</h1></div>
<div class="box2">
<h2>${stocks.longName}</h2><hr>
<h3>Price : ${stocks.regularMarketPrice} ${
stocks.currency
} | Range : ${stocks.regularMarketDayRange}</h3>
<h3>Regular Market Change : ${stocks.regularMarketChange.toFixed(
2
)} (${stocks.regularMarketChangePercent.toFixed(2)}%)</h3>
<h3>Average Change Percentage : ${stocks.twoHundredDayAverageChangePercent.toFixed(
2
)}%</h3></div>
</div><br><br>`;
})
.join("");
document.getElementById("stockList").innerHTML = htmlString;
};
////////////////////////////////////////////////
const stocksButton = document.querySelector("#stocks--link");
const stocksSection = document.querySelector("#stockList");
stocksButton.addEventListener("click", function (e) {
stocksSection.scrollIntoView({ behavior: "smooth" });
});
const contactButton = document.querySelector("#contact--link");
const footerSection = document.querySelector("#footer");
contactButton.addEventListener("click", function (e) {
footerSection.scrollIntoView({ behavior: "smooth" });
});
const aboutButton = document.querySelector("#about--link");
const aboutSection = document.querySelector("#about");
aboutButton.addEventListener("click", function (e) {
aboutSection.scrollIntoView({ behavior: "smooth" });
});
/////////////////////////////////////////////////////
const backToTopBtn = document.querySelector(".back-to-top--btn");
window.addEventListener("scroll", () => {
if (window.scrollY > 400) {
backToTopBtn.setAttribute("data-visible", true);
} else {
backToTopBtn.setAttribute("data-visible", false);
}
});
backToTopBtn.addEventListener("click", () =>
window.scrollTo({ top: 0, behavior: "smooth" })
);