-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmost-visited-websites-extension.js
More file actions
55 lines (49 loc) · 1.44 KB
/
Copy pathmost-visited-websites-extension.js
File metadata and controls
55 lines (49 loc) · 1.44 KB
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
const getDomain = (url, subdomain) => {
subdomain = subdomain || false;
url = url.replace(/(https?:\/\/)?(www.)?/i, '');
if (!subdomain) {
url = url.split('.');
url = url.slice(url.length - 2).join('.');
}
if (url.indexOf('/') !== -1) {
return url.split('/')[0];
}
return url;
};
chrome.storage.sync.get('mostVisitedWebsites', value => {
// check if no value set yet
if (Object.keys(value).length === 0) {
return chrome.storage.sync.set({
mostVisitedWebsites: [
{ url: getDomain(window.location.hostname, true), numberOfVisits: 1 }
]
});
}
// check if url exists
const isOldUrl = value.mostVisitedWebsites.find(el => {
return el.url === getDomain(window.location.hostname, true);
});
if (!isOldUrl) {
// if new url add to list
value.mostVisitedWebsites.push({
url: getDomain(window.location.hostname, true),
numberOfVisits: 1
});
} else {
// if it's not a new url add numberOfVisits
let newValue = value.mostVisitedWebsites.map(value => {
if (value.url === getDomain(window.location.hostname, true)) {
value.numberOfVisits += 1;
}
return value;
});
value.mostVisitedWebsites = newValue;
}
// sort by numberOfVisits
value.mostVisitedWebsites = value.mostVisitedWebsites.sort(
(object1, object2) => {
return object2.numberOfVisits - object1.numberOfVisits;
}
);
chrome.storage.sync.set(value);
});