-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
66 lines (58 loc) · 1.72 KB
/
popup.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
cb = chrome.bookmarks;
storage = chrome.storage.sync;
noti = chrome.notifications;
async function analyseBookmarks(){
analysisData = {};
storage.clear();
let bms = await cb.getTree();
bms.forEach(async function(bm) {
await analyseRecursively(bm);
});
for (var key in analysisData){
let data = analysisData[key];
data.sort((a,b) => (Object.values(a)[0] < Object.values(b)[0]) ? 1
: (Object.values(b)[0] < Object.values(a)[0]) ? -1 : 0);
}
storage.set(analysisData);
}
async function analyseRecursively(bm){
if(bm.children){
bm.children.forEach(async function(c) {
await analyseRecursively(c);
});
}
else{
let domain = getDomainName(bm.url);
let parentFolderId = bm.parentId;
let data = analysisData[domain];
if (!data) data = [];
let newIndex = data.findIndex(el => el[parentFolderId] != undefined);
if(newIndex == -1) {
data.push({[parentFolderId]:0});
newIndex = data.length-1;
}
data[newIndex][ Object.keys(data[newIndex])[0] ] += 1;
analysisData[domain] = data;
}
}
function getDomainName(v) {
if(!v.startsWith("http")) return v;
parser = document.createElement('a');
parser.href = v;
v = parser.host;
var is_co = v.match(/\.co\./);
v = v.split('.');
v = v.slice(is_co ? -3 : -2);
v = v.join('.');
return v;
};
document.addEventListener('DOMContentLoaded', function() {
const button = document.getElementById('analyseButton');
// onClick's logic below:
button.addEventListener('click', async function() {
await analyseBookmarks();
let result = document.createElement("p");
result.innerHTML = "Success"
document.querySelector("body").appendChild(result);
});
});