-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstats.coffee
53 lines (40 loc) · 1 KB
/
stats.coffee
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
random = require('./helpers').random
getUriFromTab = require('./helpers').getUriFromTab
if localStorage["stats"]
stats = JSON.parse localStorage["stats"]
stats ?= {}
stats.tabStats ?= {}
stats.allStats ?=
count: 0
stats.allStats.libs ?= {}
console.log stats
stats.addBoost = (tabId, normalizedUrl) ->
chrome.tabs.get tabId, (tab) =>
# .url requires "tabs" permission
pageUrl = getUriFromTab(tab)
# console.log tabId, normalizedUrl
if not @tabStats[pageUrl]
@tabStats[pageUrl] =
count: 0
libs: []
entry = @tabStats[pageUrl]
lib = normalizedUrl.library
# add stat for current tab
entry.count += 1
if lib not in entry.libs
entry.libs.push lib
# add global stat
stats.allStats.count += 1
if stats.allStats.libs[lib]
stats.allStats.libs[lib].count += 1
else
stats.allStats.libs[lib] =
count: 1
localStorage["stats"] = JSON.stringify @
@
stats.get = (id, cb) ->
chrome.tabs.get id, (tab) =>
cb(
stats.tabStats[getUriFromTab(tab)]
)
module.exports = stats