Chrome扩展API:chrome.tabs.query的全面解析
一、引言
在Chrome扩展开发中,chrome.tabs.query
是一个非常实用的API,它允许开发者根据特定条件查询浏览器中的标签页。本文将详细介绍 chrome.tabs.query
的使用场景、参数用法、各参数默认值以及其他可能的参数。想不到一个小小的chrome插件开发有那么多坑。
1.1 未见解释先看代码
content script中的代码,用于调试,getData 为sidePanel提供一个按钮用于和content script进行通信,然后触发getData 这个函数
const getData = () => {
testTabsQuery({
active: true,
currentWindow: true,
});
testTabsQuery({
active: true,
});
testTabsQuery({
currentWindow: true,
});
}
function testTabsQuery(queryInfo) {
chrome.tabs.query(queryInfo, tabs => {
for (const tab of tabs) {
console.log(tab.id, tab.url);
}
if (queryInfo.active && queryInfo.currentWindow) {
console.log('=============both=============');
return;
} else if (queryInfo.active) {
console.log('=============active============='