Skip to content

Commit

Permalink
feat: navigator.languages add navigator.language support
Browse files Browse the repository at this point in the history
  • Loading branch information
nswbmw committed Dec 23, 2024
1 parent 39248f1 commit 4cf241c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin')
const withUtils = require('../_utils/withUtils')

/**
* Pass the Languages Test. Allows setting custom languages.
* Pass the Languages Test. Allows setting custom language & languages.
*
* @param {Object} [opts] - Options
* @param {Array<string>} [opts.languages] - The languages to use (default: `['en-US', 'en']`)
* @param {string} [opts.language] - The language to use (default: `'en-US'`)
*/
class Plugin extends PuppeteerExtraPlugin {
constructor(opts = {}) {
Expand All @@ -25,19 +26,34 @@ class Plugin extends PuppeteerExtraPlugin {
}

async onPageCreated(page) {
const languages = this.opts.languages.length
? this.opts.languages
: ['en-US', 'en']
const language = this.opts.language || languages[0]

await withUtils(page).evaluateOnNewDocument(
(utils, { opts }) => {
const languages = opts.languages.length
? opts.languages
: ['en-US', 'en']
(utils, { languages }) => {
utils.replaceGetterWithProxy(
Object.getPrototypeOf(navigator),
'languages',
utils.makeHandler().getterValue(Object.freeze([...languages]))
)
},
{
opts: this.opts
languages
}
)

await withUtils(page).evaluateOnNewDocument(
(utils, { language }) => {
utils.replaceGetterWithProxy(
Object.getPrototypeOf(navigator),
'language',
utils.makeHandler().getterValue(language)
)
},
{
language
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Options (optional, default `{}`)
- `opts.languages` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)>?** The languages to use (default: `['en-US', 'en']`)
- `opts.language` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** The language to use (default: `'en-US'`)

**Extends: PuppeteerExtraPlugin**

Expand Down

0 comments on commit 4cf241c

Please sign in to comment.