Add explanatory comments across codebase
This commit is contained in:
@@ -1,28 +1,44 @@
|
||||
// MAIN world, document_start: navigator.* reports Edge instead of Opera.
|
||||
(() => {
|
||||
// Original UA string from the page (Opera).
|
||||
const ua = navigator.userAgent;
|
||||
// Only patch Opera; other browsers are left as-is.
|
||||
if (!/OPR\//.test(ua)) return;
|
||||
// Chrome major version, same logic as background.js.
|
||||
const v = (ua.match(/Chrome\/(\d+)/) || [])[1] || "130";
|
||||
// Same UA rewrite as background.js: drop OPR/, add Edg/.
|
||||
const edgeUA = ua.replace(/ OPR\/[\d.]+/, "") + ` Edg/${v}.0.0.0`;
|
||||
// Replace any Opera brand entry with Microsoft Edge, keeping its version.
|
||||
const fix = (b) => b.map((x) => (/opera/i.test(x.brand) ? { brand: "Microsoft Edge", version: x.version } : x));
|
||||
// Getter-only property definer; configurable so it can be redefined.
|
||||
const def = (o, k, g) => Object.defineProperty(o, k, { get: g, configurable: true });
|
||||
// Override on Navigator.prototype so page scripts reading navigator.userAgent see Edge.
|
||||
def(Navigator.prototype, "userAgent", () => edgeUA);
|
||||
// appVersion is the UA without the leading "Mozilla/".
|
||||
def(Navigator.prototype, "appVersion", () => edgeUA.replace(/^Mozilla\//, ""));
|
||||
// userAgentData (Client Hints) exists only in Chromium-based browsers.
|
||||
const d = navigator.userAgentData;
|
||||
// Older builds without UA-CH: nothing more to patch.
|
||||
if (!d) return;
|
||||
// Brand list with Opera replaced by Edge.
|
||||
const brands = fix(d.brands);
|
||||
// Keep the original function bound to the real object.
|
||||
const hev = d.getHighEntropyValues.bind(d);
|
||||
// Replacement userAgentData object exposing the same fields as the original.
|
||||
const patched = {
|
||||
brands,
|
||||
mobile: d.mobile,
|
||||
platform: d.platform,
|
||||
// High-entropy values also carry brands and fullVersionList: patch them too.
|
||||
getHighEntropyValues: async (h) => {
|
||||
const r = await hev(h);
|
||||
if (r.brands) r.brands = fix(r.brands);
|
||||
if (r.fullVersionList) r.fullVersionList = fix(r.fullVersionList);
|
||||
return r;
|
||||
},
|
||||
// toJSON is used when the object is serialised; must match the patched fields.
|
||||
toJSON: () => ({ brands, mobile: d.mobile, platform: d.platform }),
|
||||
};
|
||||
// Installed on the prototype, same as userAgent above.
|
||||
def(Navigator.prototype, "userAgentData", () => patched);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user