在后台js中:
chrome.tabs.onActivated.addListener((activeInfo)=>{
var tabId=activeInfo.tabId;
console.log("tab switched: "+tabId);
chrome.tabs.get(tabId,(tab)=>{
console.log("getTabById: "+tab);
});
chrome.tabs.getSelected(null, function(tab) {
console.log("getTabByUsingSelected: "+tab);
});
chrome.tabs.query({"active":true,"currentWindow":true,'lastFocusedWindow': true}, (tabs)=>{
if (chrome.runtime.lastError) {
}else{
if(tabs!==undefined){
console.log("getTabByQuery: "+tabs[0].url);
}
}
});
});
显现:
"manifest_version":2
"permissions":[
"storage",
"activeTab",
"tabs",
"<all_urls>"
],
"background":{
"scripts": ["background.js"]
},
当我不使用查询时,响应总是未定义的。查询只在第一次工作