Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

how to listen page by on? #33

Closed
yangweijie opened this issue Jan 23, 2019 · 8 comments
Closed

how to listen page by on? #33

yangweijie opened this issue Jan 23, 2019 · 8 comments

Comments

@yangweijie
Copy link

when i simulate to login some website to get cookie, there comes alert dialog. how to write like js code below same with php?

page.on('dialog', async dialog => {
    console.log(dialog.message());
    await dialog.dismiss();
    // await browser.close();
});
@nesk
Copy link
Member

nesk commented Jan 28, 2019

See the documentation about evaluated functions.

@nesk nesk closed this as completed Jan 28, 2019
@tfedor
Copy link

tfedor commented Mar 2, 2019

I checked the documentation but I think I still don't understand how this is supposed to work.

I understand that I have to use JsFunction, I think I know how to create one, but how do I get back the result in PHP?

$function = JsFunction::createWithAsync()
    ->parameters(["response"])
    ->body('return response.status();');

$page->on("response", $function);

how do I get the status in PHP? Is there an example I have missed? If it's not, can it be please created?

@nesk
Copy link
Member

nesk commented Mar 3, 2019

I should document this. You can listen for an event and react to it, however it’s currently impossible to transmit a value to PHP from a JS event.

@kids-return
Copy link

kids-return commented Mar 15, 2019

How to do this, whether to consider adding in the future

@kids-return
Copy link

I tried to do this, but something went wrong.

        $function = JsFunction::create(['request'], "
            fs = require('fs');
            fs.writeFileSync('request.txt', request.url());
        ");
        $page->on('requestfailed', $function);

PHP Fatal error: Uncaught Nesk\Rialto\Exceptions\Node\FatalException: require is not defined in /Users/lixiang/Documents/data/wwwroot/yii3/vendor/nesk/rialto/src/ProcessSupervisor.php:307

@kids-return
Copy link

    page.on('requestfailed', function (request) {
        fs = require('fs');
        fs.writeFileSync('request.txt', request.url());
    });

In nodejs, this works fine

@nesk
Copy link
Member

nesk commented Mar 25, 2019

This is because JS functions in PuPHPeteer are created with the Function constructor, your last example would look like this if written with PuPHPeteer:

page.on('requestfailed', new Function('request', `
    fs = require('fs');
    fs.writeFileSync('request.txt', request.url());
`));

This will fail because the body of the function doesn't have access to the module scope, but only to the global one.

I agree this is an issue and should be fixed, I've created an issue for this: rialto-php/rialto#19

@truong8487
Copy link

Please resolve this issue, Mr Nesk and All!The example above , i till dont understand.
Let show a example about page.on when use with puphpeteer.
Thank u so much!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants