@@ -73,30 +73,43 @@ async function populateCodeJson(data) {
7373 return codeJson ;
7474}
7575
76+ // Creates code.json object
77+ async function createCodeJson ( data ) {
78+ delete data . submit ;
79+ const codeJson = await populateCodeJson ( data ) ;
80+
81+ const jsonString = JSON . stringify ( codeJson , null , 2 ) ;
82+ // const blob = new Blob([jsonString], { type: "application/json" });
83+ document . getElementById ( "json-result" ) . value = jsonString ;
84+ }
85+
86+ // Copies code.json to clipboard
7687async function copyToClipboard ( event ) {
7788 event . preventDefault ( ) ;
89+
7890 var textArea = document . getElementById ( "json-result" ) ;
7991 textArea . select ( ) ;
8092 document . execCommand ( "copy" )
8193}
8294
83- // Creates code.json and triggers file download
84- async function downloadFile ( data ) {
85- delete data . submit ;
86- const codeJson = await populateCodeJson ( data ) ;
95+ // Triggers local file download
96+ async function downloadFile ( event ) {
97+ event . preventDefault ( ) ;
8798
88- const jsonString = JSON . stringify ( codeJson , null , 2 ) ;
89- // const blob = new Blob([jsonString], { type: "application/json" });
90- document . getElementById ( "json-result" ) . value = jsonString ;
99+ const codeJson = document . getElementById ( "json-result" ) . value
100+ const jsonObject = JSON . parse ( codeJson ) ;
101+ const jsonString = JSON . stringify ( jsonObject , null , 2 ) ;
102+ const blob = new Blob ( [ jsonString ] , { type : "application/json" } ) ;
91103
92- // // Create anchor element and create download link
93- // const link = document.createElement("a");
94- // link.href = URL.createObjectURL(blob);
95- // link.download = "code.json";
104+ // Create anchor element and create download link
105+ const link = document . createElement ( "a" ) ;
106+ link . href = URL . createObjectURL ( blob ) ;
107+ link . download = "code.json" ;
96108
97- // // Trigger the download
98- // link.click();
109+ // Trigger the download
110+ link . click ( ) ;
99111}
100112
101- window . downloadFile = downloadFile ;
113+ window . createCodeJson = createCodeJson ;
102114window . copyToClipboard = copyToClipboard ;
115+ window . downloadFile = downloadFile ;
0 commit comments