-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpopup.js
236 lines (208 loc) · 7.04 KB
/
popup.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
function loadGlobalsThenExecute(tabId, secondaryScript, callback) {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: ["globals.js", "utils.js"],
},
() => {
chrome.scripting.executeScript(
{
target: { tabId: tabId },
files: [secondaryScript],
},
callback
);
}
);
}
function addButtonListener(buttonId, scriptName) {
document.getElementById(buttonId).addEventListener("click", () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (tab) {
if (buttonId === "bulk-delete") {
const bulkDeleteButton = document.getElementById(buttonId);
bulkDeleteButton.disabled = true;
bulkDeleteButton.classList.add("progress");
loadGlobalsThenExecute(tab.id, scriptName);
} else {
loadGlobalsThenExecute(tab.id, scriptName);
}
}
});
});
}
function updateProgressBar(buttonId, progress) {
console.log(`Updating progress bar for ${buttonId}:`, progress);
const button = document.getElementById(buttonId);
button.classList.add("progress");
button.style.setProperty("--progress", `${progress}%`);
button.setAttribute("data-progress", progress);
const buttonText =
buttonId === "bulk-delete" ? "Bulk Delete" : "Bulk Archive";
const actionText = buttonId === "bulk-delete" ? "Deleting" : "Archiving";
if (progress === 100) {
button.disabled = true;
button.innerHTML = `
<span class="progress-text">100%</span>
<span class="button-text">${actionText} Complete</span>
`;
// 显示 100% 一段时间后恢复原始状态
setTimeout(() => {
button.disabled = false;
button.classList.remove("progress");
button.innerHTML = `<span class="button-text">${buttonText}</span>`;
}, 500); // 1000 毫秒 = 1 秒,您可以根据需要调整这个时间
} else {
button.disabled = true;
button.innerHTML = `
<span class="progress-text">${progress}%</span>
<span class="button-text">${actionText}...</span>
`;
}
}
// 在消息监听器中也添加文本重置
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log("Received message:", request);
if (request.action === "updateProgress") {
updateProgressBar(request.buttonId, request.progress);
} else if (request.action === "operationComplete") {
const button = document.getElementById(request.buttonId);
button.disabled = false;
button.classList.remove("progress");
updateProgressBar(request.buttonId, 100);
}
});
function initializeButtons() {
addButtonListener("add-checkboxes", "addCheckboxes.js");
addButtonListener("bulk-delete", "bulkDeleteConversations.js");
addButtonListener("toggle-checkboxes", "toggleCheckboxes.js");
addButtonListener("remove-checkboxes", "removeCheckboxes.js");
const bulkArchiveButton = document.getElementById("bulk-archive");
bulkArchiveButton.addEventListener("click", handleBulkArchive);
}
const storageKey = "BulkDeleteChatGPT_isPaid";
async function checkMembershipStatus() {
const localIsPaid = localStorage.getItem(storageKey) === "true";
updateBulkArchiveButton(localIsPaid);
const userInfo = await getUserInfo();
if (!userInfo) {
console.error("Unable to get user info");
return;
}
try {
const response = await fetch(
`https://bulk-delete-chatgpt-worker.qcrao.com/check-payment-status?user_id=${encodeURIComponent(
userInfo.id
)}`
);
const data = await response.json();
// 更新本地存储和按钮状态
localStorage.setItem(storageKey, data.isPaid);
updateBulkArchiveButton(data.isPaid);
} catch (error) {
console.error("Error checking membership status:", error);
}
}
function updateBulkArchiveButton(isPaid) {
const bulkArchiveButton = document.getElementById("bulk-archive");
if (isPaid) {
bulkArchiveButton.querySelector("span").textContent = "";
} else {
bulkArchiveButton.querySelector("span").textContent = "🔒";
}
}
async function handleBulkArchive() {
const localIsPaid = localStorage.getItem(storageKey) === "true";
if (localIsPaid) {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (tab) {
const bulkArchiveButton = document.getElementById("bulk-archive");
bulkArchiveButton.disabled = true;
bulkArchiveButton.classList.add("progress");
loadGlobalsThenExecute(tab.id, "bulkArchiveConversations.js");
}
});
return;
}
const userInfo = await getUserInfo();
if (!userInfo) {
console.error("Unable to get user info");
alert("Unable to verify user. Please try again later.");
return;
}
const response = await fetch(
`https://bulk-delete-chatgpt-worker.qcrao.com/check-payment-status?user_id=${encodeURIComponent(
userInfo.id
)}`
);
const data = await response.json();
if (data.isPaid) {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
if (tab) {
const bulkArchiveButton = document.getElementById("bulk-archive");
bulkArchiveButton.disabled = true;
bulkArchiveButton.classList.add("progress");
loadGlobalsThenExecute(tab.id, "bulkArchiveConversations.js");
}
});
} else {
showModal().then(async (result) => {
if (result) {
const payResponse = await fetch(
`https://bulk-delete-chatgpt-worker.qcrao.com/pay-bulk-archive?user_id=${encodeURIComponent(
userInfo.id
)}`,
{ method: "POST" }
);
const payData = await payResponse.json();
console.log("payData", payData);
if (payData.paymentUrl) {
window.open(payData.paymentUrl, "_blank");
} else {
alert("Failed to get payment link. Please try again later.");
}
}
});
}
}
function showModal() {
return new Promise((resolve) => {
const modal = document.getElementById("customModal");
const okButton = document.getElementById("modalOK");
const cancelButton = document.getElementById("modalCancel");
modal.style.display = "block";
okButton.onclick = () => {
modal.style.display = "none";
resolve(true);
};
cancelButton.onclick = () => {
modal.style.display = "none";
resolve(false);
};
window.onclick = (event) => {
if (event.target == modal) {
modal.style.display = "none";
resolve(false);
}
};
});
}
// function updateCopyrightYear() {
// const currentYear = new Date().getFullYear();
// document.getElementById(
// "copyright"
// ).innerHTML = `© ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
// }
document.addEventListener("DOMContentLoaded", function () {
initializeButtons();
// updateCopyrightYear();
checkMembershipStatus();
});
// 每次打开popup时检查会员状态
chrome.runtime.onConnect.addListener(function (port) {
if (port.name === "popup") {
port.onDisconnect.addListener(function () {
checkMembershipStatus();
});
}
});