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

backgroundpage.on('console') not working #4300

Closed
pyrho opened this issue Apr 17, 2019 · 5 comments · Fixed by #4704
Closed

backgroundpage.on('console') not working #4300

pyrho opened this issue Apr 17, 2019 · 5 comments · Fixed by #4704

Comments

@pyrho
Copy link

pyrho commented Apr 17, 2019

Steps to reproduce

  • Puppeteer version: 1.14.0
  • Platform / OS version: macOS 10.14.3
  • Node.js version: 11.6.0

What steps will reproduce the problem?

  1. git clone https://github.com/pyrho/puppeteer-bug.git
  2. yarn
  3. node example.js

The repo for this issue: https://github.com/pyrho/puppeteer-bug

What is the expected result?
Messages (console.log)from the background page are caught by the backgroundPage.on('console') handler.
`bg: PORT CONNECTED" should print in the node console.

What happens instead?
Messages are not caught by the handler.

When manually inspecting the background page, we can see that the message is printed in the console tab:
Screenshot 2019-04-17 at 16 09 00

Thank you for your time and this awesome project :)

@xse
Copy link

xse commented Apr 17, 2019

Hey, maybe it's just happening before your listener being setup ?
If i add like:

await backgroundPage.evaluate(() => console.log('hi'));

anywhere before your listeners in your example.js i don't see anything, but anywhere after i'll see 'bg: hi'
I don't know, since i guess your extension is supposed to run at startup it makes sense to me but maybe i'm wrong.
Have a good day!

@pyrho
Copy link
Author

pyrho commented Apr 18, 2019

Hey @xse thank you for the suggestion as this is a classic mistake.
I've changed the code in my extension to call console.log in a setInterval(1000) (so that listener hook timing shouldn't matter); the log is shown in the background page, but my backgroundPageTarget.page().on('console') handler is still not fired.

Have a good one too!

@xse
Copy link

xse commented Apr 18, 2019

Indeed my bad.
So, excuse my english, i'm pretty bad at explaining things but i'll try since i got those bg messages.
I think the issue is that there is two background pages, i actually got your example working once, but only once so that was strange, then i tried to output all console messages from all targets, it crashed so by filtering with only the targets that seems to be a background page, that works.
If you add this right after the console.log('closing'); line in your example :

    let test = await browser.targets();
    let foo = [];
    for (let i=0, len=test.length; i<len; i++) {
        console.log('target '+i);
        if (test[i]._targetInfo.url.includes('background_')) {
            console.log(test[i]);
            foo[i] = await test[i].page();
            foo[i]._client.on('Runtime.consoleAPICalled', ({args, executionContextId, timestamp, type, stackTrace, context}) => {
                console.log('Runtime.consoleAPICalled');
                console.log(args);
            });
        }
    }

You can see the messages that way.
working

I think you can get rid of most of the code and use .on('console', msg => console.log(bg: ${msg.text()}));

The console.log(test[i]); outputs two background pages, and since i added the listener to both i don't know witch one is the correct one i did not dig enough into that, but you should be able to filter the correct one. Like if you take a look at the target objects, there are easy ways to differentiates both of them like i think one has your extension name inside.
I don't know if having two background pages when you specify --disable-extensions-except is normal behavior.

I don't know if that make sense, i hope it does.
EDIT: typo and maybe more clear.

@pyrho
Copy link
Author

pyrho commented Apr 19, 2019

@xse Thank you so much for your precious help !
It does work now.

So apparently Chrome has a "hidden" extension called CryptoTokenExtension, (it does not show up in chrome://extensions/, even with developper mode enabled) that handles 2FA (see this).

I think that in my original code, I was calling browser.targets() too early (yielding only 2 targets).
Calling it when where you suggested yields 4 target, including my extension.
So when the first call triggered, my extension was not loaded yet.

As you stated, I guess that the --disable-extensions-except flag should also disable CryptoTokenExtension, but I guess that's a Chromium bug (the switch is defined here rather than a puppeteer bug.

Again, thank you so much for your help, I spent all day yesterday faking the chrome.runtime API using page.exposeFunction ¯_(ツ)_/¯; it's way better if it just works out of the box! :)

@pyrho pyrho closed this as completed Apr 19, 2019
aslushnikov added a commit to aslushnikov/puppeteer that referenced this issue Jul 14, 2019
Chrome has a set of component extensions - e.g. CryptoTokenExtension
that helps with 2FA.

These extensions are loaded regardless of the `--disable-extensions`
flag we already pass. To disable these extensions, we need to pass additional
`--disable-component-extensions-with-background-pages` flag.

Fix puppeteer#4300
@aslushnikov
Copy link
Contributor

We should disable the component extensions by default - submitted the #4704 for this.

aslushnikov added a commit that referenced this issue Jul 16, 2019
Chrome has a set of component extensions - e.g. CryptoTokenExtension
that helps with 2FA.

These extensions are loaded regardless of the `--disable-extensions`
flag we already pass. To disable these extensions, we need to pass additional
`--disable-component-extensions-with-background-pages` flag.

Fix #4300
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants