blob: b369dd28b92d86171600cffe190aa71be8d92a72 (
plain) (
blame)
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
|
const filterHttp = (items) => {
const httpsHosts = items
.filter(item => item[1].protocol === 'https:')
.map(item => item[1].host);
const httpsHostSet = new Set(httpsHosts);
return items.filter(
item => !(item[1].protocol === 'http:' && httpsHostSet.has(item[1].host))
);
};
const filterEmptyTitle = (items) => {
return items.filter(item => item[0].title && item[0].title !== '');
}
const reduceByPathname = (items, min) => {
let hash = {};
for (let item of items) {
let pathname = item[1].origin + item[1].pathname;
if (!hash[pathname]) {
hash[pathname] = item;
} else if (hash[pathname][1].visitCount < item[1].visitCount) {
hash[pathname] = item;
}
}
let filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};
const reduceByOrigin= (items, min) => {
let hash = {};
for (let item of items) {
let origin = item[1].origin;
if (!hash[origin]) {
hash[origin] = item;
} else if (hash[origin][1].visitCount < item[1].visitCount) {
hash[origin] = item;
}
}
let filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};
const getCompletions = (keyword) => {
return browser.history.search({
text: keyword,
startTime: '1970-01-01'
}).then((items) => {
items = items.map(item => [item, new URL(item.url)]);
items = filterEmptyTitle(items);
items = filterHttp(items);
items = reduceByPathname(items, 10);
items = reduceByOrigin(items, 10);
return items
.sort((x, y) => x[0].visitCount < y[0].visitCount)
.slice(0, 10)
.map(item => item[0]);
});
};
export { getCompletions };
|