-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
107 lines (90 loc) · 2.48 KB
/
options.js
File metadata and controls
107 lines (90 loc) · 2.48 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
// Amazon Return Notes - options script
function csvEscape(value) {
if (value === null || value === undefined) return "";
const str = String(value).replace(/\r?\n/g, " ");
if (/[",]/.test(str)) {
return '"' + str.replace(/"/g, '""') + '"';
}
return str;
}
function setStatus(text) {
const el = document.getElementById("status");
if (!el) return;
el.textContent = text || "";
}
function exportCsv() {
setStatus("Building CSV...");
chrome.storage.sync.get(null, data => {
const all = data || {};
const rows = [];
const header = [
"Key",
"Order ID",
"Return Created",
"RMA ID",
"ASIN",
"Product Title",
"Product Price",
"Seller",
"Product Metadata",
"Notes",
"Completed",
"Last Saved ISO",
"Last Saved Display"
];
rows.push(header);
Object.entries(all).forEach(([key, entry]) => {
if (!key.startsWith("arn_")) return;
const metaPairs = Array.isArray(entry.metadata)
? entry.metadata
: [];
const metaString = metaPairs
.map(pair => `${pair.label}: ${pair.value}`)
.join(" | ");
rows.push([
key,
entry.orderId || "",
entry.returnCreated || "",
entry.rmaId || "",
entry.asin || "",
entry.title || "",
entry.price || "",
entry.seller || "",
metaString,
entry.notes || "",
entry.completed ? "1" : "0",
entry.lastSaved || "",
entry.lastSavedDisplay || ""
]);
});
if (rows.length === 1) {
setStatus("No Amazon Return Notes entries found in storage.");
return;
}
const csv = rows
.map(row => row.map(csvEscape).join(","))
.join("\r\n");
const blob = new Blob([csv], {
type: "text/csv;charset=utf-8;"
});
const url = URL.createObjectURL(blob);
const now = new Date();
const filename = `amazon-return-notes-${now.getFullYear()}-${String(
now.getMonth() + 1
).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}.csv`;
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
setStatus(`Exported ${rows.length - 1} entries to ${filename}.`);
});
}
document.addEventListener("DOMContentLoaded", () => {
document
.getElementById("exportCsv")
.addEventListener("click", exportCsv);
setStatus("");
});