Refactor the CSV export for CSP reasons #1453
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Tock's CSP policy disallows inline Javascript, so
onclick=""
attributes are forbidden. Those attributes are how CSV exports are triggered on the analytics page. The handler relies on a random pseudo ID that is generated at render time to identify the table being exported.This PR removes the
onclick
attribute from the button, and addsdata-csv
,data-csv-id
, anddata-csv-name
attributes. Thedata-csv
attribute is just a flag that the button should act as a CSV exporter.data-csv-id
anddata-csv-name
are populated with the pseudo ID and filename hint, respectively.Then, after the DOM is loaded, this PR adds code that finds all button elements with a
data-csv
attribute. It iterates over them, pulling out their IDs and names, and adding a click event listener. It uses the ID and name to finally call thedownload_table_as_csv
function after the button is cilcked.Additional information