-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.js
170 lines (154 loc) · 7.69 KB
/
events.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var eventNum;
var snapshotEventInterval=500;
var nextSnapshotEventIndex=snapshotEventInterval;
var currentBrowserState = {};
chrome.tabs.query({},function(tabs){
chrome.storage.local.clear();
currentBrowserState.tabs = {};
currentBrowserState.windows={};
for(var i=0; i<tabs.length; ++i){
if(!tabs[i].incognito){
if(!currentBrowserState.windows.hasOwnProperty("window"+tabs[i].windowId)){
currentBrowserState.windows["window"+tabs[i].windowId]={};
currentBrowserState.windows["window"+tabs[i].windowId].id=tabs[i].windowId;
currentBrowserState.windows["window"+tabs[i].windowId].tabs={};
}
if(tabs[i].active)
currentBrowserState.windows["window"+tabs[i].windowId].activeTab=tabs[i].id;
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id]={};
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id].id=tabs[i].id;
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id].index=tabs[i].index;
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id].pinned=tabs[i].pinned;
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id].title=tabs[i].title;
currentBrowserState.windows["window"+tabs[i].windowId].tabs["tab"+tabs[i].id].url=tabs[i].url;
}
}
snapshot();
});
function snapshot(){
var currentSnapshot = {};
currentSnapshot.tabs=currentBrowserState.tabs;
currentSnapshot.windows=currentBrowserState.windows;
currentSnapshot.timestamp=Date.now();
chrome.storage.local.get("numSnapshots",function(result){
if(result.numSnapshots==undefined){
chrome.storage.local.set({"numSnapshots": 1});
chrome.storage.local.set({"numEvents": 0});
eventNum=0;
currentSnapshot.eventIndex=0;
chrome.storage.local.set({"timestamps": [currentSnapshot.timestamp]});
chrome.storage.local.set({"Snapshot0": currentSnapshot});
}else{
chrome.storage.local.set({"numSnapshots": result.numSnapshots+1});
chrome.storage.local.get("timestamps",function(result2){
result2.timestamps.push(currentSnapshot.timestamp);
chrome.storage.local.set({"timestamps": result2.timestamps});
});
chrome.storage.local.get("numEvents",function(result2){
eventNum=result2.numEvents;
currentSnapshot.eventIndex=result2.numEvents;
nextSnapshotEventIndex=result2.numEvents+snapshotEventInterval;
var store={};
store["Snapshot"+result.numSnapshots]=currentSnapshot;
chrome.storage.local.set(store);
});
}
});
}
chrome.browserAction.onClicked.addListener(function(tab) {
var optionsUrl = chrome.extension.getURL('options.html');
chrome.tabs.query({ url: optionsUrl }, function(results){
if (results.length) chrome.tabs.update(results[0].id, {active:true});
else chrome.tabs.create({url:optionsUrl});
});
});
function logEvent(eventObject){
eventObject.timestamp=Date.now();
var store={};
store["Event"+eventNum]=eventObject;
chrome.storage.local.set(store);
++eventNum;
chrome.storage.local.set({"numEvents": eventNum});
playEvent(currentBrowserState, eventObject);
if(eventNum>=nextSnapshotEventIndex){
var isDetached = false;
for(var tab in currentBrowserState.tabs){
isDetached = true;
break;
}
if(!isDetached)
snapshot();
}
}
chrome.tabs.onCreated.addListener(function(tab){
if(!tab.incognito){
var eventObject = {"eventType": "Created", "windowId": tab.windowId, "tabId": tab.id, "index": tab.index, "pinned": tab.pinned, "title": tab.title, "url": tab.url};
logEvent(eventObject);
}
});
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo){
if(currentBrowserState.windows.hasOwnProperty("window"+removeInfo.windowId) && currentBrowserState.windows["window"+removeInfo.windowId].tabs.hasOwnProperty("tab"+tabId)){
var eventObject = {"eventType": "Removed", "windowId": removeInfo.windowId, "tabId": tabId};
logEvent(eventObject);
}
});
chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId){
var tabExists = false;
var windowId = 0;
for(var chromeWindow in currentBrowserState.windows){
if(currentBrowserState.windows[chromeWindow].tabs.hasOwnProperty("tab"+removedTabId)){
tabExists = true;
windowId = currentBrowserState.windows[chromeWindow].id;
break;
}
}
if(tabExists){
var eventObject = {"eventType": "Replaced", "windowId": windowId, "tabId": removedTabId, "newTabId": addedTabId};
logEvent(eventObject);
}
});
chrome.tabs.onActivated.addListener(function(activeInfo){
if(currentBrowserState.windows.hasOwnProperty("window"+activeInfo.windowId) && currentBrowserState.windows["window"+activeInfo.windowId].tabs.hasOwnProperty("tab"+activeInfo.tabId)){
var eventObject = {"eventType": "Activated", "windowId": activeInfo.windowId, "tabId": activeInfo.tabId};
logEvent(eventObject);
}
});
chrome.tabs.onMoved.addListener(function(tabId, moveInfo){
if(currentBrowserState.windows.hasOwnProperty("window"+moveInfo.windowId) && currentBrowserState.windows["window"+moveInfo.windowId].tabs.hasOwnProperty("tab"+tabId)){
var eventObject = {"eventType": "Moved", "windowId": moveInfo.windowId, "tabId": tabId, "fromIndex": moveInfo.fromIndex, "toIndex": moveInfo.toIndex};
logEvent(eventObject);
}
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ // doesn't always fire when you navigate to google.com
if(!tab.incognito && currentBrowserState.windows.hasOwnProperty("window"+tab.windowId) && currentBrowserState.windows["window"+tab.windowId].tabs.hasOwnProperty("tab"+tabId)){
if(changeInfo.hasOwnProperty("pinned")){
if(changeInfo.pinned){
var eventObject = {"eventType": "Pinned", "windowId": tab.windowId, "tabId": tabId};
logEvent(eventObject);
}else{
var eventObject = {"eventType": "Unpinned", "windowId": tab.windowId, "tabId": tabId};
logEvent(eventObject);
}
}
if(tab.title != currentBrowserState.windows["window"+tab.windowId].tabs["tab"+tabId].title){
var eventObject = {"eventType": "Retitled", "windowId": tab.windowId, "tabId": tabId, "title": tab.title};
logEvent(eventObject);
}
if(changeInfo.hasOwnProperty("url") && changeInfo.url != currentBrowserState.windows["window"+tab.windowId].tabs["tab"+tabId].url){
var eventObject = {"eventType": "Navigated", "windowId": tab.windowId, "tabId": tabId, "url": changeInfo.url};
logEvent(eventObject);
}
}
});
chrome.tabs.onDetached.addListener(function(tabId, detachInfo){
if(currentBrowserState.windows.hasOwnProperty("window"+detachInfo.oldWindowId) && currentBrowserState.windows["window"+detachInfo.oldWindowId].tabs.hasOwnProperty("tab"+tabId)){
var eventObject = {"eventType": "Detached", "windowId": detachInfo.oldWindowId, "tabId": tabId, "position": detachInfo.oldPosition};
logEvent(eventObject);
}
});
chrome.tabs.onAttached.addListener(function(tabId, attachInfo){
if(currentBrowserState.tabs.hasOwnProperty("tab"+tabId)){
var eventObject = {"eventType": "Attached", "windowId": attachInfo.newWindowId, "tabId": tabId, "position": attachInfo.newPosition};
logEvent(eventObject);
}
});