Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • H headless-chrome-crawler
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 29
    • Issues 29
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 4
    • Merge requests 4
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • yujiosaka
  • headless-chrome-crawler
  • Merge requests
  • !95

Temporary fix for Issue : Trigger function on Page's requestfinished event #93

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/sebastienfi/patch-1 into master Feb 04, 2018
  • Overview 2
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: sebastienfi

Quick PR to expose Page's requestfinished event.

Temporary fix for https://github.com/yujiosaka/headless-chrome-crawler/issues/93 @yujiosaka your solution expose the whole page is way better.

This scenario allow to intercept the request and do something with the response without having to download the ressource a second time, which is, the official way of doing this.

const HCCrawler = require('headless-chrome-crawler')
var fs = require('fs')

HCCrawler.launch({ 
  // Function to be evaluated in browsers
  evaluatePage: (() => ({
    title: $('title').text()
  })),
  // Function to be called with evaluated results from browsers
  onSuccess: (result => {
    console.log(result)
  }),  
})
  .then(crawler => {

    crawler.on('pagerequestfinished', async request => {
        const url = request.url()
        // Matches images.
        const match = /.*\.(jpg|png|gif)$/.exec(url)
        if (match && match.length === 2) {
          const split = url.split('/')
          const filename = split[split.length - 1]
          const response = request.response()
          const buffer = await response.buffer()
          fs.writeFileSync(`./tmp/${filename}`, buffer, 'base64')
        }
    })

    crawler.queue({
      maxDepth: 1,
      skipDuplicates: true,
      url: `http://www.example.com`
    })

    crawler.onIdle() // Resolved when no queue is left
      .then(() => crawler.close()) // Close the crawler
  })
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/sebastienfi/patch-1