forked from mrflix/dimensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
153 lines (125 loc) · 3.29 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
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
const tabs = {};
function toggle(tab) {
if (!tabs[tab.id])
addTab(tab);
else
deactivateTab(tab.id);
}
function addTab(tab) {
tabs[tab.id] = Object.create(dimensions);
tabs[tab.id].activate(tab);
}
function deactivateTab(id) {
tabs[id].deactivate();
}
function removeTab(id) {
for (const tabId in tabs) {
if (tabId == id)
delete tabs[tabId];
}
}
let lastBrowserAction = null;
chrome.action.onClicked.addListener(function (tab) {
if (lastBrowserAction && Date.now() - lastBrowserAction < 10) {
// fix bug in Chrome Version 49.0.2623.87
// that triggers browserAction.onClicked twice
// when called from shortcut _execute_browser_action
return;
}
toggle(tab);
lastBrowserAction = Date.now();
});
chrome.runtime.onConnect.addListener(function (port) {
tabs[port.sender.tab.id].initialize(port);
});
chrome.runtime.onSuspend.addListener(function () {
for (const tabId in tabs) {
tabs[tabId].deactivate(true);
}
});
const dimensions = {
alive: true,
activate: function (tab) {
this.tab = tab;
this.onBrowserDisconnectClosure = this.onBrowserDisconnect.bind(this);
this.receiveBrowserMessageClosure = this.receiveBrowserMessage.bind(this);
chrome.scripting
.insertCSS(
{
target: { tabId: this.tab.id },
files: ['tooltip.css']
});
chrome.scripting.executeScript(
{
target: { tabId: this.tab.id },
files: ['utils.js', 'tooltip.chrome.js']
});
chrome.action.setIcon({
tabId: this.tab.id,
path: {
16: "images/icon16_active.png",
19: "images/icon19_active.png",
32: "images/icon16_active@2x.png",
38: "images/icon19_active@2x.png"
}
});
},
deactivate: function (silent) {
if (!this.port) {
// not yet initialized
this.alive = false;
return;
}
if (!silent) {
console.log('background -> browser: destroy');
this.port.postMessage({ type: 'destroy' });
}
this.port.onMessage.removeListener(this.receiveBrowserMessageClosure);
this.port.onDisconnect.removeListener(this.onBrowserDisconnectClosure);
chrome.action.setIcon({
tabId: this.tab.id,
path: {
16: "images/icon16.png",
19: "images/icon19.png",
32: "images/icon16@2x.png",
38: "images/icon19@2x.png"
}
});
removeTab(this.tab.id);
},
onBrowserDisconnect: function () {
this.deactivate(true);
},
initialize: function (port) {
this.port = port;
if (!this.alive) {
// was deactivated whilest still booting up
this.deactivate();
return;
}
this.port.onMessage.addListener(this.receiveBrowserMessageClosure);
this.port.onDisconnect.addListener(this.onBrowserDisconnectClosure);
},
receiveBrowserMessage: function (event) {
switch (event.type) {
case 'take screenshot':
this.takeScreenshot();
break;
case 'close_overlay':
this.deactivate();
break;
}
},
takeScreenshot: function () {
chrome.tabs.captureVisibleTab({ format: "png" }, (dataUrl) => {
this.port.postMessage({
type: 'screen data',
data: {
width: this.tab.width,
height: this.tab.height,
imgDataUrl: dataUrl,
}
});
});
}
};