Skip to content

Commit c7bb455

Browse files
committed
get domain from url & add unique domains in list
1 parent 381fd6b commit c7bb455

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

scripts/background.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,47 @@ WebTimer.prototype = {
1515

1616
addTab: function (tab) {
1717
if (this.isValidPage(tab) === true) {
18-
if (tab.id && (tab.id != 0)) {
18+
if (tab.id && (tab.id != 0)) {
1919
this.tabs = this.tabs || [];
20-
this.tabs.push(tab.url);
21-
this.activeTab = tab.url;
20+
var domain = this.extractRootDomain(tab.url);
21+
if (!this.tabs.includes(domain)){
22+
this.tabs.push(domain);
23+
}
24+
this.activeTab = domain;
2225
}
2326
}
2427
},
2528

29+
extractRootDomain: function (url) {
30+
var domain = this.extractHostname(url),
31+
splitArr = domain.split('.'),
32+
arrLen = splitArr.length;
33+
34+
if (arrLen > 2) {
35+
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
36+
if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) {
37+
domain = splitArr[arrLen - 3] + '.' + domain;
38+
}
39+
}
40+
return domain;
41+
},
42+
43+
extractHostname: function(url) {
44+
var hostname;
45+
46+
if (url.indexOf("//") > -1) {
47+
hostname = url.split('/')[2];
48+
}
49+
else {
50+
hostname = url.split('/')[0];
51+
}
52+
53+
hostname = hostname.split(':')[0];
54+
hostname = hostname.split('?')[0];
55+
56+
return hostname;
57+
},
58+
2659
isValidPage: function (tab) {
2760
if (!tab || !tab.url || (tab.url.indexOf('http:') == -1 && tab.url.indexOf('https:') == -1)
2861
|| tab.url.indexOf('chrome://') !== -1

0 commit comments

Comments
 (0)