-
Notifications
You must be signed in to change notification settings - Fork 6
/
topsites.js
73 lines (63 loc) · 1.72 KB
/
topsites.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
function extractDomain(url, y) {
var domain;
//find & remove protocol (http, ftp, etc.) and get domain
if (url.indexOf("://") > -1) {
domain = url.split('/')[2];
}
else {
domain = url.split('/')[0];
}
//find & remove port number
domain = domain.split(':')[0];
//find & remove tld
//if (y == 1) {
// domain = domain.split('.')[0];
//}
if (y == 1) {
d = splitHostname(domain);
if (d.subdomain != "www" && d.subdomain != "") {
return d.subdomain;
} else {
return d.domain;
}
} else {
return domain;
}
} //from lewdev on stackoverflow
function addhttp(url) {
if (!/^(f|ht)tps?:\/\//i.test(url)) {
url = "http://" + url;
}
return url;
}
function splitHostname(h) {
var result = {};
var regexParse = new RegExp('([a-z\-0-9]{2,63})\.([a-z\.]{2,5})$');
var urlParts = regexParse.exec(h);
result.domain = urlParts[1];
result.type = urlParts[2];
result.subdomain = h.replace(result.domain + '.' + result.type, '').slice(0, -1);;
return result;
}
document.addEventListener("DOMContentLoaded", function() {
chrome.storage.sync.get({
"showTop": true
}, function(items) {
if (items["showTop"] == true) {
listTopSites();
}
});
});
function listTopSites() { //get the top 5 sites from chrome
var ul = document.getElementById("topsites");
document.getElementById("topsites-list").style = 'display: block !important';
chrome.topSites.get(function(info){
for(var i=0;i<5;i++) {
var li = document.createElement("li");
var siteurl = info[i].url;
li.insertAdjacentHTML("beforeend", "<a href="+siteurl+">"+extractDomain(siteurl,1)+"</a>");
//li.appendChild("extractDomain(info[i].url)");
ul.appendChild(li);
}
});
}