import { app, BrowserWindow, ipcMain, protocol } from 'electron'; import path from 'path'; import log from 'electron-log'; import os from 'os'; import fs from 'fs-extra'; import { config } from './config'; import { Apis } from './types/apis'; import { EMessageType, IMessage } from './types/message'; import { sendAttachment, sendMessage } from './helpers/messageScript'; // import { sequelize } from './sql/connection'; if (!config.isDev) { // eslint-disable-next-line no-console console.log = log.log; console.info = log.info; console.error = log.error; } // sequelize // .authenticate() // .then(() => { // return sequelize.sync({ alter: true }); // }) // .catch((e) => { // console.error(e); // }); // const sendScript = ` // on run {targetBuddyPhone} // activate application "Messages" // tell application "System Events" to tell process "Messages" // key code 45 using command down // keystroke targetBuddyPhone // key code 36 // end tell // end run`; const sendScript = ` on run {targetBuddyPhone, targetMessage} activate application "Messages" tell application "System Events" to tell process "Messages" key code 45 using command down -- press Command + N to start a new window delay 0.2 set the clipboard to targetBuddyPhone keystroke "v" using command down delay 0.2 key code 48 -- press Enter to focus on the message area key code 36 key code 36 set the clipboard to targetMessage keystroke "v" using command down delay 0.3 key code 36 key code 48 -- press Enter to send end tell end run`; const sendImageScript = ` on run {targetBuddyPhone, targetAttachment} set a to POSIX file targetAttachment tell application "Messages" set targetBuddy to targetBuddyPhone set targetService to id of 1st service whose service type = iMessage set textMessage to a set theBuddy to buddy targetBuddy of service id targetService send textMessage to theBuddy end tell end run`; fs.writeFileSync(config.sendScriptPath, sendScript); fs.writeFileSync(config.sendAttachmentScriptPath, sendImageScript); let isInitialized = false; let mainWindow: BrowserWindow | null; const createWindow = () => { // Create the browser window. mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { contextIsolation: false, preload: path.join(__dirname, 'preload.js'), }, }); // and load the index.html of the app. if (config.isDev) { mainWindow.loadURL(`http://localhost:9000`); } else { mainWindow.loadURL(`file://${path.join(__dirname, `react/index.html`)}`); } if (!isInitialized) { isInitialized = true; ipcMain.handle(Apis.GET_USERNAME, () => { return os.userInfo().username; }); ipcMain.handle(Apis.SEND_MESSAGE, async (event, to: string, messages: IMessage[]) => { for (let i = 0; i < messages.length; i++) { const msg = messages[i]; if (msg.type === EMessageType.IMAGE) { // eslint-disable-next-line no-await-in-loop await sendAttachment(to.replace(/^0/, '+66'), msg.value); } else { // eslint-disable-next-line no-await-in-loop await sendMessage(to.replace(/^0/, '+66'), msg.value); } } // return imessage.send('+66982549638', '123123!'); }); } mainWindow.on('closed', () => { mainWindow = null; }); }; // 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(); protocol.registerFileProtocol('atom', (request, callback) => { const url = request.url.substr(7); callback({ path: url }); }); app.on('activate', () => { // 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', async () => { if (process.platform !== 'darwin') { // await sequelize.close(); app.quit(); } }); // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.