-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
104 lines (93 loc) · 2.38 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
side_by_side = {
on: true,
setOn: function (on) {
side_by_side.on = on;
if(side_by_side.isOn()) {
chrome.browserAction.setBadgeText({text: 'on'});
chrome.browserAction.setBadgeBackgroundColor({color: "green"});
} else {
chrome.browserAction.setBadgeText({text: 'off'});
chrome.browserAction.setBadgeBackgroundColor({color: "gray"});
}
},
isOn: function () {
return side_by_side.on;
},
toggle: function () {
side_by_side.setOn(!side_by_side.isOn());
},
current_id: null,
current_index: null
}
chrome.browserAction.onClicked.addListener(function (){
side_by_side.toggle();
});
var selectedTabs = {};
chrome.tabs.onHighlighted.addListener(function (highlightedInfo) {
selectedTabs[highlightedInfo.windowId] = highlightedInfo.tabIds;
});
var ports = {};
var focus_tab_id = '';
chrome.extension.onConnect.addListener(function (port) {
console.assert(port.name === 'side_by_side');
var tab_id = port.sender.tab.id;
ports[tab_id] = port;
console.log('port is connected from tabId:' + tab_id);
port.onMessage.addListener(function emit(msg) {
if (!side_by_side.isOn()) {
return;
}
switch(msg.type) {
case 'scroll':
var x = msg.window_scrollX;
var y = msg.window_scrollY;
sendToSelectedTabs(port, msg);
break;
case 'click':
var cx = msg.window_clickX;
var cy = msg.window_clickY;
sendToSelectedTabs(port, msg);
break;
case 'focus':
focus_tab_id = tab_id;
break;
case 'load':
if(focus_tab_id == tab_id){
msg.type = 'setfocus';
sendToTab(tab_id, port, msg);
}
break;
case 'doCompare':
console.log('START COMPARE');
break;
default:
console.log('Unhandeled: '+ msg.type);
}
});
});
function selectedTabIds() {
var tabIds = [],
window_id;
for (window_id in selectedTabs) {
if (selectedTabs.hasOwnProperty(window_id)) {
tabIds.push(selectedTabs[window_id]);
}
}
console.log('Selected tab ids: '+tabIds.join(','));
return tabIds;
}
function sendToSelectedTabs(port, msg) {
var tabIds = selectedTabIds(),i;
for (i = 0; i < tabIds.length; i++) {
var tab_id = tabIds[i];
if (ports[tab_id] && ports[tab_id] !== port) {
ports[tab_id].postMessage(msg);
}
}
}
function sendToTab(tab_id, port, msg) {
if (ports[tab_id]) {
ports[tab_id].postMessage(msg);
}
}
side_by_side.setOn(true);