diff --git a/navigation-stats/background.js b/navigation-stats/background.js index 0dee190..95ffa8d 100644 --- a/navigation-stats/background.js +++ b/navigation-stats/background.js @@ -1,17 +1,30 @@ // Load existent stats with the storage API. -var gettingStoredStats = browser.storage.local.get("hostNavigationStats"); +var gettingStoredStats = browser.storage.local.get(); + gettingStoredStats.then(results => { // Initialize the saved stats if not yet initialized. - if (!results.hostNavigationStats) { + if (!results.stats) { results = { - hostNavigationStats: {} + host: {}, + type: {} }; } - const {hostNavigationStats} = results; - // Monitor completed navigation events and update // stats accordingly. + browser.webNavigation.onCommitted.addListener((evt) => { + if (evt.frameId !== 0) { + return; + } + + let transitionType = evt.transitionType; + results.type[transitionType] = results.type[transitionType] || 0; + results.type[transitionType]++; + + // Persist the updated stats. + browser.storage.local.set(results); + }); + browser.webNavigation.onCompleted.addListener(evt => { // Filter out any sub-frame related navigation event if (evt.frameId !== 0) { @@ -20,8 +33,8 @@ gettingStoredStats.then(results => { const url = new URL(evt.url); - hostNavigationStats[url.hostname] = hostNavigationStats[url.hostname] || 0; - hostNavigationStats[url.hostname]++; + results.host[url.hostname] = results.host[url.hostname] || 0; + results.host[url.hostname]++; // Persist the updated stats. browser.storage.local.set(results); diff --git a/navigation-stats/popup.html b/navigation-stats/popup.html index 2788d4c..1102845 100644 --- a/navigation-stats/popup.html +++ b/navigation-stats/popup.html @@ -2,7 +2,11 @@

Top navigated hosts:

-