-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
108 lines (94 loc) · 3.94 KB
/
background.js
File metadata and controls
108 lines (94 loc) · 3.94 KB
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
chrome.runtime.onMessageExternal.addListener(function (msg, sender, sendResponse) {
if (msg.ping && msg.ping == 'hello') {
sendResponse({pong: "hello"});
}
});
// Handle messages from the pageController script
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.action == 'socketEvent') {
var event = msg.event;
var data = msg.data;
if (event == 'init') {
if (data.state == 'success') {
chrome.tabs.getSelected(null, function (tab) {
chrome.browserAction.setBadgeText({text: data.info.remotes.length.toString(), tabId: tab.id})
})
}
}
if (event == 'info') {
if (data.type == 'client_connected' || data.type == 'client_disconnected') {
chrome.tabs.getSelected(null, function (tab) {
chrome.browserAction.setBadgeText({text: data.info.remotes.length.toString(), tabId: tab.id})
})
}
}
}
if (msg.action == "sessionUpdate") {
chrome.storage.local.get(["session"], function (items) {
console.log(items)
if (items && items.session) {
$.extend(msg.session, items.session);
}
chrome.storage.local.set({"session": msg.session}, function () {
});
});
}
if (msg.action == 'controlUpdate') {//TODO: properly handle disconnect event
chrome.tabs.getSelected(null, function (tab) {
if (msg.active) {
chrome.browserAction.setBadgeBackgroundColor({color: "#25bb25", tabId: tab.id})
} else {
chrome.browserAction.setBadgeBackgroundColor({color: "blue", tabId: tab.id})
chrome.browserAction.setBadgeText({text: "", tabId: tab.id})
}
})
}
if (msg.action == "takeScreenshot") {
var tStart = Date.now();
chrome.storage.local.get(["controlledWindow"], function (items) {
chrome.tabs.captureVisibleTab(items.controlledWindow, {format: "jpeg", quality: 50}, function (image) {
var tTime = Date.now() - tStart;
var rStart = Date.now();
console.log("Take Screenshot time: " + tTime);
resizeImage(image, 0.4, function (img) {
var rTime = Date.now() - rStart;
console.log("Resize time: " + rTime);
console.log("Total time: " + (Date.now() - tStart));
// console.log(img)
var head = 'data:image/png;base64,';
img = img.substring(head.length);
var imgFileSize = Math.round((img.length) * 3 / 4);
console.info("Screenshot File Size: " + (imgFileSize / (1024)).toFixed(2) + "KB")
sendResponse({image: img});
})
});
});
}
return true;
})
chrome.storage.onChanged.addListener(function (changes, area) {
if (area == 'local') {
console.log("changes: " + JSON.stringify(changes))
if (changes.session) {
}
}
})
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
console.log(changeInfo.status)
console.log(tab.url);
});
function resizeImage(url, percent, callback) {
var sourceImage = new Image();
sourceImage.onload = function () {
// Create a canvas with the desired dimensions
var canvas = document.createElement("canvas");
canvas.width = sourceImage.width * percent;
canvas.height = sourceImage.height * percent;
console.info(canvas.width + "x" + canvas.height)
// Scale and draw the source image to the canvas
canvas.getContext("2d").drawImage(sourceImage, 0, 0, sourceImage.width * percent, sourceImage.height * percent);
// Convert the canvas to a data URL in PNG format
callback(canvas.toDataURL());
}
sourceImage.src = url;
}