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

Q: What is the recommend way to access a content script's execution context? #4465

Closed
pyrho opened this issue May 23, 2019 · 2 comments
Closed

Comments

@pyrho
Copy link

pyrho commented May 23, 2019

Hey !

I stumbled upon this PR which enables accessing the execution context of content scripts (injected by extensions). It's great, I cherry pick that commit into my fork of master and I'm now able to call myContentScriptExecutionContext.evaluate and access globally exposed
functions from there.
But you decided to revert that (via this PR).

So my question is, is there an official way of doing this as of now?

Thanks again for your hard work on puppeteer.

@pyrho
Copy link
Author

pyrho commented May 27, 2019

If anyone else is wondering how to do this, you can look at this forked branch. I've been using this for my project (direct reference to that branch in my package.json) and it works fine for my needs.

The following snippet allows accessing data from the content script's execution context:

        let contentScriptExecutionHandler = (() => Array.from(page.executionContexts().values()).find(ec => ec.name() === 'MY_EXTENSION_NAME'))();
        const dataFromContentScript = await contentScriptExecutionHandler.evaluate(() => window.my_content_script_variable); // set in the content script

@aslushnikov
Copy link
Contributor

I stumbled upon this PR which enables accessing the execution context of content scripts (injected by extensions). It's great, I cherry pick that commit into my fork of master and I'm now able to call myContentScriptExecutionContext.evaluate and access globally exposed
functions from there.

@pyrho nice archeology! :-)

So my question is, is there an official way of doing this as of now?

Not yet. Extension content scripts will be exposed by #1215, and we track the general DOMWorld management with #2671.

#1215 has good chances to land soon'ish.

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

No branches or pull requests

2 participants