Chrome.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jaeger <JaegerCode@gmail.com>
  5. * Date: 19/1/15
  6. * Use Puppeteer to crawl Javascript dynamically rendered pages.
  7. */
  8. namespace QL\Ext;
  9. use Nesk\Rialto\Data\JsFunction;
  10. use QL\Contracts\PluginContract;
  11. use QL\QueryList;
  12. use Nesk\Puphpeteer\Puppeteer;
  13. use Closure;
  14. class Chrome implements PluginContract
  15. {
  16. public static function install(QueryList $queryList, ...$opt)
  17. {
  18. $name = $opt[0] ?? 'chrome';
  19. $queryList->bind($name,function ($url,$options = []) {
  20. return Chrome::render($this,$url,$options);
  21. });
  22. }
  23. public static function render(QueryList $queryList,$url,$options)
  24. {
  25. $options = self::mergeOptions($options);
  26. $puppeteer = new Puppeteer;
  27. $browser = $puppeteer->launch($options);
  28. $page = $browser->newPage();
  29. if($url instanceof Closure){
  30. $html = $url($page,$browser);
  31. }else{
  32. $page->setRequestInterception(true);
  33. $page->on('request',
  34. JsFunction::createWithParameters(['request'])
  35. ->body("if (['image', 'stylesheet', 'font'].indexOf(request.resourceType()) !== -1) {
  36. request.abort();
  37. } else {
  38. request.continue();
  39. }")
  40. );
  41. $page->goto($url);
  42. $html = $page->content();
  43. $browser->close();
  44. }
  45. $queryList->setHtml($html);
  46. return $queryList;
  47. }
  48. protected static function mergeOptions($option)
  49. {
  50. $defalutOptions = [
  51. 'ignoreHTTPSErrors' => true,
  52. ];
  53. return array_merge($defalutOptions,$option);
  54. }
  55. }