Home > Enterprise >  How to specify browser language in Puppeteer from predefined variable
How to specify browser language in Puppeteer from predefined variable

Time:10-06

I need to set navigator.languages from predefined variable. something like this:

let predefinedLanguage=["bn-BD","bn"]
            await page.evaluateOnNewDocument(() => {
                Object.defineProperty(navigator, "languages", {
                    get: function() {
                        return predefinedLanguage;
                    }
                });
            });

But with code like this i always end upt getting this for navigator.languages

{
     "languages": {
          "error": true,
          "message": "ReferenceError: predefinedLanguage is not defined"
     }
}

I understand that i cant access predefinedLanguage variable inside page.evaluateOnNewDocument but I simply cant find the solution. Please help!

CodePudding user response:

According to the docs, you just need to use argument to transfer the variable value:

let predefinedLanguage=["bn-BD","bn"];
await page.evaluateOnNewDocument((language) => {
  Object.defineProperty(navigator, "languages", {
    get: function() {
      return language;
    }
  });
}, predefinedLanguage);
  • Related