-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
148 lines (127 loc) · 5.01 KB
/
popup.js
File metadata and controls
148 lines (127 loc) · 5.01 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
// popup.js
// RarePatternsScanHelperExtension
// Copyright (C) 2025 Romanus101
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
document.addEventListener("DOMContentLoaded", () => {
// Подтянуть последние результаты при открытии
chrome.runtime.sendMessage({ type: "get_results" }, (resp) => {
if (resp?.data) renderResults(resp.data);
});
});
document.getElementById("start_scanning").addEventListener("click", async () => {
let delay = parseInt(document.getElementById("delay_input").value, 10) || 1000;
if (delay < 3000) {
delay = 3000;
document.getElementById("delay_input").value = delay;
}
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.tabs.sendMessage(tab.id, { type: "start", delay });
setStatus("Scanning started");
});
document.getElementById("stop_scanning").addEventListener("click", async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.tabs.sendMessage(tab.id, { type: "stop" });
setStatus("Stop requested");
});
document.getElementById("export_results").addEventListener("click", () => {
chrome.runtime.sendMessage({ type: "get_results" }, (resp) => {
const data = resp?.data || [];
if (!data.length) return;
const format = document.getElementById("export_format")?.value || "csv";
if (format === "csv") {
exportToCSV(data);
} else if (format === "json") {
exportToJSON(data);
} else if (format === "txt") {
exportToTXT(data);
}
});
});
// === Export helpers ===
function exportToCSV(data) {
const header = "page,name,pattern,float,price";
const rows = data.map(r => `${r.page},${r.name},${r.pattern},${r.float},${r.price}`);
const csv = [header, ...rows].join("\n");
downloadFile(csv, "patterns.csv", "text/csv");
}
function exportToJSON(data) {
const json = JSON.stringify(data, null, 2);
downloadFile(json, "patterns.json", "application/json");
}
function exportToTXT(data) {
const txt = data.map(r => `${r.page} | ${r.name} | Pattern ${r.pattern} | Float ${r.float} | ${r.price}`).join("\n");
downloadFile(txt, "patterns.txt", "text/plain");
}
function downloadFile(content, filename, type) {
const blob = new Blob([content], { type });
const url = URL.createObjectURL(blob);
chrome.downloads.download({ url, filename });
}
// Рисуем результаты в реальном времени + статус
chrome.runtime.onMessage.addListener((msg) => {
if (msg.type === "results") {
renderResults(msg.data || []);
setStatus(`Found: ${msg.data?.length || 0}`);
}
if (msg.type === "progress") {
setStatus(msg.text);
}
});
// Helpers
function renderResults(data) {
const ul = document.getElementById("results_list");
ul.innerHTML = "";
data.forEach((item) => {
const li = document.createElement("li");
li.textContent = `${item.name} | Pattern ${item.pattern} | Float ${item.float} | ${item.price}`;
ul.appendChild(li);
});
}
function setStatus(text) {
// Если добавишь в popup.html <div id="status"></div>, будет отображаться.
const el = document.getElementById("status");
if (el) el.textContent = text;
}
//Progress bar
function renderProgressBar(totalPages) {
const bar = document.getElementById("progressbar");
if (!bar) return;
bar.innerHTML = "";
for (let i = 1; i <= totalPages; i++) {
const seg = document.createElement("div");
seg.className = "segment";
bar.appendChild(seg);
}
}
function updateProgress(currentPage) {
const segments = document.querySelectorAll("#progressbar .segment");
segments.forEach((seg, idx) => {
if (idx < currentPage) seg.classList.add("active");
else seg.classList.remove("active");
});
}
chrome.runtime.onMessage.addListener((msg) => {
if (msg.type === "results") {
renderResults(msg.data || []);
setStatus(`Found: ${msg.data?.length || 0}`);
}
if (msg.type === "progress") {
setStatus(msg.text);
}
if (msg.type === "progress_page") {
renderProgressBar(msg.total);
updateProgress(msg.current);
}
});