@@ -67,7 +67,6 @@ function runCodeFromTextarea() {
6767}
6868
6969function onReady ( ) {
70- // snippets.addEventListener('change', updateSnippet);
7170 document
7271 . getElementById ( 'run-btn' )
7372 . addEventListener ( 'click' , runCodeFromTextarea ) ;
@@ -78,33 +77,30 @@ function onReady() {
7877 document . head . appendChild ( readyElement ) ;
7978}
8079
81- // when clicking the import code button
82- // show a UI with a url input + fetch button
83- // only accepts api.github.com urls (for now)
84- // add another function to parse a regular url
80+ // import button
81+ // show a url input + fetch button
82+ // takes a url where there is raw code
8583fetchbtnElement . addEventListener ( "click" , function ( ) {
86- // https://developer.github.com/v3/repos/contents/#get-repository-content
87- // Format:
88- // https://api.github.com/repos/username/reponame/contents/filename.py
8984 let url = document
9085 . getElementById ( 'snippet-url' )
9186 . value ;
9287 // minimal js fetch code
9388 // needs better error handling
9489 fetch ( url )
95- . then ( res => res . json ( ) )
96- . then ( data => {
97- // The Python code is in data.content
98- // it is encoded with Base64. Use atob to decode it.
99- //https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob
100- var decodedData = atob ( data . content ) ;
90+ . then ( response => {
91+ if ( ! response . ok ) { throw response }
92+ return response . text ( )
93+ } )
94+ . then ( text => {
10195 // set the value of the code editor
102- editor . setValue ( decodedData ) ;
96+ editor . setValue ( text ) ;
97+ // hide the ui
10398 urlConainerElement . classList . add ( "d-none" ) ;
10499 } ) . catch ( err => {
100+ // show the error as is for troubleshooting.
105101 document
106- . getElementById ( "errors " )
107- . innerHTML = "Couldn't fetch code. Make sure the link is public."
102+ . getElementById ( "error " )
103+ . innerHTML = err
108104 } ) ;
109105
110106} ) ;
0 commit comments