Skip to content

Commit

Permalink
Some bug fixes and changes
Browse files Browse the repository at this point in the history
- Removed waStore since window.Store is already loaded by whatsapp-web.js
- Removed dark mode toggle since whatsapp officially supports it
- Better window.Store detection on preload.js
- Added Ctrl+Q shortcut to exit WALC (issue WAClient#43)
- Immediately retry connecting to whatsapp when internet is online
  • Loading branch information
Zzombiee2361 committed Aug 16, 2020
1 parent 54812b4 commit ff0fd32
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 171 deletions.
44 changes: 17 additions & 27 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var trayIcon;
var aboutWALC;

let isConnected = true;
let firstCall = true;
let preventExit = true;

let pieBrowser;
Expand Down Expand Up @@ -160,18 +159,6 @@ const WhatsAppMenu = [{
await markAllChatsAsRead();
menuItem.enabled = true;
}
}, {
label: 'separator',
type: 'separator'
}, {
label: "Dark Mode",
sublabel: "Toggle Dark Mode",
type: 'checkbox',
checked: settings.get('darkMode.value'),
click: (menuItem, window, e) => {
settings.set('darkMode.value', menuItem.checked);
win.webContents.send("setDarkMode", menuItem.checked);
}
}];

const settingsMenu = [{
Expand Down Expand Up @@ -286,6 +273,7 @@ const windowMenu = [{
label: 'Exit',
sublabel: 'Quit current window of WALC completely',
type: 'normal',
accelerator: 'CmdOrCtrl+Q',
click: () => {
app.isQuiting = true;
app.quit();
Expand Down Expand Up @@ -427,24 +415,26 @@ function loadWA() {
// return
console.log(e);
}

const KEEP_PHONE_CONNECTED_IMG_SELECTOR = '[data-asset-intro-image-light="true"], [data-asset-intro-image-dark="true"]';
await page.waitForSelector(KEEP_PHONE_CONNECTED_IMG_SELECTOR, { timeout: 0 });
botClient = new Client();
await botClient.initialize(page, win);
if (firstCall) {
firstCall = false;
win.webContents.executeJavaScript("Notification.requestPermission(function(p){if(p=='granted'){new Notification('WALC Desktop Notifications', {body:'Desktop Notifications are enabled.', icon:'https://web.whatsapp.com/favicon.ico'});};});");
botClient.on('message', (msg) => {
console.log(msg.body);
});
botClient.on('ready', () => {
customeTitle = `WALC`;
preventTitleChange = false;
win.setTitle(customeTitle);
preventTitleChange = true;
});

}
botClient.on('ready', () => {
// win.webContents.executeJavaScript("Notification.requestPermission(function(p){if(p=='granted'){new Notification('WALC Desktop Notifications', {body:'Desktop Notifications are enabled.', icon:'https://web.whatsapp.com/favicon.ico'});};});");
win.webContents.send('storeOnLoad');

customeTitle = `WALC`;
preventTitleChange = false;
win.setTitle(customeTitle);
preventTitleChange = true;
});

botClient.on('message', (msg) => {
console.log(msg.body);
});

botClient.initialize(page, win);
}).catch((err) => {
console.log(err);
});
Expand Down
14 changes: 3 additions & 11 deletions offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function resetCounter() {
}

function liveCheck() {
clearTimeout(timeout);
retryButton.disabled = true;
retryText.innerHTML = 'Retrying...';
ipcRenderer.send('liveCheck');
// setTimeout(resetCounter, 2000);
}

function count() {
Expand All @@ -37,14 +37,6 @@ function count() {
}

ipcRenderer.on('offline', resetCounter);
retryButton.addEventListener('click', () => {
clearTimeout(timeout);
liveCheck();
});
retryButton.addEventListener('click', liveCheck);
window.addEventListener('online', () => setTimeout(liveCheck, 1000));
count();

if(settings.get('darkMode.value')) {
document.body.classList.add('bg-dark');
} else {
document.body.classList.remove('bg-dark');
}
37 changes: 7 additions & 30 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { remote, ipcRenderer } = require('electron');
const Store = require('electron-store');
const electronSpellchecker = require('electron-spellchecker');
const waStore = require('./waStore');

const settings = new Store({ name: 'settings' });

Expand Down Expand Up @@ -63,7 +62,7 @@ function renderTray() {
canvas.width = logo.naturalWidth;
canvas.height = logo.naturalHeight;

if(window.Store.State.default.state !== 'CONNECTED') {
if(window.Store.AppState.state !== 'CONNECTED') {
ctx.filter = 'grayscale(100%)';
}
ctx.drawImage(logo, 0, 0);
Expand Down Expand Up @@ -92,7 +91,7 @@ function renderTray() {
function appStateChange(event, state) {
if (['OPENING', 'DISCONNECTED', 'TIMEOUT'].includes(state)) {
setTimeout(() => {
if (state === window.Store.State.default.state) {
if (state === window.Store.AppState.state) {
new Notification('WALC disconnected', {
body: "Please check your connection.",
icon: "favicon.ico"
Expand All @@ -106,34 +105,12 @@ function appStateChange(event, state) {
}

function storeOnLoad() {
setTimeout(function() {
if(window.Store || document.getElementById('startup') === null) {
waStore(); // auto load window.Store if undefined

renderTray();
window.Store.Chat.on('change:unreadCount', renderTray);
window.Store.Chat.on('change:muteExpiration', renderTray);
window.Store.State.default.on('change:state', appStateChange);
} else {
storeOnLoad();
}
}, 3000);
}

function setDarkMode(enabled) {
if(enabled) {
document.body.classList.add('dark');
} else {
document.body.classList.remove('dark');
}
}

function applySettings() {
setDarkMode(settings.get('darkMode.value'));
renderTray();
window.Store.Chat.on('change:unreadCount', renderTray);
window.Store.Chat.on('change:muteExpiration', renderTray);
window.Store.AppState.on('change:state', appStateChange);
}

window.addEventListener('load', storeOnLoad);
window.addEventListener('load', applySettings);
document.addEventListener("DOMContentLoaded", setupSpellChecker);
ipcRenderer.on('renderTray', renderTray);
ipcRenderer.on('setDarkMode', (ev, enabled) => setDarkMode(enabled));
ipcRenderer.on('storeOnLoad', storeOnLoad);
103 changes: 0 additions & 103 deletions waStore.js

This file was deleted.

0 comments on commit ff0fd32

Please sign in to comment.