Skip to content

Commit ec6d351

Browse files
committed
url validation added. logs and alert error when same url is submitted again, if wrong url was submitted before, it tries to make it valid url using fixurl function
1 parent 8c4eafa commit ec6d351

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

frontend/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
</div>
1818
<div class="navbar-buttons">
1919
<form class="navbar-form">
20-
<input class="url-input" id="stumble-url" type="text" placeholder="submit URL for others">
21-
<button type="button" navbar-button" id ="submitUrlButton"style="border-radius: 10px; font-size: large;">&#8680;</button>
22-
<!-- <button class="submit-button" type="submit">Submit</button> -->
20+
<input type="url" class="url-input" id="stumble-url" placeholder="submit URL for others" pattern="https?://.+" required>
21+
<!-- <button type="button" class ="navbar-button" id ="submitUrlButton"style="border-radius: 10px; font-size: large;">&#8680;</button> -->
22+
<input type="submit" class="navbar-button" value="&#8680;" id="submitUrlButton" style="border-radius: 10px; font-size: large;">
2323
</form>
2424
<button id="upvoteButton" class="navbar-button" style="border-radius: 10px; font-size: large;">&#x1F44D;</button>
2525
<button id="stumbleButton" class="navbar-button" style="border-radius: 10px; font-size: large;">Stumble</button>
@@ -36,7 +36,7 @@
3636

3737
<!-- Iframe -->
3838
<div class="iframe-container">
39-
<iframe class="iframe" id="stumble-iframe" src="https://channel4.wtf/" frameborder="0"></iframe>
39+
<iframe sandbox ="allow-forms allow-modals allow-orientation-lock allow-pointer-lock allow-popups allow-popups-to-escape-sandbox allow-presentation allow-same-origin allow-scripts" class="iframe" id="stumble-iframe" src="https://channel4.wtf/" frameborder="0"></iframe>
4040
</div>
4141
<script src="index.js" type="module"></script>
4242
<script src="https://cdn.jsdelivr.net/npm/shepherd.js@10.0.1/dist/js/shepherd.min.js"></script>

frontend/index.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
8293
async 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 = /^(https?:\/\/)[^\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+
239271
async 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

Comments
 (0)