# Attaching to existing tabs

    library(chromote)
    r <- Chromote$new()

When you use `ChromoteSession$new()` or `b$new_session()`, you’re typically connecting to an existing browser, but creating a new tab to attach to. It’s also possible to attach to an existing browser and and existing tab. In Chrome debugging terminology a tab is called a “Target”, and there is a command to retrieve the list of current Targets:

    r$Target$getTargets()

Every target has a unique identifier string associated with it called the `targetId`; `"9DAE349A3A533718ED9E17441BA5159B"` is an example of one.

Here we define a function that retrieves the ID of the first Target (tab) from a Chromote object:

    first_id <- function(r) {
      ts <- r$Target$getTargets()$targetInfos
      stopifnot(length(ts) > 0)
      r$Target$getTargets()$targetInfos[[1]]$targetId
    }

The following code shows an alert box in the first tab, whatever it is:

    rc <- ChromeRemote$new(host = "localhost", port = 9222)
    r <- Chromote$new(browser = rc)
    tid <- first_id(r)
    b <- r$new_session(targetId = tid)
    b$Runtime$evaluate('alert("this is the first tab")')