We are writing a chrome extension that returns a random battery level when the battery level is checked by a site running client-side code for fingerprinting reasons. Sample code that can be used by a site can be seen below.
navigator.getBattery().then(function(battery) {
console.log(battery.level);
});
We are unable to find documentation regarding how the navigator.getBattery()
method can be overwritten to accomplish the goal. The incomplete content-script.js can be seen below.
var navigatorBatteryPrivacy = '(' + function() {
'use strict';
var navigator = window.navigator;
var modifiedNavigator;
if (Navigator.prototype) {
modifiedNavigator = Navigator.prototype;
} else {
modifiedNavigator = Object.create(navigator);
Object.defineProperty(window, 'navigator', {
value: modifiedNavigator,
configurable: false,
enumerable: false,
writable: false
});
}
modifiedNavigator.getBattery = function modifiedGetBattery() {
return Promise.resolve(new BatteryManager());
};
} + ')();';
var s = document.createElement('script');
s.textContent = navigatorBatteryPrivacy;
document.documentElement.appendChild(s);
s.remove();
We appreciate the cooperation of the community members.
document_start
. – Muskellunge