Files
teams-whisper/app/ext/spoof.js
T

45 lines
2.4 KiB
JavaScript

// MAIN world, document_start: navigator.* reports Edge instead of Opera.
(() => {
// Original UA string from the page (Opera).
const originalUA = navigator.userAgent;
// Only patch Opera; other browsers are left as-is.
if (!/OPR\//.test(originalUA)) return;
// Chrome major version, same logic as background.js.
const chromeMajor = (originalUA.match(/Chrome\/(\d+)/) || [])[1] || "130";
// Same UA rewrite as background.js: drop OPR/, add Edg/.
const edgeUA = originalUA.replace(/ OPR\/[\d.]+/, "") + ` Edg/${chromeMajor}.0.0.0`;
// Replace any Opera brand entry with Microsoft Edge, keeping its version.
const fixBrands = brandList => brandList.map(entry => (/opera/i.test(entry.brand) ? { brand: "Microsoft Edge", version: entry.version } : entry));
// Getter-only property definer; configurable so it can be redefined.
const defineGetter = (object, key, getter) => Object.defineProperty(object, key, { get: getter, configurable: true });
// Override on Navigator.prototype so page scripts reading navigator.userAgent see Edge.
defineGetter(Navigator.prototype, "userAgent", () => edgeUA);
// appVersion is the UA without the leading "Mozilla/".
defineGetter(Navigator.prototype, "appVersion", () => edgeUA.replace(/^Mozilla\//, ""));
// userAgentData (Client Hints) exists only in Chromium-based browsers.
const uaData = navigator.userAgentData;
// Older builds without UA-CH: nothing more to patch.
if (!uaData) return;
// Brand list with Opera replaced by Edge.
const brands = fixBrands(uaData.brands);
// Keep the original function bound to the real object.
const originalHighEntropy = uaData.getHighEntropyValues.bind(uaData);
// Replacement userAgentData object exposing the same fields as the original.
const patched = {
brands,
mobile: uaData.mobile,
platform: uaData.platform,
// High-entropy values also carry brands and fullVersionList: patch them too.
getHighEntropyValues: async hints => {
const result = await originalHighEntropy(hints);
if (result.brands) result.brands = fixBrands(result.brands);
if (result.fullVersionList) result.fullVersionList = fixBrands(result.fullVersionList);
return result;
},
// toJSON is used when the object is serialised; must match the patched fields.
toJSON: () => ({ brands, mobile: uaData.mobile, platform: uaData.platform }),
};
// Installed on the prototype, same as userAgent above.
defineGetter(Navigator.prototype, "userAgentData", () => patched);
})();