mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-21 04:59:14 +00:00
feat: battery percentage changed event (#126)
New "change_battery" event is triggered when the battery percentage changes. Has current battery percentage and charging state as object param. Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
@@ -237,6 +237,12 @@ client.on('group_update', (notification) => {
|
|||||||
console.log('update', notification);
|
console.log('update', notification);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.on('change_battery', (batteryInfo) => {
|
||||||
|
// Battery percentage for attached device has changed
|
||||||
|
const { battery, plugged } = batteryInfo;
|
||||||
|
console.log(`Battery: ${battery}% - Charging? ${plugged}`);
|
||||||
|
});
|
||||||
|
|
||||||
client.on('disconnected', (reason) => {
|
client.on('disconnected', (reason) => {
|
||||||
console.log('Client was logged out', reason);
|
console.log('Client was logged out', reason);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ const { ClientInfo, Message, MessageMedia, Contact, Location, GroupNotification
|
|||||||
* @fires Client#group_update
|
* @fires Client#group_update
|
||||||
* @fires Client#disconnected
|
* @fires Client#disconnected
|
||||||
* @fires Client#change_state
|
* @fires Client#change_state
|
||||||
|
* @fires Client#change_battery
|
||||||
*/
|
*/
|
||||||
class Client extends EventEmitter {
|
class Client extends EventEmitter {
|
||||||
constructor(options = {}) {
|
constructor(options = {}) {
|
||||||
@@ -296,6 +297,20 @@ class Client extends EventEmitter {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await page.exposeFunction('onBatteryStateChangedEvent', (state) => {
|
||||||
|
|
||||||
|
const { battery, plugged } = state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted when the battery percentage for the attached device changes
|
||||||
|
* @event Client#change_battery
|
||||||
|
* @param {object} batteryInfo
|
||||||
|
* @param {number} batteryInfo.battery - The current battery percentage
|
||||||
|
* @param {boolean} batteryInfo.plugged - Indicates if the phone is plugged in (true) or not (false)
|
||||||
|
*/
|
||||||
|
this.emit(Events.BATTERY_CHANGED, { battery, plugged });
|
||||||
|
});
|
||||||
|
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
window.Store.Msg.on('add', (msg) => { if(msg.isNewMsg) window.onAddMessageEvent(msg); });
|
window.Store.Msg.on('add', (msg) => { if(msg.isNewMsg) window.onAddMessageEvent(msg); });
|
||||||
window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(msg); });
|
window.Store.Msg.on('change', (msg) => { window.onChangeMessageEvent(msg); });
|
||||||
@@ -304,6 +319,7 @@ class Client extends EventEmitter {
|
|||||||
window.Store.Msg.on('change:isUnsentMedia', (msg, unsent) => { if(msg.id.fromMe && !unsent) window.onMessageMediaUploadedEvent(msg); });
|
window.Store.Msg.on('change:isUnsentMedia', (msg, unsent) => { if(msg.id.fromMe && !unsent) window.onMessageMediaUploadedEvent(msg); });
|
||||||
window.Store.Msg.on('remove', (msg) => { if(msg.isNewMsg) window.onRemoveMessageEvent(msg); });
|
window.Store.Msg.on('remove', (msg) => { if(msg.isNewMsg) window.onRemoveMessageEvent(msg); });
|
||||||
window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); });
|
window.Store.AppState.on('change:state', (_AppState, state) => { window.onAppStateChangedEvent(state); });
|
||||||
|
window.Store.Conn.on('change:battery', (state) => { window.onBatteryStateChangedEvent(state); });
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ exports.Events = {
|
|||||||
QR_RECEIVED: 'qr',
|
QR_RECEIVED: 'qr',
|
||||||
DISCONNECTED: 'disconnected',
|
DISCONNECTED: 'disconnected',
|
||||||
STATE_CHANGED: 'change_state',
|
STATE_CHANGED: 'change_state',
|
||||||
|
BATTERY_CHANGED: 'change_battery'
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user