I'm writing a simple Chrome extension that tries to spoof your current user agent. I just found out about this question and implemented it in my code.
The following is a content script that tries to replace the current user agent string, set to run at document_start
:
let useragent, actualCode, timer = null;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.useragent) {
sendResponse({error: false});
useragent = request.useragent;
}
else sendResponse({error: true});
});
var receivedUA = function() {
if (useragent != null) {
window.clearInterval(timer);
actualCode = function(userAgent) {
console.log('im being called');
'use strict';
let navigator = window.navigator;
let modifiedNavigator;
if ('userAgent' in Navigator.prototype) {
modifiedNavigator = Navigator.prototype;
} else {
modifiedNavigator = Object.create(navigator);
Object.defineProperty(window, 'navigator', {
value: modifiedNavigator,
configurable: false,
enumerable: false,
writable: false
});
Object.defineProperty(navigator, 'navigator', {
value: modifiedNavigator,
configurable: false,
enumerable: false,
writable: false
});
}
Object.defineProperties(modifiedNavigator, {
userAgent: {
value: userAgent.useragent,
configurable: false,
enumerable: true,
writable: false
},
appVersion: {
value: userAgent.appversion,
configurable: false,
enumerable: true,
writable: false
},
platform: {
value: userAgent.platform,
configurable: false,
enumerable: true,
writable: false
}
});
console.dir(modifiedNavigator);
};
let evt = new Event('reset');
document.documentElement.addEventListener('reset', actualCode(useragent));
document.documentElement.dispatchEvent(evt);
document.documentElement.removeEventListener('reset', actualCode(useragent));
}
else {
window.clearTimeout(timer);
timer = window.setTimeout(receivedUA, 100);
}
}
receivedUA();
However, I'm testing on whatsmyua.com and the code is called when the document starts to load, but after it has loaded, it displays my true user agent, and typing window.navigator.userAgent
on the console returns the same. What am I missing here?