0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CDP経由でChromeのタブを切り替える

Posted at

以下ソース

signage.rb
require 'chrome_remote'

# Chromeリモートデバッグプロトコルに接続
client = ChromeRemote.client

# 開かれているタブを取得し、ページタイプのタブのみをフィルタリング
targets = client.send_cmd('Target.getTargets')['targetInfos']
page_targets = targets.select { |target| target['type'] == 'page' }

# タブを順に切り替える
index = page_targets.size - 1
loop do
  if index < 0
    index = page_targets.size - 1 # 最初のタブに戻る
  end

  # タブをアクティブにする
  target_id = page_targets[index]['targetId']
  client.send_cmd('Target.activateTarget', targetId: target_id)

  # 現在のタブのURLを出力
  url = page_targets[index]['url']
  puts "Switched to tab: #{url}"

  index -= 1
  sleep 1 # 1秒ごとに切り替え
end

用途

デジタルサイネージ的にChromeを使いたいときに一定時間で切り替える。
chrome_remote経由なので自由に操作はできる。リロードしてみたり、スクロールしてみたり。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?