@@ -67,7 +67,6 @@ function runCodeFromTextarea() {
67
67
}
68
68
69
69
function onReady ( ) {
70
- // snippets.addEventListener('change', updateSnippet);
71
70
document
72
71
. getElementById ( 'run-btn' )
73
72
. addEventListener ( 'click' , runCodeFromTextarea ) ;
@@ -78,33 +77,30 @@ function onReady() {
78
77
document . head . appendChild ( readyElement ) ;
79
78
}
80
79
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
85
83
fetchbtnElement . 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
89
84
let url = document
90
85
. getElementById ( 'snippet-url' )
91
86
. value ;
92
87
// minimal js fetch code
93
88
// needs better error handling
94
89
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 => {
101
95
// set the value of the code editor
102
- editor . setValue ( decodedData ) ;
96
+ editor . setValue ( text ) ;
97
+ // hide the ui
103
98
urlConainerElement . classList . add ( "d-none" ) ;
104
99
} ) . catch ( err => {
100
+ // show the error as is for troubleshooting.
105
101
document
106
- . getElementById ( "errors " )
107
- . innerHTML = "Couldn't fetch code. Make sure the link is public."
102
+ . getElementById ( "error " )
103
+ . innerHTML = err
108
104
} ) ;
109
105
110
106
} ) ;
0 commit comments