question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Can I redefine the `Page.loadEventFired` event after it's been fired?

See original GitHub issue

I want to submit a login form, then redefine Page.loadEventFired again and do something else when the new page loads. Something like this:

  Page.loadEventFired(() => {
    Promise.resolve()
    .then(function() {
      return Runtime.evaluate(/* expression here will submit login form */)
    })
    .then(function (response) {
      // probably not necessary once things are working
      console.log('response:', response);
    })
    .then(/* redefine Page.loadEventFired here */)
    .then(function() {
      return Page.navigate({
        url: 'second link after login',
      });
    })

// I'm not sure where we should finish logic so we can handle a load event again but
// something happens in between here and I take a screenshot after:

... Page.loadEventFired...
    .then(Page.captureScreenshot)
    .then(screenshot)
    .then(function() {
      client.close();
    });
  });

Looked around for an example and couldn’t find one.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
cyrus-andcommented, Apr 20, 2017

Here we go, since v0.20.0 you can also write:

const CDP = require('chrome-remote-interface');

CDP(async (client) => {
    const {Network, Page} = client;

    try {
        await Page.enable();

        console.log('stage 0');
        await Page.navigate({url: 'https://example.com'});
        await Page.loadEventFired();

        console.log('stage 1');
        await Page.navigate({url: 'https://example.com/1'});
        await Page.loadEventFired();

        console.log('stage 2');
        await Page.navigate({url: 'https://example.com/2'});
        await Page.loadEventFired();

        console.log('stage 3');
        await Page.navigate({url: 'https://example.com/3'});
        await Page.loadEventFired();

        console.log('done');
    } catch (err) {
        console.error(err);
    }

    client.close();
}).on('error', (err) => {
    console.error(err);
});

Any feedback is welcome.

1reaction
jmrnilssoncommented, Jun 3, 2017

I know this is closed and all but I found it plenty useful. After trying some of the approaches mentioned here, I went with using a function hoisted as a state variable. In doing so I can await multiple page loads in a single function by resolving a Promise by the page load.

const CDP = require('chrome-remote-interface');
let pageLoad = clickSomeActionThenSubmitForm;

function clickSomeActionThenSubmitForm(Runtime) {
    const clickSomeAction = 'document.querySelector(\'a[href="#someAction"]\').click()';
    const setSomeInfo = 'document.querySelector("input#someinfo").value = "info"';
    const submit = 'document.querySelector(\'form[action*="/smilesAndJoy"] button\').click()';

    return Runtime
        .evaluate({ expression: clickSomeAction })
        .then(() => { return new Promise(resolve => { pageLoad = resolve; }) })
        .then(() => { return Runtime.evaluate({expression: setSomeInfo })})
        .then(() => { return Runtime.evaluate({expression: submit })})
        .then(() => { return new Promise(resolve => { pageLoad = resolve; }) });
}
 ...
Page.loadEventFired(() => { pageLoad(Runtime) }); 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Target domain events not firing? - Stack Overflow
A fun thing about the Target Domain is that it does not have an enable method. However, it does have setDiscoverTargets method, ...
Read more >
Page.loadEventFired can't receive sometimes - Google Groups
Hi,. I met a problem that loadEventFired event can not received when I create multiple chrome tabs by chrome-remote-interface, almost 100 tab ...
Read more >
Introduction to Highcharts events
It is fired after the load phase and after a few other events. Basically, whenever anything is changed in the chart – this...
Read more >
How to find out which JavaScript events fired? - Tutorialspoint
Using Developer Tools · Go to the sources tab. · Click on the Event Listener Breakpoints, on the right side. · Then perform...
Read more >
Session traces: Explore a webpage's life cycle
After you select the session URL or session trace ID, you can use the detailed ... The time from when the request began...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found