Skip to content

Commit f9bbe02

Browse files
author
jdev082
committed
fix(search): properly handle URLs without a protocol being specified
Signed-off-by: jdev082 <jdev082@jdev.eu.org>
1 parent 3835029 commit f9bbe02

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/search.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ searchbar.addEventListener('input', async() => {
5151

5252
function loadURL(url, scheck='true') {
5353
view = document.querySelector('.current');
54-
if (isSearch(url)) {
55-
document.querySelector(
56-
'.current'
57-
).src = `${engineurls[preferences.searchengine]}${encodeURIComponent(url)}`;
58-
} else {
54+
if (checkUrlValidity(url)) {
5955
if ( url.startsWith('http://') ) {
6056
alert(`Page ${url} is not secure.`);
6157
}
@@ -77,7 +73,11 @@ function loadURL(url, scheck='true') {
7773
}
7874
return;
7975
}
80-
view.src = url;
76+
view.src = "https://" + url;
77+
} else {
78+
document.querySelector(
79+
'.current'
80+
).src = `${engineurls[preferences.searchengine]}${encodeURIComponent(url)}`;
8181
}
8282
removeChildren(suggestionsEl);
8383
view.addEventListener('did-finish-load', () => {
@@ -120,8 +120,19 @@ function isSearch(input) {
120120
return true;
121121
}
122122

123+
function checkUrlValidity(url) {
124+
var urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate protocol
125+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // validate domain name
126+
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // validate OR ip (v4) address
127+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // validate port and path
128+
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // validate query string
129+
'(\\#[-a-z\\d_]*)?$','i'); // validate fragment locator
130+
return !!urlPattern.test(url);
131+
}
132+
123133
// add listeners
124134
searchbar.addEventListener('keydown', (e) => {
125135
var url = document.getElementById('searchbar').value;
126136
if (e.code === 'Enter') loadURL(url);
137+
console.log(checkUrlValidity(url))
127138
});

0 commit comments

Comments
 (0)