Giter Club home page Giter Club logo

Comments (6)

cderv avatar cderv commented on May 20, 2024 1

Idea: with event emitter API, it would be possible to register some callback for event known as error, like Network.loadingFailed. This way, if this message is received from chrome, we know that something went wrong and we could throw an error or a warning.

from crrri.

cderv avatar cderv commented on May 20, 2024

After 1 and 2, we have something that look like this:

  1. Create the connexion to the page directly. CDPSession class will create a websocket connection that will listen to chrome message and
    • emits an error events in case of a message received with an error
    • emits the name of the command sent when it received a message that correspond to the good reception of a command (a message with no error and an id)
    • emits the name of the method received for a message that contains a method.
      These are the three types of message that we can receive from chrome.
  2. Defines every action in chaining using events, based on the previous emission explained above.
  3. send the first command to start the all flow.
devtools::load_all()

work_dir <- chr_new_data_dir()
chrome <- chr_launch(work_dir = work_dir, headless = TRUE)
ws_endpoint <- chr_get_ws_addr(debug_port = 9222, type = 'page')

# connexion to the session using page endpoint
page_session <- CDPSession$new(ws_endpoint)

page_session$once("Runtime.executionContextCreated", function(...) cat("First command passed!"))

page_session$once("Runtime.enable",
                ~ page_session$sendCommand('Page.enable'))
page_session$once("Page.enable",
                ~ page_session$sendCommand('Runtime.addBinding', params = list(name = "pagedownListener")))
page_session$once("Runtime.addBinding",
                ~ page_session$sendCommand('Page.navigate',
                                         params = list(url = "file:///C:/Users/chris/Documents/test.nb.html")))
page_session$once('Page.domContentEventFired',
                ~ page_session$sendCommand('Runtime.evaluate',
                                           params = list(expression = "!!window.PagedPolyfill")
                ))
page_session$once("Runtime.evaluate",
                function(data) if (!isTRUE(data$result$result$value)) {
                    page_session$sendCommand("Page.printToPDF",
                                    params = list(printBackground = TRUE, preferCSSPageSize = TRUE))
})
page_session$once('Runtime.bindingCalled',
                ~ page_session$sendCommand("Page.printToPDF",
                                    params = list(printBackground = TRUE, preferCSSPageSize = TRUE)
                ))
page_session$once("Page.printToPDF",
               function(data) writeBin(jsonlite::base64_dec(data$result$data), "test.pdf"))

# will launch all the registered events in sequence
page_session$sendCommand('Runtime.enable')

from crrri.

cderv avatar cderv commented on May 20, 2024

Following a discussion on slack, it seems possible to offer several working APIs if we organize things correctly by stage:

  • eventEmitter
  • callbacks
  • promises (modifié)

This seems like a very good idea.

from crrri.

cderv avatar cderv commented on May 20, 2024

This has been going well with #38 and #35. This issue needs to be split in several ideas that we still want to implement and be closed.

from crrri.

cderv avatar cderv commented on May 20, 2024

This is now handle in #40 and will be closed by the merge I think. Are we ok on this ?

from crrri.

RLesur avatar RLesur commented on May 20, 2024

Sounds good to me

from crrri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.