Your initial approach is flawed to begin with. What do you mean by "time spent on a site"? The page loads, packets cease to flow, yet the user is still reading the page. Just network analysis is hardly a good metric.
And what about external resources? Do your users "spend time" on Google Analytics website when trackers make their requests?
If you are dead-set on network capture, chrome.webRequest
or chrome.webNavigation
APIs should let you tap into requests made by the client.
As for implementing this properly as a Chrome extension, it should be trivial. You can use chrome.tabs
API to keep track of open/active tabs.
- Declare
"tabs"
permission to get access to URL data
- On initialization, query current tabs (or just the active tab)
- Listen to
onCreated
for new tabs, or better yet for onActivated
for change of visible tab (possibly do so with windows too, there's a separate API for that)
- Within the active tab, track URL changes with
onUpdated
If you're new to extension development, start with the Overview. Good read, and a good hub to other documentation.
As for recommending JavaScript resources, I'll pass; too many of them.