-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
143 lines (130 loc) · 4.34 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
// popup.js
document.addEventListener("DOMContentLoaded", () => {
const generateAuditButton = document.getElementById(
"generate-audit-purchase-orders-button",
);
const fileInputClient = document.getElementById("file-input-client");
const fileInputSupplier = document.getElementById("file-input-supplier");
const generateListButtonClient = document.getElementById(
"generate-list-button-client",
);
const generateListButtonSupplier = document.getElementById(
"generate-list-button-supplier",
);
const clientSelect = document.getElementById("client-select");
const supplierSelect = document.getElementById("supplier-select");
const statusDiv = document.getElementById("status");
generateAuditButton.addEventListener("click", () => {
chrome.tabs.create(
{
url: "https://www.obraprimaweb.com.br/App/Compras/ComprasResult.aspx",
},
(tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["content.js"],
});
},
);
});
fileInputClient.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target.result;
const clients = content
.split("\n")
.filter((line) => line.trim() !== "");
chrome.storage.local.set({ clients }, () => {
updateStatus("Lista de clientes carregada");
populateClientSelect(clients);
});
};
reader.readAsText(file);
}
});
fileInputSupplier.addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const content = e.target.result;
const suppliers = content
.split("\n")
.filter((line) => line.trim() !== "");
chrome.storage.local.set({ suppliers }, () => {
updateStatus("Lista de fornecedores carregada");
populateSupplierSelect(suppliers);
});
};
reader.readAsText(file);
}
});
generateListButtonClient.addEventListener("click", () => {
fileInputClient.click();
});
generateListButtonSupplier.addEventListener("click", () => {
fileInputSupplier.click();
});
function populateClientSelect(clients) {
clientSelect.innerHTML = "";
clients.forEach((client) => {
const option = document.createElement("option");
option.value = client.trim();
option.textContent = client.trim();
clientSelect.appendChild(option);
});
clientSelect.classList.remove("hidden");
}
function populateSupplierSelect(suppliers) {
supplierSelect.innerHTML = "";
suppliers.forEach((supplier) => {
const option = document.createElement("option");
option.value = supplier.trim();
option.textContent = supplier.trim();
supplierSelect.appendChild(option);
});
supplierSelect.classList.remove("hidden");
}
clientSelect.addEventListener("change", (event) => {
const selectedClient = event.target.value;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "fillClientName",
clientName: selectedClient,
});
});
});
supplierSelect.addEventListener("change", (event) => {
const selectedSupplier = event.target.value;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
action: "fillSupplierName",
supplierName: selectedSupplier,
});
});
});
chrome.storage.local.get("clients", ({ clients }) => {
if (clients) {
populateClientSelect(clients);
updateStatus("Lista de clientes carregada");
}
});
chrome.storage.local.get("suppliers", ({ suppliers }) => {
if (suppliers) {
populateSupplierSelect(suppliers);
updateStatus("Lista de fornecedores carregada");
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "updateStatus") {
updateStatus(request.message, request.isError);
}
});
});
function updateStatus(message, isError = false) {
const statusDiv = document.getElementById("status");
statusDiv.textContent = message;
statusDiv.style.color = isError ? "red" : "black";
}