berstend / puppeteer-extra

💯 Teach puppeteer new tricks through plugins.
https://extra.community
MIT License
6.41k stars 739 forks source link

[Feature] navigator.getBattery substitution #796

Open starkdm opened 1 year ago

starkdm commented 1 year ago

How can I change the values of BatteryManager properties? The problem is that the navigator.getBattery() method is asynchronous and returns Promise. When using ReplaceWithProxy method I can't pass async handler.

    utils.replaceWithProxy(Object.getPrototypeOf(navigator), 'getBattery', {
        apply: (target, ctx, args) => {
            var promise = utils.cache.Reflect.apply(...arguments);
            /*
            const batteryMgr = await promise;
            utils.replaceProperty(batteryMgr, 'charging', { get: () => true });
            utils.replaceProperty(batteryMgr, 'chargingTime', { get: () => 0 });
            utils.replaceProperty(batteryMgr, 'dischargingTime', { get: () => Infinity });
            utils.replaceProperty(batteryMgr, 'level', { get: () => 1 });
            */
            return promise; //Promise.resolve(batteryMgr);
        }
    });

The purpose is to hide battery info to avoid being tracked by websites.