-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
87 lines (75 loc) · 1.88 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
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// if (request.type == "snapshot") {
// sendSnapshotToServer(request.payload);
// return true;
// }
routeMessage(request.type);
return true;
});
function routeMessage(messageType) {
switch (messageType) {
case "openWS":
openConnection();
break;
case "closeWS":
closeConnection();
break;
case "zoom-in":
zoomIn();
break;
case "zoom-out":
zoomOut();
break;
default:
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.sendMessage(tab.id, { type: messageType, tabID: tab.id });
});
break;
}
}
function zoomIn() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.getZoom(tab.id, function(zoomFactor) {
chrome.tabs.setZoom(tab.id, zoomFactor + 0.2);
});
});
}
function zoomOut() {
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.getZoom(tab.id, function(zoomFactor) {
chrome.tabs.setZoom(tab.id, zoomFactor - 0.2);
});
});
}
var ws = null;
function closeConnection() {
if (ws)
ws.close();
}
function openConnection() {
closeConnection();
var url = "ws://localhost:9001";
ws = new WebSocket(url);
ws.onopen = onOpen;
ws.onclose = onClose;
ws.onmessage = onMessage;
ws.onerror = onError;
}
function onOpen() {
console.log("Websocket connected.");
}
function onClose() {
console.log("Websocket disconnected.");
ws = null;
}
function onMessage(event) {
console.log(event);
routeMessage(event.data);
}
function onError(event) {
alert("Websocket error.");
}
// function sendSnapshotToServer(snapshot) {
// if (ws)
// ws.send(snapshot);
// }