-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
104 lines (93 loc) · 3.8 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
document.addEventListener('DOMContentLoaded', function() {
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
const continueButton = document.getElementById('continueButton');
const finishButton = document.getElementById('finishButton');
const navigationButtons = document.getElementById('navigationButtons');
const loadingSpinner = document.getElementById('loadingSpinner');
const loadingText = document.getElementById('loadingText');
const statusDiv = document.getElementById('status');
startButton.addEventListener('click', function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0].url.includes('https://www.etsy.com/your/orders/sold')) {
chrome.tabs.sendMessage(tabs[0].id, {action: "start"}, function(response) {
if (chrome.runtime.lastError) {
console.error('Error starting collection:', chrome.runtime.lastError.message);
updateStatus("Error: Please refresh the Etsy Sold Orders page and try again.");
} else if (response && response.status === "started") {
startButton.style.display = 'none';
stopButton.style.display = 'block';
showLoading("Processing current page...");
updateStatus("Collection started...");
}
});
} else {
updateStatus("Please navigate to the Etsy Sold Orders page before starting.");
}
});
});
stopButton.addEventListener('click', function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "stop"}, function(response) {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
updateStatus("Error: Please refresh the Etsy Sold Orders page and try again.");
} else if (response && response.status === "stopped") {
resetUI();
updateStatus("Collection stopped.");
}
});
});
});
continueButton.addEventListener('click', function() {
sendResponse({proceed: true});
navigationButtons.style.display = 'none';
showLoading("Loading next page...");
});
finishButton.addEventListener('click', function() {
sendResponse({proceed: false});
resetUI();
updateStatus("Finishing collection. Preparing CSV download...");
});
function updateStatus(message) {
statusDiv.textContent = message;
}
function resetUI() {
startButton.style.display = 'block';
stopButton.style.display = 'none';
navigationButtons.style.display = 'none';
hideLoading();
}
function showLoading(message) {
loadingText.textContent = message;
loadingSpinner.style.display = 'block';
navigationButtons.style.display = 'none';
}
function hideLoading() {
loadingSpinner.style.display = 'none';
}
let pendingResponse = null;
// Listen for updates from content script and background script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "updateStatus") {
updateStatus(request.status);
} else if (request.action === "promptNextPage") {
hideLoading();
stopButton.style.display = 'none';
navigationButtons.style.display = 'block';
updateStatus(`Collected ${request.totalEmails} emails from ${request.currentPage} page(s).`);
pendingResponse = sendResponse;
return true; // Indicates that the response is sent asynchronously
} else if (request.action === "showLoading") {
showLoading(request.message || "Processing...");
} else if (request.action === "hideLoading") {
hideLoading();
}
});
function sendResponse(response) {
if (pendingResponse) {
pendingResponse(response);
pendingResponse = null;
}
}
});