Skip to content

Commit

Permalink
send image logic DONE
Browse files Browse the repository at this point in the history
  • Loading branch information
luizfverissimo committed May 10, 2021
1 parent 5fa0869 commit 9b1e756
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
7 changes: 7 additions & 0 deletions api/imageSendApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { clipboard, nativeImage } = require("electron");

function imageCopyToClipboard(path) {
clipboard.writeImage(nativeImage.createFromPath(path));
}

module.exports = imageCopyToClipboard
20 changes: 14 additions & 6 deletions api/senderApi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const puppeteer = require('puppeteer-core');
const stringInject = require('stringinject');
const locateChrome = require('locate-chrome');
const { clipboard, nativeImage } = require('electron');

function createMessage(message, infos) {
const messageCreated = stringInject.default(message, infos);
Expand All @@ -13,16 +14,12 @@ function createUrl(message, phone) {
return urlCreated;
}

async function sendWhatsappMessage(message, contacts, timeBefore, timeAfter) {
async function sendWhatsappMessage(message, contacts, timeBefore, timeAfter, isSendingImage, imagePath) {
const locateChromePath = await locateChrome()

const browser = await puppeteer.launch({
executablePath: locateChromePath,
headless: false,
defaultViewport: {
width: 1024,
height: 768
}
headless: false
});
const page = await browser.newPage();

Expand All @@ -35,6 +32,17 @@ async function sendWhatsappMessage(message, contacts, timeBefore, timeAfter) {
await page.waitForSelector('#side > header');
await page.waitForTimeout((Math.random() * (4 - 2) + 2) * timeBefore);
await page.keyboard.press('Enter');

if(isSendingImage) {
await page.waitForTimeout((Math.random() * (4 - 2) + 2) * timeAfter);
clipboard.writeImage(nativeImage.createFromPath(imagePath));
await page.keyboard.down('Control')
await page.keyboard.press('V')
await page.keyboard.up('Control')
await page.waitForTimeout((Math.random() * (4 - 2) + 2) * timeBefore);
await page.keyboard.press('Enter');
}

await page.waitForTimeout((Math.random() * (4 - 2) + 2) * timeAfter);
} catch (err) {
console.log(err);
Expand Down
4 changes: 2 additions & 2 deletions build/js/app.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const notificationSender = require('./api/notification');
const openDialogReadFile = require('./api/readFileApi');
const openDialogReadImage = require('./api/readImageApi');
const sendWhatsappMessage = require('./api/senderApi');
const imageCopyToClipboard = require('./api/imageSendApi');

function createWindow() {
const win = new BrowserWindow({
Expand Down Expand Up @@ -32,6 +33,10 @@ ipcMain.on('notify', (event, message) => {
notificationSender(message);
});

ipcMain.on('imageSend', (event, path) => {
imageCopyToClipboard(path)
});

ipcMain.on('file', (event) => {
const result = openDialogReadFile();
event.returnValue = result;
Expand All @@ -42,8 +47,8 @@ ipcMain.on('image', (event) => {
event.returnValue = result;
});

ipcMain.on('send', (event, message, contacts, timeBefore, timeAfter ) => {
sendWhatsappMessage(message, contacts, timeBefore, timeAfter)
ipcMain.on('send', (event, message, contacts, timeBefore, timeAfter, isSendingImage, imagePath ) => {
sendWhatsappMessage(message, contacts, timeBefore, timeAfter, isSendingImage, imagePath)
});

app.whenReady().then(createWindow);
8 changes: 4 additions & 4 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ contextBridge.exposeInMainWorld('electron', {
}
},
senderApi: {
sendWhatsappMessage: (message, contacts, timeBefore, timeAfter) => {
ipcRenderer.send('send', message, contacts, timeBefore, timeAfter);
sendWhatsappMessage: (message, contacts, timeBefore, timeAfter, isSendingImage, imagePath) => {
ipcRenderer.send('send', message, contacts, timeBefore, timeAfter, isSendingImage, imagePath);
}
},
imageApi: {
sendImage: () => {
return;
sendImage: (path) => {
ipcRenderer.send('imageSend', path)
}
}
});
4 changes: 3 additions & 1 deletion src/contexts/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export function AppContextProvider({ children }) {
messageSaved,
listJSON,
timeBefore,
timeAfter
timeAfter,
isSendingImage,
imagePath
);
}
}
Expand Down

0 comments on commit 9b1e756

Please sign in to comment.