-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
210 lines (192 loc) · 7.51 KB
/
background.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var tab_id_dict = new Map();
mysocket = new WebSocket("ws://linus.casa:8000/");
mysocket.onopen = function(evt) { console.log('opened!');}
//browser.browserAction.onClicked.addListener(handle_click);
//function handle_click(tab) {
//console.log('sending tab!');
//mysocket.send(JSON.stringify(tab));
//}
mysocket.onmessage = function(evt) {
console.log(evt.data);
//var tablist = [];
//browser.tabs.query({}).then((tabs)=>{
//tablist = tabs;
//});
//console.log(tablist);
data = JSON.parse(evt.data);
switch (data.kind) {
//case 'create_tab':
//console.log('create!');
//browser.tabs.onCreated.removeListener(handle_created);
//var tabPromise = browser.tabs.create({});
//tabPromise.then(function(tab) {
//tab_id_dict.set(data.payload.id, tab.id);
//updateMappingMessage(tab.id, data.payload.id);
//browser.tabs.onCreated.addListener(handle_created);
//});
//break;
case 'activate_tab':
console.log('receiving activate! ' + data.payload.id);
other_tab = data.payload;
other_tab['active'] = true;
if(!tab_id_dict.get(other_tab.id)){
console.log('on receving activate, didn\'t find id ' + other_tab.id);
handle_invalid_tab(other_tab);
} else {
console.log("on receiving activate a valid tab found your id " + other_tab.id);
browser.tabs.onActivated.removeListener(handle_activated);
browser.tabs.onUpdated.removeListener(handle_updated);
updatePromise = browser.tabs.update(tab_id_dict.get(data.payload.id), {"active": true});
updatePromise.then(function(tab) {
browser.tabs.onActivated.addListener(handle_activated);
browser.tabs.onUpdated.addListener(handle_updated);
});
}
break;
case 'remove_tab':
console.log('receiving remove!' + data.payload.id);
var soonToBeDeadTab = tab_id_dict.get(data.payload.id);
if(soonToBeDeadTab){
browser.tabs.onRemoved.removeListener(handle_removed);
browser.tabs.remove(soonToBeDeadTab);
tab_id_dict.delete(data.payload.id);
browser.tabs.onRemoved.addListener(handle_removed);
}
break;
case 'update_tab':
var url = data.payload.url;
var id = data.payload.id;
var active = data.payload.active;
if(!active){
active = false;
}
console.log('receiving update tab! with ' + id + ' and ' + url + 'and active ' + active);
// TODO : factor out update map case with activate_tab case
if(!tab_id_dict.get(id)){
console.log('on received update, didn\'t find id in map, handling...');
handle_invalid_tab(data.payload);
}
else{
var mytab = browser.tabs.get(tab_id_dict.get(id));
mytab.then((tab) => {
console.log('on received update, comparing my url '+ tab.url + ' with your url ' + url);
if(tab.url != url){
console.log('urls are different! we are updating and active is ' + active);
//browser.tabs.onCreated.removeListener(handle_created);
browser.tabs.onUpdated.removeListener(handle_updated);
var tabPromise = browser.tabs.update(tab_id_dict.get(id), {"url": url, "active": active});
tabPromise.then((tab) => {
browser.tabs.onUpdated.addListener(handle_updated);
//browser.tabs.onCreated.addListener(handle_created);
});
}
});
}
break;
case 'replace_tab':
console.log('replace!');
break;
case 'update_mapping':
console.log('receiving updating map!');
myid = data.payload['yourid'];
yourid = data.payload['myid'];
tab_id_dict.set(yourid, myid);
console.log(tab_id_dict);
break;
default:
console.log('I dont know what to do with this data!');
}
}
function setTabId(keyId, valId){
}
function handle_invalid_tab(tab){
// create tab, update map, send updtade map messgae
var url = tab.url;
var active = tab.active;
var id = tab.id;
console.log("Sending invalid tab ID update message");
browser.tabs.onUpdated.removeListener(handle_updated);
//browser.tabs.onCreated.removeListener(handle_created);
if(url === 'about:newtab'){
var tabPromise = browser.tabs.create({'active':active});
}else {
var tabPromise = browser.tabs.create({"url": url, "active": active});
}
tabPromise.then((tabObj) => {
tab_id_dict.set(id, tabObj.id);
updateMappingMessage(tabObj.id, id);
browser.tabs.onUpdated.addListener(handle_updated);
//browser.tabs.onCreated.addListener(handle_created);
});
}
function updateMappingMessage(myid, yourid){
var msg = create_message('update_mapping');
msg.payload = {'myid' : myid, 'yourid' : yourid};
mysocket.send(JSON.stringify(msg));
}
browser.tabs.onActivated.addListener(handle_activated);
//browser.tabs.onCreated.addListener(handle_created);
browser.tabs.onRemoved.addListener(handle_removed);
browser.tabs.onReplaced.addListener(handle_replaced);
browser.tabs.onUpdated.addListener(handle_updated);
function create_message(kind){
return {'kind': kind};
}
//function handle_created(tab){
//var msg = create_message('create_tab');
//console.log('sending a create!');
//msg.payload = tab;
//mysocket.send(JSON.stringify(msg));
//}
function handle_activated(active_info){
// active_info = { tabId: <num>, windowId, <num>}
tab_promise = browser.tabs.get(active_info.tabId);
tab_promise.then(function(tab) {
var msg = create_message('activate_tab');
msg.payload = tab;
console.log('sending an activate! with id ' + tab.id + ' and active ' + tab.active);
console.log(msg);
mysocket.send(JSON.stringify(msg));
});
}
function handle_replaced(tab){
var msg = create_message('replace_tab');
msg.payload = tab;
//mysocket.send(JSON.stringify(msg));
}
function handle_removed(id){
console.log('sending a removed!');
var msg = create_message('remove_tab');
msg.payload = {'id': id};
//var dieTabId;
for (var kid in tab_id_dict){
if(tab_id_dict.get(kid) == id){
//dieTabId = kid;
tab_id_dict.delete(kid);
break;
}
}
//tab_id_dict.delete(dieTabId);
console.log(msg);
mysocket.send(JSON.stringify(msg));
}
function handle_updated(id, update_info){
if(update_info.url){
console.log(id);
var msg = create_message('update_tab');
console.log('sending a updated for a new url! ' + id + ' and url ' + update_info.url);
console.log(update_info);
active = update_info.active;
if(!active){
active = false;
}
msg.payload = {'url': update_info.url, 'id' : id, 'active' : active};
mysocket.send(JSON.stringify(msg));
} else if(update_info.active) {
console.log('sending an updated for active tab! (not sure why we get this...) with id ' + id);
console.log(update_info);
var msg = create_message('update_tab');
msg.payload = {'id' : id , 'active' : update_info.active };
mysocket.send(JSON.stringify(msg));
}
}