This snippet illustrates how to reconstruct the legacy navigator.userAgent
string value from the modern navigator.userAgentData
values. The legacy
navigator.userAgent
is created only if the current navigator.userAgent
string matches the reduced User-Agent specification outlined in
User-Agent Reduction.
Copy the contents of uach-retrofill.js
to the relevant location in your project.
Import the function from the module:
import { overrideUserAgentUsingClientHints } from './uach-retrofill.js';
The overrideUserAgentUsingClientHints()
function takes an array of hints that will be used to generate the legacy user-agent string format and update the value in navigator.userAgent
.
The various hints used, which you can optionally pass in based on your needs are:
architecture
- the CPU architecturebitness
- the Operating System's underlying CPU architecture bitness (e.g., "32" or "64")model
- the model on AndroidplatformVersion
- the Operating System's versionuaFullVersion
- the browser's full version, in case the significant version (provided by default) is not enoughwow64
- whether or not the browser is running in 32-bit mode on 64-bit Windows
For example, if you need the full uaFullVersion
, you would specify:
overrideUserAgentUsingClientHints(["uaFullVersion"]).then(() => {
// navigator.userAgent will be updated with the generated string
document.getElementById("output").innerHTML = navigator.userAgent;
})
https://googlechromelabs.github.io/uach-retrofill/demo.html
Please see the Code of Conduct and Contributing guides.