Skip to content

Commit

Permalink
implement reply via notification on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
JoniVR committed Jan 15, 2019
1 parent e0ffe16 commit 97f7e3d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
28 changes: 28 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,34 @@ function showNotification({id, title, body, icon, silent}) {
});
}

function typeReply(message) {
const event = document.createEvent('TextEvent');
event.initTextEvent('textInput', true, true, window, message, 0, 'en-US');
const inputField = document.querySelector('[contenteditable="true"]');
if (inputField) {
inputField.focus();
return inputField.dispatchEvent(event);
}
}

async function sendReply() {
(await elementReady('._30yy._38lh._39bl')).click();
return true;
}

ipc.on('notification-callback', (event, data) => {
window.postMessage({type: 'notification-callback', data}, '*');
});

ipc.on('notification-reply', (event, data) => {
const previousConversation = getIndex();
window.postMessage({type: 'notification-callback', data}, '*');
// Wait for Messenger to go to correct message and then start typing and sending
setTimeout(async function() {
await typeReply(data.reply);
await sendReply();
if (previousConversation) {
selectConversation(previousConversation);
}
}, 50);
});
8 changes: 7 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isDev = require('electron-is-dev');
const appMenu = require('./menu');
const config = require('./config');
const tray = require('./tray');
const {sendAction} = require('./util');
const {sendAction, sendBackgroundAction} = require('./util');

require('./touch-bar'); // eslint-disable-line import/no-unassigned-import

Expand Down Expand Up @@ -411,6 +411,7 @@ ipcMain.on('notification', (event, {id, title, body, icon, silent}) => {
const notification = new Notification({
title,
body,
hasReply: true,
icon: nativeImage.createFromDataURL(icon),
silent
});
Expand All @@ -420,6 +421,11 @@ ipcMain.on('notification', (event, {id, title, body, icon, silent}) => {
sendAction('notification-callback', {callbackName: 'onclick', id});
});

notification.on('reply', (event, reply) => {
// We use onclick event used by messenger to go to the right convo
sendBackgroundAction('notification-reply', {callbackName: 'onclick', id, reply});
});

notification.on('close', () => {
sendAction('notification-callback', {callbackName: 'onclose', id});
});
Expand Down
5 changes: 5 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ function sendAction(action, ...args) {
win.webContents.send(action, ...args);
}

function sendBackgroundAction(action, ...args) {
getWindow().webContents.send(action, ...args);
}

exports.getWindow = getWindow;
exports.sendAction = sendAction;
exports.sendBackgroundAction = sendBackgroundAction;

0 comments on commit 97f7e3d

Please sign in to comment.