-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomain_matching.js
60 lines (60 loc) · 1.83 KB
/
domain_matching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* globals document,window,alert */
class DomainRegex {
constructor(selector) {
this.form = document.querySelector(selector.formSelector);
this.textboxUrl = this.form.querySelector(selector.textboxUrlSelector);
this.submit = this.form.querySelector(selector.submitSelector);
}
init() {
this.bindEvent();
}
bindEvent() {
this.form.addEventListener("submit", (event) => {
if (this.isUrlValid()) {
this.getDomain(this.validatedUrl);
}
else {
alert("Incorrect URL !!!");
event.preventDefault();
}
});
}
isUrlValid() {
this.validatedUrl = URLDECRYPTOR.exec(this.textboxUrl.value);
return this.validatedUrl;
}
getDomain(extractedUrl) {
if (extractedUrl[1]) {
if (extractedUrl[2]) {
this.showDomainSubDomain(extractedUrl[2] + "." + extractedUrl[3], extractedUrl[1]);
}
else {
this.showDomainSubDomain(extractedUrl[3], extractedUrl[1]);
}
}
else if (extractedUrl[2]) {
this.showDomainSubDomain(extractedUrl[3], extractedUrl[2]);
}
else {
this.showDomainSubDomain(extractedUrl[3]);
}
}
showDomainSubDomain(domain, subdomain) {
if (subdomain) {
alert("Domain : " + domain + "\nSubDomain : " + subdomain);
}
else {
alert("Domain : " + domain);
}
}
}
const URLDECRYPTOR = new RegExp(/^(?:(?:http|https|ftp):\/\/)?(www)?\.?(?:((?:[\w\d\_\-]+)*[\w\d\-\_]+)\.)?([\w\d\_\-]+\.[\w\d\.]{2,5})(?:\/[\w\d\-\_\?\=\&\#\.%]+)*$/);
window.onload = () => {
var selector = {
formSelector: "[data-value=form]",
textboxUrlSelector: "[data-value=input-url]",
submitSelector: "[data-value=submit-btn]"
};
var obj = new DomainRegex(selector);
obj.init();
};