Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export CSV #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"manifest_version": 3,
"name": "JSON-As-Table Viewer",
"version": "1.0.5",
"version": "1.0.6",
"description": "*** View JSON response from a URL as HTML table ***",
"icons": {
"16": "images/icon16.png",
Expand All @@ -26,7 +26,12 @@
}
],
"web_accessible_resources": [
"libs/DataTables-1.10.21/images/*",
"images/*"
{
"resources": [
"libs/DataTables-1.10.21/images/*",
"images/*"
],
"matches": [ "<all_urls>" ]
}
]
}
}
27 changes: 24 additions & 3 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ function renderTable() {
responsive: true,
data: dataArray,
columns: dataColumns,
order: [[1, 'asc']]
order: [[1, 'asc']],
"lengthMenu": [[-1, 20, 30, 50, 100], ["All", 20, 30, 50, 100]]
});

//Expandable panel below each row to show original JSON of the row
Expand All @@ -97,6 +98,26 @@ function renderTable() {
tr.addClass('shown');
}
});

$('#exportCsv').on('click', function () {
const arrayOfJson = contentArray
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(arrayOfJson[0])
let csv = arrayOfJson.map(row => header.map(fieldName =>
JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')
const filename = "json.csv"

// Create link and download
var link = document.createElement('a');
link.setAttribute('href', 'data:text/csv;charset=utf-8,%EF%BB%BF' + encodeURIComponent(csv));
link.setAttribute('download', filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
});
}

Expand All @@ -115,10 +136,10 @@ function tableHTML() {
"</head>" +
"<body>" +
"<div id='content-div' class='acs-content-div'>" +
"<div class='acs-logo-bg'><div class='acs-logo'></div></div>" +
"<div id='table-div' class='acs-table-div'>" +
"<table id=\"table_id\" class=\"display\">" +
"</table>" +
"<button id=\"exportCsv\" type=\"button\">Export CSV</button>\n" +
"</div>" +
"<br/>" +
"<button type=\"button\" class=\"collapsible\">Click to see original JSON text</button>\n" +
Expand Down Expand Up @@ -216,7 +237,7 @@ function errorMessage() {
*/
function isJsonOnlyPage() {
return document.body !== undefined
&& document.body.getElementsByTagName("*").length === 1
&& document.body.getElementsByTagName("*").length === 2
&& document.getElementsByTagName("pre").length === 1;
}

Expand Down