-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.js
228 lines (183 loc) · 5.17 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Modules to control application life and create native browser window
const { ipcMain, app, BrowserWindow, dialog } = require('electron');
const fs = require('fs');
const path = require('path');
const { shell } = require('electron');
const Store = require('electron-store');
//const contextMenu = require('electron-context-menu');
let mainWindow;
/**
* Create the main application window.
*/
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1280,
height: 720,
minWidth: 1280,
minHeight: 720,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
spellcheck: true
},
icon: path.join(__dirname, 'app/img/icon.png')
})
// and load the index.html of the app.
mainWindow.loadFile('app/index.html')
// No menu bar
mainWindow.setMenuBarVisibility(false)
// Open external links in default browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' };
});
// Show dev tools
//mainWindow.webContents.openDevTools()
//contextMenu();
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
/**
* ----------------------------------------------------------
* Map E-Mail Scraper functions
*
* These functions are called from the renderer process.
* ----------------------------------------------------------
*/
const store = new Store();
/**
* Gets the saved user data.
*
* @returns {Promise}
*/
ipcMain.handle('getData', async (event, arg) => {
var data = store.get('data');
// If there is no data, return an empty object
if (data == undefined) {
data = {
googleAPIKey: "",
hunterAPIKey: "",
collections: []
};
}
return data;
});
/**
* Saves the user data.
*
* @returns {Promise}
*/
ipcMain.handle('saveData', async (event, arg) => {
var data = {
googleAPIKey: arg.googleAPIKey,
hunterAPIKey: arg.hunterAPIKey,
collections: arg.collections
};
// Save the data with electron store
store.set('data', data);
return true;
});
/**
* Export the data to a CSV file.
*
* @returns {Promise}
*/
ipcMain.handle('saveCSV', async (event, arg) => {
var toLocalPath = path.resolve(app.getPath("desktop"), "map-email-scraper.csv");
dialog.showSaveDialog({
defaultPath: toLocalPath,
filters: [
{ name: 'CSV', extensions: ['csv'] }
]
}).then((result) => {
// If the user cancelled the save dialog, return
if (result.canceled) {
return;
}
// Get the file path
var filePath = result.filePath;
// Get all collections
var data = store.get('data');
var collections = data.collections;
// Put all collections into one array
var allCollections = [];
for (var i = 0; i < collections.length; i++) {
for (var j = 0; j < collections[i].results.length; j++) {
allCollections.push(collections[i].results[j]);
}
}
// Create header from object keys of first collection
var header = Object.keys(allCollections[0]).join(",") + "\r";
// Create CSV string
var csv = header;
for (var i = 0; i < allCollections.length; i++) {
var row = "";
for (var key in allCollections[i]) {
if (row != "") {
row += ",";
}
// If the value is an array, join it with a semicolon
if (Array.isArray(allCollections[i][key])) {
allCollections[i][key] = allCollections[i][key].join(";");
}
row += allCollections[i][key];
}
csv += row + "\r";
}
// Write CSV to file
fs.writeFileSync(filePath, csv);
});
})
/**
* Export the data to a JSON file.
*
* @returns {Promise}
*/
ipcMain.handle('saveJSON', async (event, arg) => {
var toLocalPath = path.resolve(app.getPath("desktop"), "map-email-scraper.json");
dialog.showSaveDialog({
defaultPath: toLocalPath,
filters: [
{ name: 'JSON', extensions: ['json'] }
]
}).then((result) => {
// If the user cancelled the save dialog, return
if (result.canceled) {
return;
}
// Get the file path
var filePath = result.filePath;
// Get all collections
var data = store.get('data');
var json = JSON.stringify(data.collections);
// Write CSV to file
fs.writeFileSync(filePath, json);
});
});
/**
* Show an alert message.
*/
ipcMain.on("send-alert", (event, incomingMessage) => {
const options = {
type: "none",
buttons: ["Okay"],
title: "Alert",
message: incomingMessage
}
dialog.showMessageBox(mainWindow, options);
});