Auto-normalize urls
This commit is contained in:
parent
434db9cd0f
commit
1cdea46b28
1 changed files with 23 additions and 1 deletions
24
libbrowz.js
24
libbrowz.js
|
@ -1,8 +1,30 @@
|
||||||
let tabGroup = document.querySelector("tab-group");
|
let tabGroup = document.querySelector("tab-group");
|
||||||
|
function normalizeUrl(url) {
|
||||||
|
// Define regex patterns for matching URL schemes and local addresses
|
||||||
|
const httpPattern = /^http:\/\//i;
|
||||||
|
const httpsPattern = /^https:\/\//i;
|
||||||
|
const filePattern = /^file:\/\//i;
|
||||||
|
const indexPattern = /^index\.html/i;
|
||||||
|
const localPattern = /^(192\.168|127\.0|localhost)/i;
|
||||||
|
|
||||||
|
// Check if the URL already has a valid scheme
|
||||||
|
if (httpPattern.test(url) || httpsPattern.test(url) || filePattern.test(url) || indexPattern.test(url)) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine if the URL starts with a local address or needs HTTPS
|
||||||
|
if (localPattern.test(url)) {
|
||||||
|
return `http://${url}`;
|
||||||
|
} else {
|
||||||
|
return `https://${url}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function go() {
|
function go() {
|
||||||
let browserFrame = tabGroup.getActiveTab().webview
|
let browserFrame = tabGroup.getActiveTab().webview
|
||||||
let browser = tabGroup.getActiveTab()
|
let browser = tabGroup.getActiveTab()
|
||||||
let url = document.getElementById("txtUrl").value
|
let url = normalizeUrl(document.getElementById("txtUrl").value)
|
||||||
if (url.includes("youtube.com") || url.includes("youtu.be")) {
|
if (url.includes("youtube.com") || url.includes("youtu.be")) {
|
||||||
url = url.replaceAll("youtube.com", "yewtu.be")
|
url = url.replaceAll("youtube.com", "yewtu.be")
|
||||||
url = url.replaceAll("youtu.be", "yewtu.be")
|
url = url.replaceAll("youtu.be", "yewtu.be")
|
||||||
|
|
Loading…
Reference in a new issue