Skip to content

Conversation

@aslushnikov
Copy link
Contributor

This patch introduces Page.queryObjects and
ExecutionContext.queryObjects methods to query JavaScript heap
for objects with a certain prototype.

Fixes #304.

This patch introduces Page.queryObjects and
ExecutionContext.queryObjects methods to query JavaScript heap
for objects with a certain prototype.

Fixes puppeteer#304.
await page.evaluate(() => window.map = new Map());
const prototypeHandle = await page.evaluateHandle(() => Map.prototype);
const objectsHandle = await page.queryObjects(prototypeHandle);
const count = await page.evaluate(objects => objects.length, objectsHandle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make me feel safer, can we actually check that the object here is the map and is usable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@JoelEinbinder
Copy link
Contributor

This is really exciting. It feels a bit clumsy to use, but maybe that is ok. I was hoping for something like page.queryObjects(string) -> Array<JSHandle>, but I can easily write that helper myself.

@aslushnikov
Copy link
Contributor Author

aslushnikov commented Oct 11, 2017

This is really exciting. It feels a bit clumsy to use, but maybe that is ok. I was hoping for something like page.queryObjects(string) -> Array, but I can easily write that helper myself.

Disposing an array of JSHandles is not very nice and talkative from protocol's point of view:

await Promise.all(handles.map(handle => handle.dispose()));

Most of the time you'll be satisfied with inspecting the objects with page.evaluate, thus this approach:

const objectPrototype = await page.evaluateHandle(() => Object.prototype);
const objectsHandle = await page.queryObjects(objectPrototype);
const count = await page.evaluate(objects => objects.length, objectsHandle);

Copy link
Contributor

@alexkozy alexkozy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@aslushnikov aslushnikov merged commit 23c0ba0 into puppeteer:master Oct 11, 2017
ithinkihaveacat pushed a commit to ithinkihaveacat/puppeteer that referenced this pull request Oct 31, 2017
This patch introduces `Page.queryObjects` and
`ExecutionContext.queryObjects` methods to query JavaScript heap
for objects with a certain prototype.

Fixes puppeteer#304.
@aslushnikov aslushnikov deleted the query-objects branch January 25, 2018 21:53
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 this pull request may close these issues.

3 participants