aboutsummaryrefslogtreecommitdiff
path: root/src/background/histories.js
blob: a7d3d4714ed581da546c0503c42af114ff1d3588 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 filterClosedPath = (items) => {
  const allSimplePaths = items
    .filter(item => item[1].hash === '' && item[1].search === '')
    .map(item => item[1].origin + item[1].pathname);
  const allSimplePathSet = new Set(allSimplePaths);
  return items.filter(
    item => !(item[1].hash === '' && item[1].search === '' &&
      (/\/$/).test(item[1].pathname) &&
      allSimplePathSet.has(
        (item[1].origin + item[1].pathname).replace(/\/$/, '')
      )
    )
  );
};

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].href.length > item[1].href.length) {
      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].href.length > item[1].href.length) {
      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: 0,
  }).then((historyItems) => {
    return [historyItems.map(item => [item, new URL(item.url)])]
      .map(filterEmptyTitle)
      .map(filterHttp)
      .map(filterClosedPath)
      .map(items => reduceByPathname(items, 10))
      .map(items => reduceByOrigin(items, 10))
      .map(items => items
        .sort((x, y) => x[0].visitCount < y[0].visitCount)
        .slice(0, 10)
        .map(item => item[0])
      )[0];
  });
};

export { getCompletions };