0

I would like to click on a specific element of a custom HTML widget at regular intervals.

I have a minimal working example below. The generated widget is from Airbnb. I would like to click the "Next" button every few seconds to scroll through the images. The button's class is "_1rftspj9" (the only button in the app with such a class), which is what I'm trying to use in the custom js script to select it. I'm a javascript novice, so forgive me if I'm missing something obvious.

library(shiny)

ui <- fluidPage(tags$script("
    var but = document.querySelector(\"[class='_1rftspj9']\");
    setInterval(function () {but.click();},3000);"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      shiny::htmlOutput(outputId = "airbnbListing")
    )
  )
)

server <- function(input, output) {
  output$airbnbListing <- renderUI({
    HTML(
      '<div class="airbnb-embed-frame" data-id="48429200" data-view="home" style="width:1800px;height:1200px;margin:auto"><a href="https://www.airbnb.com/rooms/48429200?check_in=2022-11-28&amp;check_out=2022-12-05&amp;guests=1&amp;adults=1&amp;s=66&amp;source=embed_widget">View On Airbnb</a><a href="https://www.airbnb.com/rooms/48429200?check_in=2022-11-28&amp;check_out=2022-12-05&amp;guests=1&amp;adults=1&amp;s=66&amp;source=embed_widget" rel="nofollow">Beautifully Decorated, Comfortable, Beachfront 3 Bedroom House on Gulf Beach</a><script async="" src="https://www.airbnb.com/embeddable/airbnb_jssdk"></script></div>'
    )
  })
}

shinyApp(ui = ui, server = server)
Cyrille
  • 3,427
  • 1
  • 30
  • 32
Joe Robert
  • 84
  • 3
  • Hi Joe, welcome! I think you should test/work on a simple example with just HTML/JS before trying to get this working in Shiny. This will be much simpler for others to help. – Cyrille Jul 07 '22 at 06:37
  • 1
    Have a look at `invalidateLater` and create a reactive that triggers regularly – Clemsang Jul 07 '22 at 06:48
  • Thank you for your feedback. I did have a version of this with invalidateLater previously that wasn't working. My main question relates to how to tell JS to find an element and click on it. @Cyrille what environment do you recommend using to get a simple HTML/JS example? I'm coming from knowing how to develop in Shiny specifically and knowing little about proper HTML/JS. – Joe Robert Jul 07 '22 at 14:24

1 Answers1

0

As you said your "main question relates to how to tell JS to find an element and click on it". You should then remove all mention of Shiny in your question and provide a minimum working example with just that HTML and JS if you can.

However, your question is actually how to do the above, but within an iframe. This is trickier and a different problem as you're trying to run JS code across two websites in a way. Although they appear as one.

The result of this is that it's not possible to have code on your page/domain select elements from another website (in this case the AirBnB site)

See the question and its first comment for more information.

Cyrille
  • 3,427
  • 1
  • 30
  • 32