Chrome 扩展:在 background.js 中,我无法获取选项卡详细信息我尝试了 3 种方法,但结果未定义

问题描述

后台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"]
    },

当我不使用查询时,响应总是未定义的。查询仅第一次有效

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)