Repository navigation
Expand file tree
/
Copy pathstocks.js
More file actions
170 lines (155 loc) · 7.71 KB
/
Copy pathstocks.js
File metadata and controls
170 lines (155 loc) · 7.71 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Lưu danh sách cổ phiếu vào localStorage
function saveStockList(stockList) {
localStorage.setItem('stockList', JSON.stringify(stockList));
}
// Lấy danh sách cổ phiếu từ localStorage
function getStockList() {
const stockList = localStorage.getItem('stockList');
return stockList ? JSON.parse(stockList) : [];
}
// Thêm mã cổ phiếu vào danh sách
function addStockToList(stockCode) {
const stockList = getStockList();
if (!stockList.includes(stockCode)) {
stockList.push(stockCode);
saveStockList(stockList);
console.log(`Đã thêm mã cổ phiếu: ${stockCode}`);
} else {
console.log(`Mã cổ phiếu ${stockCode} đã có trong danh sách.`);
}
fetchAndUpdateStockPrices();
}
// Xóa mã cổ phiếu khỏi danh sách
function removeStockFromList(stockCode) {
const stockList = getStockList();
const updatedList = stockList.filter(code => code !== stockCode);
saveStockList(updatedList);
console.log(`Đã xóa mã cổ phiếu: ${stockCode}`);
fetchAndUpdateStockPrices();
}
// Lấy giá cổ phiếu từ API
async function fetchStockPrices(stockList) {
const STOCK_API_URL = 'https://mastrade.masvn.com/api/v1/market/symbolLatest';
try {
const response = await fetch(`${STOCK_API_URL}?symbolList=${stockList.join(',')}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Lỗi khi lấy dữ liệu giá cổ phiếu:', error);
return [];
}
}
// Cập nhật giá cổ phiếu và hiển thị
async function fetchAndUpdateStockPrices() {
const stockList = getStockList();
if (stockList.length === 0) {
displayStockTicker([]);
return;
}
const prices = await fetchStockPrices(stockList);
const stockData = stockList.map(stockCode => {
const stockInfo = prices.find(item => item.s === stockCode) || {};
const price = stockInfo.c || 'N/A';
const change = changeFormatValue(stockInfo.r) || '0.0';
const changeColor = change > 0 ? '#27AE60' : change < 0 ? '#E74C3C' : '#BDC3C7';
return `<div class="item">${stockCode}: <span style="color: white;">${price}</span> <span style="color: ${changeColor};">(${change}%)</span></div>`;
});
displayStockTicker(stockData);
}
function changeFormatValue(value) {
var DEFAULT_LOCALE = 'en-GB';
var FORMAT_NUMBER = new Intl.NumberFormat(DEFAULT_LOCALE, {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
if (!isNaN(value) && (value !== '')) {
return FORMAT_NUMBER.format(parseFloat(value) * 100);
} else {
return '';
}
}
// Hiển thị danh sách dưới dạng dòng chạy
function displayStockTicker(stockData) {
let stockTicker = document.getElementById('stockTicker');
if (!stockTicker) {
// Tạo thanh hiển thị nếu chưa tồn tại
stockTicker = document.createElement('div');
stockTicker.id = 'stockTicker';
stockTicker.style.position = 'fixed';
stockTicker.style.bottom = '0';
stockTicker.style.left = '0';
stockTicker.style.width = '100%';
stockTicker.style.background = '#2C3E50'; // Màu nền xanh nhạt
stockTicker.style.color = '#FFFFFF'; // Màu chữ trắng
stockTicker.style.fontSize = '16px';
stockTicker.style.overflow = 'hidden';
stockTicker.style.whiteSpace = 'nowrap';
// stockTicker.style.padding = '10px 0';
stockTicker.style.display = 'flex';
stockTicker.style.alignItems = 'center';
stockTicker.style.justifyContent = 'space-between'
document.body.appendChild(stockTicker);
}
// Cập nhật danh sách cổ phiếu
stockTicker.innerHTML = `
<div id="stockTickers" style="height: 20px; overflow: hidden;">
<div id="stockList">${stockData.join(' ') || "Không có cổ phiếu nào trong danh sách."}</div>
</div><input id="stockInput" type="text" placeholder="Nhập mã cổ phiếu" style="margin-left: 6px; width: 150px;padding: 6px; font-size: 14px;"/>
`;
// Gắn sự kiện xử lý cho ô input
const stockInput = document.getElementById('stockInput');
stockInput.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
const stockCode = stockInput.value.trim().toUpperCase();
if (stockCode) {
const stockList = getStockList();
if (stockList.includes(stockCode)) {
removeStockFromList(stockCode); // Nếu đã tồn tại, xóa
} else {
addStockToList(stockCode); // Nếu chưa tồn tại, thêm
}
stockInput.value = ''; // Xóa nội dung input sau khi xử lý
}
scrollStocks();
}
});
// Tạo hàm cuộn
var interval;
scrollStocks();
window.addEventListener('resize', scrollStocks);
var currentIndex = 0;
function scrollStocks() {
clearInterval(interval);
var stockList = document.getElementById('stockList');
var stockTickers = document.getElementById('stockTickers');
var stockItems = stockList.children;
var width = stockTickers.offsetWidth;
var items = stockList.getElementsByClassName('item')[0];
var itemWidth = items.offsetWidth;;
var totalItems = Math.round(stockItems.length * itemWidth / width);
currentIndex++;
if (currentIndex >= totalItems) {
// Cuộn thêm một dòng giả
stockList.style.transform = 'translateY(-' + (currentIndex * 20) + 'px)';
setTimeout(function() {
// Đặt lại vị trí cuộn về đầu danh sách
stockList.style.transition = 'none';
stockList.style.transform = 'translateY(0)';
currentIndex = 0;
setTimeout(function() {
stockList.style.transition = 'transform 1s ease-in-out';
}, 50);
}, 2000); // Thời gian hiệu ứng cuộn
} else {
stockList.style.transform = 'translateY(-' + (currentIndex * 20) + 'px)';
}
interval = setInterval(scrollStocks, 4000);
}
}
// Tự động hiển thị ticker và cập nhật giá mỗi 60 giây
document.addEventListener('DOMContentLoaded', () => {
if (/Mobi|Android/i.test(navigator.userAgent) == false) {
//fetchAndUpdateStockPrices();
// setInterval(fetchAndUpdateStockPrices, 60000); // Cập nhật giá mỗi 60 giây
}
});