I have the following node js file calling evaluateOnNewDocument().
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
puppeteer.use(StealthPlugin())
const fs = require('fs')
const preldpath = process.argv[2];
const url = process.argv[3];
const pdfpath = process.argv[4];
(async () => {
const browser = await puppeteer.launch({headless: 'new'});
const page = await browser.newPage();
const preloadFile = fs.readFileSync(preldpath, 'utf8');
await page.evaluateOnNewDocument(preloadFile);
await page.goto(url, {waitUntil: 'networkidle2'});
await page.pdf({path: pdfpath});
await browser.close();
})();
The preloadfile looks like the following.
Object.defineProperty(navigator, "languages", {
get: function() {
return ["fr-ca", "fr"];
};
});
I use the nodejs script with the preload javascript file to open the following html file, the languages still remain the default and have not been changed to “fr-ca fr”. How to preload a javascript file to make navigator.languages
different from the default?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Navigator Languages</title>
</head>
<body>
<h1>Your Preferred Languages:</h1>
<p id="language-output"></p>
<script>
const languageOutput = document.getElementById('language-output');
languageOutput.textContent = navigator.languages.join(', ');
</script>
</body>
</html>