-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost-searcher.js
154 lines (127 loc) · 3.92 KB
/
host-searcher.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class HostSearcher {
constructor() {
this.minSimilarity = 0.5;
this.domainMinLength = 5;
this.srcAndHref = document.querySelectorAll('[src],[href]');
this.uniqueHosts = this.extractValues();
this.uniqueHostsWithComa = this.uniqueHosts.join(',');
}
/**
* @returns {*[]}
*/
getUniqueHosts() {
return this.uniqueHosts;
}
/**
* @returns {string}
*/
getUniqueHostsWithComa() {
return this.uniqueHostsWithComa;
}
/**
* @returns {number}
*/
getMinSimilarity() {
return this.minSimilarity;
}
/**
* Domains extraction.
* @returns {[]}
*/
extractValues() {
var currentDomain = location.hostname;
var __this = this;
var uniqueHosts = [];
for (var i = 0; i < __this.srcAndHref.length; ++i) {
var item = __this.srcAndHref[i];
var value;
var hostName;
if (item.getAttribute('src') !== null) {
value = item.getAttribute('src');
}
if (item.getAttribute('href') !== null) {
value = item.getAttribute('href');
}
if (value.length > __this.domainMinLength &&
value.indexOf('.') > -1) {
var parser = document.createElement('a');
parser.href = value;
if (parser.hostname.length > __this.domainMinLength) {
hostName = parser.hostname;
if (uniqueHosts.indexOf(hostName) === -1 &&
hostName.indexOf('.') > -1) {
var similarity = __this.similarity(currentDomain, hostName);
if (similarity < __this.minSimilarity) {
/**
* Collecting hosts.
*/
uniqueHosts.push(hostName);
}
}
}
}
}
return uniqueHosts;
}
/**
* Similarity of strings.
* @stackoverflow: https://stackoverflow.com/questions/10473745/compare-strings-javascript-return-of-likely
* @param s1
* @param s2
* @returns {number}
*/
similarity(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength === 0) {
return 1.0;
}
return (longerLength - this.editDistance(longer, shorter)) / parseFloat(longerLength);
}
/**
* Stackoverflow
* @param s1
* @param s2
* @returns {*}
*/
editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = [];
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i === 0) {
costs[j] = j;
} else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) !== s2.charAt(j - 1)) {
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
}
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) {
costs[s2.length] = lastValue;
}
}
return costs[s2.length];
}
}
var hostSearcher = new HostSearcher();
console.log('');
console.log('[[[ HOST SEARCHER ALL:: START ]]]');
console.log('');
console.log('Total: ', hostSearcher.getUniqueHosts().length);
console.log('');
console.log(hostSearcher.getUniqueHostsWithComa());
console.log('');
console.log('[[[ HOST SEARCHER ALL :: END ]]]');