-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
403 lines (351 loc) · 11.7 KB
/
monitor.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
let previousTokens = new Set();
let refreshCount = 0;
let tokenCount = 0;
let startTime = null;
let refreshInterval = null;
let audio = null;
let monitoringTabId = null;
let isFirstLoad = true;
let chatIdStorage = {
get: async () => {
if (chrome.storage) {
return new Promise((resolve) => {
chrome.storage.local.get("telegramChatId", (data) => {
resolve(data.telegramChatId || "");
});
});
} else {
return localStorage.getItem("telegramChatId") || "";
}
},
set: async (value) => {
if (chrome.storage) {
return new Promise((resolve) => {
chrome.storage.local.set({ telegramChatId: value }, resolve);
});
} else {
localStorage.setItem("telegramChatId", value);
}
},
};
const TELEGRAM_CONFIG = {
botToken: "7702772482:AAHDaRaNJXKExgotEOH98SRsiPCb9IMDx9g",
};
// DOM elements
const volumeSlider = document.getElementById("volume");
const volumeLevel = document.getElementById("volume-level");
const searchTemplate = document.getElementById("search-template");
const searchPreview = document.getElementById("search-preview");
const alertSoundSelect = document.getElementById("alert-sound");
const startButton = document.getElementById("start-button");
const testSoundButton = document.getElementById("test-sound");
const telegramChatId = document.getElementById("telegram-chat-id");
const testTelegramBtn = document.getElementById("test-telegram");
const telegramStatus = document.getElementById("telegram-status");
const autoSearchToggle = document.getElementById("auto-search");
// Set default search template
searchTemplate.value = "XRP {ticker} twitter";
// Load saved auto-search preference
chrome.storage.local.get("autoSearch", (data) => {
autoSearchToggle.checked = data.autoSearch !== false; // Default to true if not set
});
// Save auto-search preference when changed
autoSearchToggle.addEventListener("change", (e) => {
chrome.storage.local.set({ autoSearch: e.target.checked });
});
// Event Listeners
volumeSlider.addEventListener("input", (e) => {
const volume = e.target.value;
if (audio) {
audio.volume = volume / 100;
}
volumeLevel.textContent = volume + "%";
});
alertSoundSelect.addEventListener("change", (e) => {
if (audio) {
audio.src = chrome.runtime.getURL(e.target.value);
playTestSound();
}
});
searchTemplate.addEventListener("input", updateSearchPreview);
testSoundButton.addEventListener("click", () => {
playTestSound();
});
startButton.addEventListener("click", async () => {
if (!audio) initializeAudio();
try {
await audio.play();
audio.pause();
audio.currentTime = 0;
console.log("Audio test successful");
} catch (e) {
console.error("Audio test failed:", e);
alert(
"Please click somewhere on the page to enable sound notifications"
);
return;
}
if (!startTime) {
startTime = new Date();
document.getElementById("start-time").textContent =
startTime.toLocaleTimeString();
startButton.textContent = "Stop Monitoring";
isFirstLoad = true;
startMonitoring();
} else if (refreshInterval) {
clearInterval(refreshInterval);
refreshInterval = null;
startButton.textContent = "Resume Monitoring";
} else {
startButton.textContent = "Stop Monitoring";
isFirstLoad = true;
startMonitoring();
}
});
async function sendTelegramAlert(tokenName, isTest = false) {
try {
// Get the stored chat ID
const chatId = await chatIdStorage.get();
if (!chatId) {
console.log("No Telegram chat ID configured");
return;
}
const message = isTest
? "🔔 Test Message: Your Telegram notifications are working!"
: `🔥 New Token Alert\n\nToken: ${tokenName}\nTime: ${new Date().toLocaleTimeString()}\nSearch: ${searchUrl}`;
const url = `https://api.telegram.org/bot${TELEGRAM_CONFIG.botToken}/sendMessage`;
console.log("Sending Telegram message to chat:", chatId);
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_id: chatId,
text: message,
disable_web_page_preview: false,
}),
});
const data = await response.json();
if (!response.ok || !data.ok) {
throw new Error(data.description || "Unknown Telegram error");
}
return true;
} catch (e) {
console.error("Telegram error details:", {
error: e.message,
tokenName: tokenName,
});
throw e;
}
}
(async () => {
const savedChatId = await chatIdStorage.get();
if (savedChatId) {
telegramChatId.value = savedChatId;
}
})();
telegramChatId.addEventListener("change", async () => {
const chatId = telegramChatId.value.trim();
await chatIdStorage.set(chatId);
});
// Test Telegram button
testTelegramBtn.addEventListener("click", async () => {
const chatId = telegramChatId.value.trim();
if (!chatId) {
telegramStatus.textContent = "Please enter a chat ID first";
telegramStatus.style.color = "#f44336";
return;
}
// Save the chat ID first before testing
await chatIdStorage.set(chatId);
telegramStatus.textContent = "Sending test message...";
try {
await sendTelegramAlert("TEST_MESSAGE", true);
telegramStatus.textContent = "Test message sent successfully!";
telegramStatus.style.color = "#4CAF50";
} catch (e) {
console.error("Telegram test failed:", e);
telegramStatus.textContent = `Failed to send: ${e.message}`;
telegramStatus.style.color = "#f44336";
}
});
function initializeAudio() {
audio = new Audio(chrome.runtime.getURL(alertSoundSelect.value));
audio.volume = volumeSlider.value / 100;
}
function playTestSound() {
if (!audio) initializeAudio();
if (audio) {
audio.currentTime = 0;
audio.play().catch((e) => {
console.error("Audio play error details:", {
error: e.name,
message: e.message,
code: e.code,
});
});
}
}
function updateSearchPreview() {
const template = searchTemplate.value;
searchPreview.textContent = template.replace("{ticker}", "$EXAMPLE");
}
function updateStats() {
document.getElementById("refresh-count").textContent = refreshCount;
document.getElementById("token-count").textContent = tokenCount;
if (startTime) {
const hoursElapsed = (new Date() - startTime) / (1000 * 60 * 60);
const tokensPerHour = (tokenCount / hoursElapsed).toFixed(2);
document.getElementById("tokens-per-hour").textContent = tokensPerHour;
}
}
async function createMonitoringTab() {
const tab = await chrome.tabs.create({
url: "https://firstledger.net/tokens",
active: false,
});
monitoringTabId = tab.id;
console.log("Created monitoring tab:", tab.id);
return tab;
}
async function extractTokens() {
try {
if (!monitoringTabId) {
console.error("No monitoring tab found");
return [];
}
const result = await chrome.scripting.executeScript({
target: { tabId: monitoringTabId },
func: () => {
const tokens = [];
const elements = document.querySelectorAll("a.grid");
elements.forEach((element) => {
const titleDiv = element.querySelector(
".description .title"
);
if (titleDiv) {
tokens.push({ name: titleDiv.textContent.trim() });
}
});
return tokens;
},
});
return result[0].result;
} catch (e) {
console.error("Error extracting tokens:", e);
return [];
}
}
async function startMonitoring() {
if (!monitoringTabId) {
await createMonitoringTab();
}
refreshInterval = setInterval(async () => {
try {
await chrome.tabs.reload(monitoringTabId);
refreshCount++;
updateStats();
await new Promise((resolve) => setTimeout(resolve, 1000));
const tokens = await extractTokens();
if (Array.isArray(tokens)) {
checkForNewTokens(tokens);
}
} catch (e) {
console.error("Error in monitoring cycle:", e);
if (e.message.includes("No tab with id")) {
monitoringTabId = null;
await createMonitoringTab();
}
}
}, 3000);
}
async function checkForNewTokens(tokens) {
if (!Array.isArray(tokens)) {
console.error("Invalid tokens data in checkForNewTokens:", tokens);
return;
}
tokens.forEach(async (token) => {
if (!token || !token.name) {
console.warn("Invalid token object:", token);
return;
}
const tokenKey = token.name;
if (!previousTokens.has(tokenKey)) {
if (!isFirstLoad) {
tokenCount++;
updateStats();
if (audio) {
audio.currentTime = 0;
audio.play().catch((e) => {
console.error("Audio play error details:", {
error: e.name,
message: e.message,
code: e.code,
state: audio.readyState,
audioSrc: audio.src,
});
});
}
if (autoSearchToggle.checked) {
const searchQuery = searchTemplate.value.replace(
"{ticker}",
token.name
);
window.open(
`https://www.google.com/search?q=${encodeURIComponent(
searchQuery
)}`,
"_blank"
);
}
chrome.runtime.sendMessage({
type: "NEW_TOKEN",
tokenName: token.name,
});
await sendTelegramAlert(token.name);
}
previousTokens.add(tokenKey);
}
});
if (isFirstLoad) {
console.log(
"Initial token list loaded:",
previousTokens.size,
"tokens"
);
isFirstLoad = false;
}
}
// Initialize audio on page load with user gesture
document.addEventListener(
"click",
function initAudio() {
if (!audio) {
initializeAudio();
audio
.play()
.then(() => {
audio.pause();
audio.currentTime = 0;
document.removeEventListener("click", initAudio);
})
.catch(console.error);
}
},
{ once: true }
);
function setupCopyButton(elementId) {
document.getElementById(elementId).addEventListener("click", function () {
navigator.clipboard.writeText(this.textContent).then(() => {
const originalText = this.textContent;
this.textContent = "Copied!";
setTimeout(() => {
this.textContent = originalText;
}, 1000);
});
});
}
// Initial setup
setupCopyButton("xrpAddress");
updateSearchPreview();