LoginSignup
0
0

はじめに

ブックマーク関連のChrome拡張機能開発において
「タブ」の「親グループ」を取得する方法について

必要な権限

tabstabGrooup の権限が必要です。

"permissions": ["tabs", "tabGroups"]

「親グループ(tabGroup)」の取得方法

const parent = chrome.tabGroups.get(tab.groupId)

このときのtabは、groupId プロパティを持つ単一のTabです。

グループ名を取得したいときは

// 先程の続き
parent.title

で取得可能

その他のプロパティはこちら

groupId の取得方法

あいまい検索でtabを取得したい場合

chrome.tabs.query({}, (tabs) => {

  const regex = new RegExp(`.*${query}.*`, 'i');
  
  tabs.forEach(async (tab) => {
    if (tab.title.match(regex) || tab.url.match(regex)) {
      const parent = tab.groupId !== -1 ? await chrome.tabGroups.get(tab.groupId) : '';
    console.log(parent.title + ' / ' + tab.title)
    // 親グループ / タブ名 というログになる
    }
  })
})

シンプルなバージョン

chrome.tabs.query({}, async (tabs) => {

  //なにかしらの方法で tabs から tab を抜き出す
  
  const parent = await chrome.tabGroups.get(tab.groupId)
  console.log(parent.title + ' / ' + tab.title)
  // 親グループ / タブ名 というログになる
})

chrome.tabGroups.get() はプロミスを返すため async / await を使用しています。

関連記事

ブックマークの「親フォルダ」を取得したい時

popupのconsole.logを確認したい時

おわりに

以上、タブとグループについてでした。

次は何を書こうかしら・・・

0
0
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
0