generated from sindresorhus/electron-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (125 loc) · 3.85 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
138
139
140
141
142
143
144
145
"use strict";
const path = require("path");
const { app, BrowserWindow, Menu, protocol, shell } = require("electron");
/// const {autoUpdater} = require('electron-updater');
const { is } = require("electron-util");
const unhandled = require("electron-unhandled");
const debug = require("electron-debug");
const contextMenu = require("electron-context-menu");
const config = require("./config.js");
const menu = require("./menu.js");
const windows = require("./windows.js");
const { autoUpdater } = require("electron-updater");
const { parsePhoneNumber } = require("libphonenumber-js/mobile");
unhandled();
contextMenu();
if (is.windows) {
windows().then();
}
// Note: Must match `build.appId` in package.json
app.setAppUserModelId("me.lucasfreitas.WhatsAppCaller");
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient("tel", process.execPath, [
path.resolve(process.argv[1]),
]);
app.setAsDefaultProtocolClient("callto", process.execPath, [
path.resolve(process.argv[1]),
]);
}
} else {
app.setAsDefaultProtocolClient("tel");
app.setAsDefaultProtocolClient("callto");
}
// Prevent window from being garbage collected
let mainWindow;
const createMainWindow = async () => {
const win = new BrowserWindow({
title: app.name,
show: false,
width: 1,
height: 1,
});
win.on("ready-to-show", () => {
// win.show();
});
win.on("closed", () => {
// Dereference the window
// For multiple windows store them in an array
mainWindow = undefined;
});
return win;
};
// Prevent multiple instances of the app
if (!app.requestSingleInstanceLock()) {
app.quit();
}
app.on("second-instance", () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.show();
}
});
app.on("window-all-closed", () => {
app.quit();
});
app.on("activate", async () => {
if (!mainWindow) {
mainWindow = await createMainWindow();
}
});
const parseNumberManual = (number) => {
const cleanRegex = new RegExp(/^(\+)|\D/, "g");
let cleanNumber = String(number).trim();
cleanNumber.replace(/^(tel|callto):\/\//, "");
cleanNumber = cleanNumber.replace(cleanRegex, "$1");
if (cleanNumber.startsWith("0")) {
cleanNumber = "49" + cleanNumber.substr(1);
}
return cleanNumber;
};
const parseNumber = (number) => {
let cleanNumber = String(number).trim();
cleanNumber = cleanNumber.replace(/^(tel|callto):/g, "");
cleanNumber = cleanNumber.replace(/(%20|%32|%43|%45)/g, "");
const tryWithDE = parsePhoneNumber(cleanNumber, "DE");
const tryWithBR = parsePhoneNumber(cleanNumber, "BR");
if (tryWithDE.isValid() && tryWithDE.getType() === 'MOBILE') {
return String(tryWithDE.number).replace('+', '')
}
if (tryWithBR.isValid() && tryWithBR.getType() === 'MOBILE') {
return String(tryWithBR.number).replace('+', '')
}
};
app.on("open-url", async (event, url) => {
const number = parseNumber(url);
const status = await handleWhatsAppLink(mainWindow, number);
app.quit();
});
app.on("ready", async () => {
await autoUpdater.checkForUpdatesAndNotify();
});
autoUpdater.on("update-downloaded", (info) => {
autoUpdater.quitAndInstall();
});
const handleWhatsAppLink = async (win, phoneNumber) => {
const isProtocolRegistered = protocol.isProtocolRegistered("whatsapp");
if (isProtocolRegistered) {
return await shell.openExternal(`whatsapp://send?phone=${phoneNumber}`);
} else {
return await shell.openExternal(`https://api.whatsapp.com/send/?phone=${phoneNumber}`);
}
};
const noop = (request, callback) => {
console.log(request);
};
(async () => {
await app.whenReady();
protocol.registerStringProtocol("callto", noop);
protocol.registerStringProtocol("tel", noop);
// If we are running a non-packaged version of the app && on windows
Menu.setApplicationMenu(menu);
mainWindow = await createMainWindow();
})();