Apply ruff lint and rename cryptic identifiers to readable names

This commit is contained in:
Stefano Zini
2026-11-05 09:09:20 +01:00
parent 19de4654ff
commit ead33657c7
24 changed files with 4459 additions and 1856 deletions
+21 -21
View File
@@ -1,44 +1,44 @@
// MAIN world, document_start: navigator.* reports Edge instead of Opera.
(() => {
// Original UA string from the page (Opera).
const ua = navigator.userAgent;
const originalUA = navigator.userAgent;
// Only patch Opera; other browsers are left as-is.
if (!/OPR\//.test(ua)) return;
if (!/OPR\//.test(originalUA)) return;
// Chrome major version, same logic as background.js.
const v = (ua.match(/Chrome\/(\d+)/) || [])[1] || "130";
const chromeMajor = (originalUA.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`;
const edgeUA = originalUA.replace(/ OPR\/[\d.]+/, "") + ` Edg/${chromeMajor}.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));
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 def = (o, k, g) => Object.defineProperty(o, k, { get: g, configurable: true });
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.
def(Navigator.prototype, "userAgent", () => edgeUA);
defineGetter(Navigator.prototype, "userAgent", () => edgeUA);
// appVersion is the UA without the leading "Mozilla/".
def(Navigator.prototype, "appVersion", () => edgeUA.replace(/^Mozilla\//, ""));
defineGetter(Navigator.prototype, "appVersion", () => edgeUA.replace(/^Mozilla\//, ""));
// userAgentData (Client Hints) exists only in Chromium-based browsers.
const d = navigator.userAgentData;
const uaData = navigator.userAgentData;
// Older builds without UA-CH: nothing more to patch.
if (!d) return;
if (!uaData) return;
// Brand list with Opera replaced by Edge.
const brands = fix(d.brands);
const brands = fixBrands(uaData.brands);
// Keep the original function bound to the real object.
const hev = d.getHighEntropyValues.bind(d);
const originalHighEntropy = uaData.getHighEntropyValues.bind(uaData);
// Replacement userAgentData object exposing the same fields as the original.
const patched = {
brands,
mobile: d.mobile,
platform: d.platform,
mobile: uaData.mobile,
platform: uaData.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;
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: d.mobile, platform: d.platform }),
toJSON: () => ({ brands, mobile: uaData.mobile, platform: uaData.platform }),
};
// Installed on the prototype, same as userAgent above.
def(Navigator.prototype, "userAgentData", () => patched);
defineGetter(Navigator.prototype, "userAgentData", () => patched);
})();