-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyscript.js
More file actions
45 lines (36 loc) · 1.65 KB
/
Copy pathmyscript.js
File metadata and controls
45 lines (36 loc) · 1.65 KB
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
// When the tab has been changed, myscript is called to log the webpage's time, either by simply
// resetting the start time of a previously visited webpage or appending a new webpage object if
// it is unknown
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
// check request message
if( request.message === "tab_changed" ) {
var firstHref = window.location.hostname;
var fav = firstHref;
// special use cases for google domains so we can grab a favicon
if(firstHref.includes("www.google")){
fav="www.google.com";
}
// Strip off preceding www. and ending .com's from URL's to make webname prettier
firstHref = firstHref.replace('www.', '').replace('.com', '');
firstHref = firstHref.charAt(0).toUpperCase() + firstHref.slice(1);
// Loop through to check if the website we're adding is already in the list, if so
// just edit the time and dont append the webname to overall time object
var index = firstHref;
if(request.times[firstHref] != undefined){
request.times[firstHref].start_time = new Date().getTime();
}
// check to make sure the object hasn't been seen before
if(!(firstHref in request.times)){
var website = {
webname: firstHref,
favicon: ("\"http://www.google.com/s2/favicons?domain=" + fav + "\""),
start_time: new Date().getTime(),
total_time: 0,
prev_total_time: 0 };
request.times[firstHref] = website;
}
chrome.runtime.sendMessage({"message": "track_tab", "url": firstHref, "times":request.times, "index": index});
}
}
);