-
Notifications
You must be signed in to change notification settings - Fork 0
/
duneSaverPanel.js
64 lines (57 loc) · 2.21 KB
/
duneSaverPanel.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
const summary = document.getElementById("dataSummary")
const contentWrapper = document.getElementsByClassName("contentWrapper")[0]
const content = document.getElementById("content")
const saveButton = document.getElementById("saveButton")
const viewButton = document.getElementById("viewButton")
const tips = document.getElementById("successTips")
function formatDataForCsv(data) {
const dataArr = [data.columns?.join(",")];
data?.data?.forEach(d => {
const item = data?.columns?.map(key => d[key])
dataArr.push(item.join(','))
})
return dataArr.join('\n');
}
function getDownloadUrl(text) {
const BOM = '\uFEFF';
if (window.Blob && window.URL && window.URL.createObjectURL) {
const csvData = new Blob([BOM + text], {type: "text/csv"});
return URL.createObjectURL(csvData)
} else {
return "data:attachment/csv;charset=utf-8," + BOM + text
}
}
saveButton.addEventListener("click", () => {
chrome.storage.local.get("queryData").then((data) => {
chrome.downloads.download({
url: getDownloadUrl(formatDataForCsv(data.queryData)),
filename: "dune_query_" + (data.queryData?.execution_id || "data") + ".csv",
conflictAction: "overwrite"
}, (downloadId) => {
if (downloadId && tips) {
tips.style.display = "inline-block"
setTimeout(() => tips.style.display = "none", 3000)
}
})
})
})
viewButton.addEventListener('click', () => {
contentWrapper.classList.toggle("showAll")
})
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'local' && changes.queryData?.newValue) {
const queryData = changes.queryData.newValue
const id = queryData.execution_id
const at = queryData.generated_at
summary.innerHTML = `
<div>Execution ID: ${id}</div>
<div>Generated At: ${at ? new Date(at).toLocaleString() : '-'}</div>
`
content.innerText = JSON.stringify(queryData)
if (document.getElementById("content").getBoundingClientRect().height > 78) {
viewButton.style.display = "block"
} else {
viewButton.style.display = "none"
}
}
})