add regex rule for checking valid url

This commit is contained in:
Sherry Ma 2020-03-09 17:02:56 -04:00 committed by Martin D Kealey
parent ea528ff3f6
commit 5f56aa8541

View file

@ -1875,6 +1875,7 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
const tabId = currentTab.id; const tabId = currentTab.id;
const fullURL = this.checkUrl(url); const fullURL = this.checkUrl(url);
if (fullURL !== "") {
// Assign URL to container // Assign URL to container
await Logic.setOrRemoveAssignment(tabId, fullURL, userContextId, false); await Logic.setOrRemoveAssignment(tabId, fullURL, userContextId, false);
@ -1884,23 +1885,26 @@ Logic.registerPanel(P_CONTAINER_EDIT, {
// Show new assignments // Show new assignments
const assignments = await Logic.getAssignmentObjectByContainer(userContextId); const assignments = await Logic.getAssignmentObjectByContainer(userContextId);
this.showAssignedContainers(assignments); this.showAssignedContainers(assignments);
}
}, },
checkUrl(url){ checkUrl(url){
// append "https://" if protocol not found // append "https://" if protocol not found
const validUrl = /[\w.-]+(?:\.[\w.-]+)/g;
const regexWww = /.*www\..*/g; const regexWww = /.*www\..*/g;
const foundWww = url.match(regexWww);
const regexhttp = /^http:\/\/.*/g; const regexhttp = /^http:\/\/.*/g;
const regexhttps = /^https:\/\/.*/g; const regexhttps = /^https:\/\/.*/g;
const foundhttp = url.match(regexhttp);
const foundhttps = url.match(regexhttps);
let newURL = url; let newURL = url;
if (!foundhttp && !foundhttps) { if (!url.match(validUrl)) {
return "";
}
if (!url.match(regexhttp) && !url.match(regexhttps)) {
newURL = "https://" + url; newURL = "https://" + url;
} }
if (!foundWww) { if (!url.match(regexWww)) {
if (newURL.match(regexhttp)) { if (newURL.match(regexhttp)) {
newURL = "http://www." + newURL.substring(7); newURL = "http://www." + newURL.substring(7);
} else if (newURL.match(regexhttps)) { } else if (newURL.match(regexhttps)) {