tabs.getCurrent()结果是未定义的? [英] tabs.getCurrent() result is undefined?

查看:126
本文介绍了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位)



https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs来自MDN tabs.getCurrent()


获取 tabs.Tab ,其中包含有关运行此脚本的选项卡的信息。



您可以在上下文中调用此函数有一个浏览器选项卡,例如选项页面。如果你从后台脚本或弹出窗口调用它,它将返回undefined。


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

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

请参阅 选项卡 browserAction.onClicked 听众的参数。


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():

Get a tabs.Tab containing information about the tab that this script is running in.

You can call this function in contexts where there is a browser tab, such as an options page. If you call it from a background script or a popup, it will return undefined.

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()结果是未定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆