search-engine-google icon indicating copy to clipboard operation
search-engine-google copied to clipboard

Allow to run custom scripts on the dom

Open gsouf opened this issue 8 years ago • 5 comments

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

gsouf avatar Apr 24 '16 10:04 gsouf

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();

gsouf avatar Sep 23 '16 07:09 gsouf

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.

LunarDevelopment avatar Jul 12 '18 08:07 LunarDevelopment

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 avatar Jun 20 '19 19:06 jcatrysse

@jcatrysse no this is not done yet and there are no dates planed on providing this, don't expect to see it for now

gsouf avatar Jun 20 '19 19:06 gsouf

ok, thank you.

jcatrysse avatar Jun 20 '19 19:06 jcatrysse