mirror of
https://github.com/glanceapp/glance.git
synced 2025-02-16 10:29:41 +01:00
Replace previous url check with regex expression
This commit is contained in:
parent
a28d847678
commit
ceb6e3a9c9
@ -88,14 +88,10 @@ function updateRelativeTimeForElements(elements)
|
||||
}
|
||||
}
|
||||
|
||||
// This regex pattern matches example.com and www.example.com cases
|
||||
function isValidUrl(string){
|
||||
let url;
|
||||
try {
|
||||
url = new URL(string);
|
||||
} catch ({ name, message }) {
|
||||
return false;
|
||||
}
|
||||
return url.protocol === "http:" || url.protocol === "https:";
|
||||
const pattern = /^(?:[a-z0-9-]+\.)+[a-z]+$/
|
||||
return pattern.test(string)
|
||||
}
|
||||
|
||||
function setupSearchBoxes() {
|
||||
@ -128,8 +124,11 @@ function setupSearchBoxes() {
|
||||
}
|
||||
|
||||
if (event.key == "Enter") {
|
||||
const input = inputElement.value.trim();
|
||||
let input = inputElement.value.trim();
|
||||
if (isValidUrl(input)) {
|
||||
if (!input.match(/^https?:\/\//i)) {
|
||||
input = 'http://' + input;
|
||||
}
|
||||
window.open(input, '_blank').focus();
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user