Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
152 lines (135 loc) · 5.31 KB
/
Copy pathbackground.js
File metadata and controls
152 lines (135 loc) · 5.31 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
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
importScripts("sessionGrabber.js");
chrome.runtime.onMessageExternal.addListener(function (msg, sender, sendResponse) {
if (msg.ping && msg.ping == 'hello') {
sendResponse({pong: "hello"});
}
});
// The badge belongs to the tab the controller runs in. Messages from the injected
// pageController carry that tab in sender.tab; anything else (e.g. the popup) falls
// back to the active tab of the last focused window.
function resolveTabId(sender) {
if (sender && sender.tab && typeof sender.tab.id === 'number') {
return Promise.resolve(sender.tab.id);
}
return chrome.tabs.query({active: true, lastFocusedWindow: true}).then(function (tabs) {
return tabs.length > 0 ? tabs[0].id : undefined;
});
}
function setRemoteCountBadge(sender, data) {
resolveTabId(sender).then(function (tabId) {
if (tabId === undefined) return;
chrome.action.setBadgeText({text: data.info.remotes.length.toString(), tabId: tabId});
});
}
// 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') {
setRemoteCountBadge(sender, data);
}
}
if (event == 'info') {
if (data.type == 'client_connected' || data.type == 'client_disconnected') {
setRemoteCountBadge(sender, data);
}
}
}
if (msg.action == "sessionUpdate") {
chrome.storage.local.get(["session"], function (items) {
console.log(items)
if (items && items.session) {
Object.assign(msg.session, items.session);
}
chrome.storage.local.set({"session": msg.session}, function () {
});
});
}
if (msg.action == 'controlUpdate') {//TODO: properly handle disconnect event
resolveTabId(sender).then(function (tabId) {
if (tabId === undefined) return;
if (msg.active) {
chrome.action.setBadgeBackgroundColor({color: "#25bb25", tabId: tabId})
} else {
chrome.action.setBadgeBackgroundColor({color: "blue", tabId: tabId})
chrome.action.setBadgeText({text: "", tabId: tabId})
}
})
}
if (msg.action == "takeScreenshot") {
takeScreenshot().then(function (image) {
sendResponse({image: image});
}).catch(function (err) {
console.warn("Failed to take screenshot: " + err);
sendResponse({});
});
}
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 takeScreenshot() {
var tStart = Date.now();
return chrome.storage.local.get(["controlledWindow"]).then(function (items) {
var options = {format: "jpeg", quality: 50};
return typeof items.controlledWindow === 'number'
? chrome.tabs.captureVisibleTab(items.controlledWindow, options)
: chrome.tabs.captureVisibleTab(options);
}).then(function (image) {
var tTime = Date.now() - tStart;
var rStart = Date.now();
console.log("Take Screenshot time: " + tTime);
return resizeImage(image, 0.4).then(function (dataUrl) {
var rTime = Date.now() - rStart;
console.log("Resize time: " + rTime);
console.log("Total time: " + (Date.now() - tStart));
// Strip the "data:image/png;base64," header, the remote only wants the payload
var img = dataUrl.substring(dataUrl.indexOf(",") + 1);
var imgFileSize = Math.round((img.length) * 3 / 4);
console.info("Screenshot File Size: " + (imgFileSize / (1024)).toFixed(2) + "KB")
return img;
});
});
}
// Service workers have no DOM, so the resize runs on an OffscreenCanvas
// instead of an <img> plus a detached <canvas>.
function resizeImage(url, percent) {
return fetch(url).then(function (response) {
return response.blob();
}).then(function (blob) {
return createImageBitmap(blob);
}).then(function (bitmap) {
var canvas = new OffscreenCanvas(
Math.max(1, Math.round(bitmap.width * percent)),
Math.max(1, Math.round(bitmap.height * percent)));
console.info(canvas.width + "x" + canvas.height)
// Scale and draw the source image to the canvas
canvas.getContext("2d").drawImage(bitmap, 0, 0, canvas.width, canvas.height);
bitmap.close();
// Convert the canvas to a data URL in PNG format
return canvas.convertToBlob({type: "image/png"});
}).then(blobToDataUrl);
}
function blobToDataUrl(blob) {
return new Promise(function (resolve, reject) {
var reader = new FileReader();
reader.onloadend = function () {
resolve(String(reader.result));
};
reader.onerror = function () {
reject(reader.error);
};
reader.readAsDataURL(blob);
});
}