-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
137 lines (115 loc) · 4.03 KB
/
index.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
console.log("From index.js");
const { BrowserWindow } = require('@electron/remote')
const {desktopCapturer,ipcRenderer} = require('electron');
var imgCropWindow;
function createScreenshotWindow(requestType){
takeScreenshot(function(imageURL){
var request = {
imageURL:imageURL,
type:requestType
}
imgCropWindow = new BrowserWindow({
frame:false,
fullscreen:true,
show:false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
})
imgCropWindow.loadFile('mask.html').then(()=>{
imgCropWindow.webContents.send( 'request-object',request);
})
imgCropWindow.once('ready-to-show', () => {
imgCropWindow.show()
})
},'image/png');
}
// 1 - Mask to crop the image
// 2 - Mask to extract text
function takeScreenshot(callback, imageFormat) {
var _this = this;
this.callback = callback;
imageFormat = imageFormat || 'image/jpeg';
this.handleStream = (stream) => {
// _this.callback("s")
// 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';
video.style.width = this.videoWidth + 'px';
video.play();
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) {
_this.callback(canvas.toDataURL(imageFormat));
return 0;
} else {
console.log('Need callback!');
}
video.remove();
try {
stream.getTracks()[0].stop();
} catch (e) {}
}
video.srcObject = stream;
document.body.appendChild(video);
};
this.handleError = function(e) {
console.log(e);
};
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
for (const source of sources) {
// Filter: main screen
if ((source.name === "Entire Screen") || (source.name === "Screen 1") || (source.name === "Screen 2")) {
try{
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: source.id,
minWidth: 1280,
maxWidth: 4000,
minHeight: 720,
maxHeight: 4000
}
}
});
_this.handleStream(stream);
} catch (e) {
_this.handleError(e);
}
}
}
});
}
const newMask = document.getElementById('createMask');
newMask.addEventListener('click', function(event){
console.log("making")
createScreenshotWindow(1);
})
var ocrMask = document.getElementById('ocrMask');
ocrMask.addEventListener('click', function(){
console.log("making")
createScreenshotWindow(2);
})
ipcRenderer.on('key-shortcut', function(args){
console.log("making");
createScreenshotWindow(1);
})
ipcRenderer.on('key-shortcut-ocr', function(args){
console.log("making")
createScreenshotWindow(2);
})
ipcRenderer.on('close-crop', function(args){
imgCropWindow.close();
})