-
Notifications
You must be signed in to change notification settings - Fork 2
/
service_worker.js
72 lines (62 loc) · 2.44 KB
/
service_worker.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
'use strict';
var connections = {};
chrome.runtime.onConnect.addListener(function (port) {
var extensionListener = function (message) {
// The original connection event doesn't include the tab ID of the
// DevTools page, so we need to send it explicitly.
if (message.name == 'init') {
connections[message.tabId] = port;
return;
}
}
// Listen to messages sent from the DevTools page
port.onMessage.addListener(extensionListener);
port.onDisconnect.addListener(function (port) {
port.onMessage.removeListener(extensionListener);
var tabs = Object.keys(connections);
for (var i = 0, len = tabs.length; i < len; i++) {
if (connections[tabs[i]] == port) {
delete connections[tabs[i]];
break;
}
}
});
});
// Receive message from content script and relay to the devTools page for the
// current tab
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// Messages from content scripts should have sender.tab set
if (sender.tab) {
let tabId = sender.tab.id;
if (request.payload && request.payload.mojitoDetected) {
// change extension icon to enable state
chrome.action.setIcon({
tabId: tabId,
path: 'images/logo.png'
});
if (request.payload.mojitoVersion) {
chrome.action.setPopup({
tabId: tabId,
popup: 'popups/popup.html?mojitoVersion=' + request.payload.mojitoVersion + '&containerName=' + request.payload.containerName + '&pageHostname=' + request.payload.pageHostname
});
} else {
chrome.action.setPopup({
tabId: tabId,
popup: 'popups/popup.html'
});
}
if ('count' in request.payload) {
// update count of activated experiments on the page
chrome.action.setBadgeText({ text: String(request.payload.count), tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: '#3498db', tabId: tabId });
}
}
if (tabId in connections) {
/*if (request.target == 'Panel') {
connections[tabId].postMessage(request);
}*/
connections[tabId].postMessage(request);
}
}
return true;
});