@@ -79,8 +79,23 @@ async function closeChannel() {
7979 }
8080}
8181
82+ function checkHttpUrl ( url ) {
83+ let givenURL ;
84+ try {
85+ givenURL = new URL ( url ) ;
86+ } catch ( error ) {
87+ console . log ( "error is" , error )
88+ return false ;
89+ }
90+ return givenURL . protocol === "http:" || givenURL . protocol === "https:" ;
91+ }
92+
8293async function submiturl ( ) {
8394 let url = document . getElementById ( "stumble-url" ) . value ;
95+ if ( ! checkHttpUrl ( url ) ) {
96+ alert ( "invalid url" )
97+ return
98+ }
8499 console . log ( "submit url : " , url ) ;
85100 if ( typeof window . ethereum != "undefined" ) {
86101 // provider / connection to the blockchain
@@ -98,7 +113,10 @@ async function submiturl() {
98113 await listenForTransactionMined ( transactionResponse , provider ) ;
99114 console . log ( "Done!!" ) ;
100115 } catch ( error ) {
101- console . log ( error ) ;
116+ error = JSON . stringify ( error )
117+ error = JSON . parse ( error ) ;
118+ console . log ( error . error ) ;
119+ alert ( error . error . message ) ;
102120 }
103121 }
104122}
@@ -236,6 +254,20 @@ function getRndInteger(min, max) {
236254 return Math . floor ( Math . random ( ) * ( max - min ) ) + min ;
237255}
238256
257+
258+ function fixUrl ( url ) {
259+ // Regular expression to check URL format with "http://" or "https://"
260+ const urlPattern = / ^ ( h t t p s ? : \/ \/ ) [ ^ \s / $ . ? # ] .[ ^ \s ] * $ / i;
261+
262+ if ( ! urlPattern . test ( url ) ) {
263+ // Invalid URL format, add "https://" to the beginning of the URL
264+ url = `https://${ url } ` ;
265+ }
266+
267+ return url ;
268+ }
269+
270+
239271async function stumble ( ) {
240272 let url = "" ;
241273 const provider = new ethers . providers . JsonRpcProvider ( "https://rpc.sepolia.org" ) ;
@@ -251,6 +283,8 @@ async function stumble() {
251283
252284 url = await contract . urlArray_element ( index ) ;
253285 console . log ( url ) ;
286+ url = fixUrl ( url ) ;
287+ console . log ( "fixed url : " , url ) ;
254288
255289 // modify iframe url
256290 var stumble_iframe = document . getElementById ( "stumble-iframe" ) ;
0 commit comments