【Chrome拡張機能】現在表示しているタブIDを取得する方法

現在のタブ情報を取得する関数

background.js


async function getCurrentTab() {
	let queryOptions = { active: true, currentWindow: true };
	let [tab] = await chrome.tabs.query(queryOptions);
	return tab;
}

使い方

background.js


chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
	getCurrentTab().then((tab) => {
		console.log(tab.id);
	});

	return true;
});

参考