-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
250 lines (211 loc) · 8.59 KB
/
script.js
File metadata and controls
250 lines (211 loc) · 8.59 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// ----------------------------
// script.js
// ----------------------------
window.addEventListener('DOMContentLoaded', () => {
const BTCAddressInput = document.getElementById("BTCAddressInput");
const serverSelect = document.getElementById("serverUrl");
const validPools = [
"https://eusolo.ckpool.org/users/",
"https://solo.ckpool.org/users/",
"https://ausolo.ckpool.org/users/"
];
// Lokale Daten laden
const savedBTCAddress = localStorage.getItem("BTCAddressInput");
const savedServer = localStorage.getItem("serverUrl");
if (savedBTCAddress) BTCAddressInput.value = savedBTCAddress;
if (savedServer && validPools.includes(savedServer)) serverSelect.value = savedServer;
// Pool-Auswahl speichern
serverSelect.addEventListener("change", () => {
if (validPools.includes(serverSelect.value)) {
localStorage.setItem("serverUrl", serverSelect.value);
}
});
});
// ----------------------------
// Globals
// ----------------------------
let lastNetworkDiffFetch = 0;
let cachedNetworkDiff = null;
let lastWorkerFetch = 0;
let cachedWorkerData = null;
let cachedBTCAddressInput = null;
const validPools = [
"https://eusolo.ckpool.org/users/",
"https://solo.ckpool.org/users/",
"https://ausolo.ckpool.org/users/"
];
// ----------------------------
// Validierung Bitcoin-Adresse
// ----------------------------
function isValidBitcoinAddress(address) {
return /^(bc1q|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/i.test(address);
}
// ----------------------------
// Netzwerk-Schwierigkeit
// ----------------------------
async function getNetworkDiff() {
const url = 'https://blockchain.info/q/getdifficulty?cors=true';
const now = Date.now();
if (cachedNetworkDiff !== null && (now - lastNetworkDiffFetch) < 60000) {
return cachedNetworkDiff;
}
try {
const res = await fetch(url);
const text = await res.text();
const diff = parseFloat(text);
cachedNetworkDiff = diff;
lastNetworkDiffFetch = now;
return diff;
} catch (err) {
console.error("Error fetching network difficulty:", err);
return cachedNetworkDiff; // fallback
}
}
// ----------------------------
// Worker Daten abrufen
// ----------------------------
async function getWorkerData(BTCAddressInput) {
const now = Date.now();
// Cache prüfen
if (cachedWorkerData && cachedBTCAddressInput === BTCAddressInput && (now - lastWorkerFetch) < 60000) {
return cachedWorkerData;
}
let serverUrl = localStorage.getItem("serverUrl");
if (!validPools.includes(serverUrl)) {
serverUrl = "https://eusolo.ckpool.org/users/";
}
// 1️⃣ API versuchen
const apiUrl = serverUrl + encodeURIComponent(BTCAddressInput) + "?json";
try {
const res = await fetch(apiUrl);
const data = await res.json();
if (data.worker && data.worker.length) {
cachedWorkerData = data;
cachedBTCAddressInput = BTCAddressInput;
lastWorkerFetch = now;
return data;
}
} catch (err) {
console.warn("API JSON failed, falling back to HTML parsing");
}
// 2️⃣ Fallback: HTML parsen
try {
const htmlUrl = serverUrl + encodeURIComponent(BTCAddressInput);
const proxyUrl = 'https://api.allorigins.win/raw?url=' + encodeURIComponent(htmlUrl);
const res = await fetch(proxyUrl);
const text = await res.text();
const jsonMatch = text.match(/{[\s\S]*"workername"[\s\S]*}/);
if (!jsonMatch) throw new Error('No JSON data found');
const data = JSON.parse(jsonMatch[0]);
cachedWorkerData = data;
cachedBTCAddressInput = BTCAddressInput;
lastWorkerFetch = now;
return data;
} catch (err) {
console.error("Error fetching worker data:", err);
return null;
}
}
// ----------------------------
// Update Dashboard
// ----------------------------
async function updateData() {
const nowStr = new Date().toLocaleString();
document.getElementById("lastupdate").textContent = nowStr;
const BTCAddressInput = document.getElementById("BTCAddressInput").value.trim();
// Validierung
if (!isValidBitcoinAddress(BTCAddressInput)) {
resetDashboard("❌ Invalid Bitcoin address!");
return;
}
localStorage.setItem("BTCAddressInput", BTCAddressInput);
try {
const networkDiff = await getNetworkDiff();
const data = await getWorkerData(BTCAddressInput);
if (!data || !data.worker || !data.worker.length) throw new Error("No data found");
const worker = data.worker[0];
// Dashboard ausfüllen
document.getElementById("worker").textContent = worker.workername;
document.getElementById("lastshare").textContent = worker.lastshare;
document.getElementById("shares").textContent = worker.shares;
const bestShareT = worker.bestshare / 1e12;
const bestEverT = worker.bestever / 1e12;
const networkDiffT = networkDiff / 1e12;
document.getElementById("bestShare").textContent = bestShareT.toFixed(6) + " T";
document.getElementById("bestEver").textContent = bestEverT.toFixed(6) + " T";
document.getElementById("networkDiff").textContent = networkDiffT ? networkDiffT.toFixed(2) + " T" : "–";
document.getElementById("hashrate1m").textContent = parseFloat(worker.hashrate1m) + " TH/s";
document.getElementById("hashrate5m").textContent = parseFloat(worker.hashrate5m) + " TH/s";
document.getElementById("hashrate1hr").textContent = parseFloat(worker.hashrate1hr) + " TH/s";
document.getElementById("hashrate1d").textContent = parseFloat(worker.hashrate1d) + " TH/s";
document.getElementById("hashrate7d").textContent = parseFloat(worker.hashrate7d) + " TH/s";
// Fortschrittsbalken
const percent = networkDiff ? (bestShareT / networkDiffT) * 100 : 0;
const bar = document.getElementById("progressBar");
bar.style.width = Math.min(percent, 100) + "%";
bar.className = "bar " + (percent >= 100 ? "green" : percent >= 75 ? "yellow" : "red");
document.getElementById("percent").textContent = "Best Shot in percent: " + percent.toFixed(5) + " %";
// Chancen berechnen
function odds(myHashrateTH, networkDiff, days) {
const myHashrate = myHashrateTH * 1e12;
const networkHashrate = networkDiff * Math.pow(2, 32) / 600;
const pPerBlock = myHashrate / networkHashrate;
const blocks = days * 144;
const pTotal = 1 - Math.pow(1 - pPerBlock, blocks);
const percent = pTotal * 100;
return {
percentStr: percent.toFixed(3) + " %",
oneInStr: percent > 0 ? "1:" + Math.round(100 / percent) : "–"
};
}
const dayOdds = odds(parseFloat(worker.hashrate1d), networkDiff, 1);
const weekOdds = odds(parseFloat(worker.hashrate1d), networkDiff, 7);
const monthOdds = odds(parseFloat(worker.hashrate1d), networkDiff, 30);
const yearOdds = odds(parseFloat(worker.hashrate1d), networkDiff, 365);
document.getElementById("oddsDayPercent").textContent = dayOdds.percentStr;
document.getElementById("oddsDayChance").textContent = dayOdds.oneInStr;
document.getElementById("oddsWeekPercent").textContent = weekOdds.percentStr;
document.getElementById("oddsWeekChance").textContent = weekOdds.oneInStr;
document.getElementById("oddsMonthPercent").textContent = monthOdds.percentStr;
document.getElementById("oddsMonthChance").textContent = monthOdds.oneInStr;
document.getElementById("oddsYearPercent").textContent = yearOdds.percentStr;
document.getElementById("oddsYearChance").textContent = yearOdds.oneInStr;
// Details öffnen
document.querySelectorAll('.details-odds, .details-shares').forEach(d => d.open = true);
} catch (err) {
console.error("Error loading data:", err);
resetDashboard("Invalid or not found!");
}
}
// ----------------------------
// Dashboard reset
// ----------------------------
function resetDashboard(message) {
const resetFields = [
"lastshare","shares","bestShare","bestEver",
"networkDiff","hashrate1m","hashrate5m","hashrate1hr",
"hashrate1d","hashrate7d","percent",
"oddsDayPercent","oddsDayChance","oddsWeekPercent",
"oddsWeekChance",
"oddsMonthPercent","oddsMonthChance",
"oddsYearPercent","oddsYearChance"
];
document.getElementById("worker").textContent = message;
resetFields.forEach(id => {
const el = document.getElementById(id);
if(el) el.textContent = id === "percent" ? "Best Shot in percent: –" : "–";
});
const bar = document.getElementById("progressBar");
if(bar) bar.style.width = "0%";
if(bar) bar.className = "bar red";
}
// ----------------------------
// Events
// ----------------------------
document.getElementById('updateBtn').onclick = updateData;
document.getElementById('BTCAddressInput').onkeydown = function(e) {
if (e.key === 'Enter') {
e.preventDefault();
updateData();
}
};