search-engine-google
search-engine-google copied to clipboard
Allow to run custom scripts on the dom
We should provide a common interface to run scripts on the dom.
That would allow to make some advanced analyse of the page as for instance:
- graphical position of things in the page
- Simulation of clicks
That will be possible with the incoming implementation of browsers (phantomjs will be the first browser)
Once browser are implemented we will have the notion of interactive queries.
Here is a draft of what the interactive queries could be:
<?php
// Start an interactive query
$interactiveQuery = $googleClient->interactiveQuery($query);
// Get screenshot as binary data
$screenshotBinary = $interactiveQuery->screenshot(); // binary media
// Save screenshot in a file
$screenshotFile = $interactiveQuery->screenshot('/tmp/file.png'); // file media
// Make screenshot of a part of the page
$screenshotPartial = $interactiveQuery->boxScreenshot($top, $left, $height, $width);
// Execute script on the page
$interactiveQuery->pageExecuteScript('return document.body.scrollHeight;');
foreach($interactiveQuery->response->getNaturalResults() as $result){
// Can work with result types and data as usually
if($result->is(ResultType::CLASSICAL)){
//...
}
// Execute user defined scripts within a node context
$box = $interactiveQuery->nodeExecuteScript($result->getNode(), 'return node.getBoundingClientRect();');
// Shortcut to get node dimension (same as previous)
$box = $interactiveQuery->nodeDimensions($result->getNode());
// Make screenshot of a node
$interactiveQuery->nodeScreenshot('/tmp/file.png');
}
// Close the process to save resources
$interactiveQuery->close();
// This will thow an error because the process was closed
$interactiveQuery->screenshot();
There is already a PHP library which wraps phantomjs, a function in serp-spider which returns the dom to pass to that other library is sufficient to run scripts.
Has this been implemented? I am looking to simulate the onmousdedown event to simulate the "real user" experience and retrieve the newly created URL. I have the PhantomJS running but I am somewhat clueless...
@jcatrysse no this is not done yet and there are no dates planed on providing this, don't expect to see it for now
ok, thank you.