Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page.exposeFunction #96

Open
chaosharmonic opened this issue Oct 8, 2024 · 0 comments
Open

Page.exposeFunction #96

chaosharmonic opened this issue Oct 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@chaosharmonic
Copy link
Contributor

chaosharmonic commented Oct 8, 2024

Puppeter and Playwright both offer a Page.exposeFunction enabling you to set globals to the window object and call them.

Playwright also has a Page.exposeBinding, though I'm less sure what's distinct there, as well as equivalents to both for the larger BrowserContext.

I'm still having trouble getting function definitions passed in manually from outside, but I can confirm that assigning to window will persist across page.evaluate calls:

await page.evaluate(async () => {
    window['hey'] = () => console.log('listen')
})

await page.evaluate(async () => {
    hey() // will print to browser console
})

That said it's partly an issue getting plain and arrow functions to consistently type coerce to and from strings. The below does work:

const hey = () => {
    console.log('listen!')
}

await page.evaluate(async (hey) => {
    window['listen'] = eval(hey)
}, { args: [ hey.toString() ]})

await page.evaluate(async () => {
    listen() // will still print to browser console
})

But, it only works with arrow functions. Using function -- whether on its own or with const -- you have to isolate the code block and its arguments separately, and then pass them into Function. For a simple example without arguments:

function hey() // or const hey = function...
    console.log('listen!')
}

const hello = `{${hey.toString().split('{').slice(1).join('{')}`

await page.evaluate(async (hey) => {
    window['listen'] = Function(hey)
}, { args: [ hello ] })

await page.evaluate(async () => {
    listen()
})

Both eval and Function also don't work by default using Firefox, even on the New Tab page

@lino-levan lino-levan added the enhancement New feature or request label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants