forked from hamilton-lima/cap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
163 lines (139 loc) · 4.21 KB
/
renderer.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
154
155
156
157
158
159
160
161
162
163
const { desktopCapturer, ipcRenderer } = require("electron");
const { app } = require("electron").remote;
const fs = require("fs");
console.log("desktopCapturer", desktopCapturer);
function capture() {
fullscreenScreenshot(function(base64data) {
saveImage(base64data);
copyToclipboard(base64data);
}, "image/png");
}
ipcRenderer.on("capture", data => {
log("on capture");
capture();
});
function copyToclipboard(base64data) {
const nativeImage = require("electron").nativeImage;
const { clipboard } = require("electron");
const image = nativeImage.createFromDataURL(base64data);
clipboard.writeImage(image);
}
function log(message) {
console.log(message);
}
function error(error) {
console.error(error);
}
function getDesktop() {
return app.getPath("desktop");
}
function getSavePath() {
console.log("getSavePath()");
let rawData = fs.readFileSync("settings.json");
settings = JSON.parse(rawData); //TODO: Read settings once and store as global var.
console.log("rawData: " + rawData);
console.log(settings);
if (settings.saveDir != "") {
return settings.saveDir;
}
return getDesktop();
}
function getTimeStampForFileName() {
return new Date()
.toISOString()
.replace("T", "_")
.replace(/:/g, "-")
.replace("Z", "")
.trim();
}
function getFileName() {
const path = require("path");
const result = getSavePath() + path.sep + getTimeStampForFileName() + ".png";
return result;
}
function saveImage(data) {
var base64Data;
var binaryData;
const fileName = getFileName();
log("saving image to: " + fileName);
base64Data = data.replace(/^data:image\/png;base64,/, "");
base64Data += base64Data.replace("+", " ");
binaryData = new Buffer(base64Data, "base64").toString("binary");
try {
fs.writeFileSync(fileName, binaryData, "binary");
} catch (e) {
error("Error saving file", e);
}
}
/**
* Create a screenshot of the entire screen using the desktopCapturer module of Electron.
*
* @param callback {Function} callback receives as first parameter the base64 string of the image
* @param imageFormat {String} Format of the image to generate ('image/jpeg' or 'image/png')
**/
function fullscreenScreenshot(callback, imageFormat) {
var _this = this;
this.callback = callback;
imageFormat = imageFormat || "image/jpeg";
this.handleStream = stream => {
// Create hidden video tag
var video = document.createElement("video");
video.style.cssText = "position:absolute;top:-10000px;left:-10000px;";
// Event connected to stream
video.onloadedmetadata = function() {
// Set video ORIGINAL height (screenshot)
video.style.height = this.videoHeight + "px"; // videoHeight
video.style.width = this.videoWidth + "px"; // videoWidth
// Create canvas
var canvas = document.createElement("canvas");
canvas.width = this.videoWidth;
canvas.height = this.videoHeight;
var ctx = canvas.getContext("2d");
// Draw video on canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
if (_this.callback) {
// Save screenshot to base64
_this.callback(canvas.toDataURL(imageFormat));
}
// Remove hidden video tag
video.remove();
try {
// Destroy connect to stream
stream.getTracks()[0].stop();
} catch (e) {}
};
video.src = URL.createObjectURL(stream);
document.body.appendChild(video);
};
this.handleError = function(e) {
console.log(e);
};
// Filter only screen type
desktopCapturer.getSources({ types: ["screen"] }, (error, sources) => {
if (error) throw error;
for (let i = 0; i < sources.length; ++i) {
console.log(sources);
// Filter: main screen
if (sources[i].name === "Entire screen") {
navigator.webkitGetUserMedia(
{
audio: false,
video: {
mandatory: {
chromeMediaSource: "desktop",
chromeMediaSourceId: sources[i].id,
minWidth: 1280,
maxWidth: 4000,
minHeight: 720,
maxHeight: 4000
}
}
},
this.handleStream,
this.handleError
);
return;
}
}
});
}