本文介绍了tabs.getCurrent()结果是未定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我导航到例如amazon.com或google.com并点击浏览器图标进行浏览器操作时,我不确定为什么我无法使用getCurrent()检索有关当前标签的信息。我缺少什么提示?



清单:

  { 
name:testGetCurrentTab,
version:1.0,
description:,
manifest_version:2,

图标:{
48:icons / icon-48.png
},

权限:[
标签,
< all_urls>
],

browser_action:{
default_icon:icon / icon-32.png
},

背景:{
scripts:[background.js]
}
}

背景:

  function displayInfo(){

function onGot (tabInfo){
console.log('Inside onGot()...');
console.log(tabInfo);
}

函数onError(错误){
console.log(`错误:$ {error}`);
}

var gettingCurrent = browser.tabs.getCurrent();
gettingCurrent.then(onGot,onError);
}

browser.browserAction.onClicked.addListener(displayInfo);

以下是输出:

 内在onGot()... background.js:4:7 

undefined background.js:5:7

Firefox Dev Edition 54(64位)



browserAction.onClicked 事件返回活动标签,您不需要其他API调用。

  browser.browserAction.onClicked .addListener(函数(tab){
console.log(tab)
})

请参阅的参数。


Not sure why I cannot retrieve info about current tab using getCurrent() when I navigate to, say, amazon.com or google.com and hit the browser icon for a browser action. Any hints on what I am missing?

MANIFEST:

{
  "name": "testGetCurrentTab",
  "version":  "1.0",
  "description":  "",
  "manifest_version": 2,

  "icons": {
    "48": "icons/icon-48.png"
  },

  "permissions": [
      "tabs",
      "<all_urls>"
  ],

  "browser_action": {
      "default_icon": "icon/icon-32.png"
  },

  "background": {
      "scripts": ["background.js"]
  }
}

BACKGROUND:

function displayInfo() {

  function onGot(tabInfo) {
    console.log('Inside onGot() ...');
    console.log(tabInfo);
  }

  function onError(error) {
    console.log(`Error: ${error}`);
  }

  var gettingCurrent = browser.tabs.getCurrent();
  gettingCurrent.then(onGot, onError);
}

browser.browserAction.onClicked.addListener(displayInfo);

Here is the output:

Inside onGot() ...  background.js:4:7

undefined  background.js:5:7

Firefox Dev Edition 54 (64bit)

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/getCurrent

解决方案

From MDN tabs.getCurrent():

The browserAction.onClicked event returns the active tab, you do not need another API call.

browser.browserAction.onClicked.addListener(function(tab) {
 console.log(tab)
})

See the tab parameter for browserAction.onClicked listener.

这篇关于tabs.getCurrent()结果是未定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 16:23