chrome.windows.getAll() 未定义?

发布于 2024-10-02 02:54:13 字数 684 浏览 5 评论 0原文

我想为 google chrome / chromium 编写一个扩展(一个会话管理器,它比图库中已有的扩展具有更多功能和养眼效果)。

但我无法让以下代码工作:

function list_session() {
 var list = [];
 chrome.windows.getAll(
  {"populate" : true},

  function (window_list) {
   for(window in window_list) {
    list.concat(window.tabs);
   }
  }
 );
 console.log(list);
 return list;
}

这是使用 google api 的一个相当简单的示例,但我只得到“未定义”值作为回报,而不是选项卡列表。此外,窗口列表似乎是空的。

我目前在 Ubuntu 10.10 下运行 Chromium 7.0.517.44 (64615)。我也尝试过谷歌的官方 chrome 版本,结果相同。

API 文档可以在这里找到: http://code.google.com/chrome/extensions/windows.html

菲尼亚斯

I want to write an extension (a session manager which has more features and eye candy than the ones already in the gallery) for google chrome / chromium.

But I can't get the following code to work:

function list_session() {
 var list = [];
 chrome.windows.getAll(
  {"populate" : true},

  function (window_list) {
   for(window in window_list) {
    list.concat(window.tabs);
   }
  }
 );
 console.log(list);
 return list;
}

It's a fairly simple example for the use of the google api, but instead of a list of tabs I get only 'undefined'values in return. Furthermore, the window list seems to be empty.

I'm currently running Chromium 7.0.517.44 (64615) under Ubuntu 10.10. I've tried the official chrome release from google as well with the same results.

API documentation can be found here:
http://code.google.com/chrome/extensions/windows.html

phineas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2024-10-09 02:54:13

假设您在清单中声明了 tabs 权限,则此代码存在几个问题:

  • list_session() 函数将返回空列表,因为您在回调函数中修改了列表,它可以在 console.log(list);return 15 分钟后由 chrome 调用。您需要更改程序结构以使用回调。

  • concat 方法不修改原始数组

  • in 运算符不建议使用循环遍历数组,因为它可能返回的不是您所期望的。

所以我会写这样的东西:

function list_session(callback) {

    chrome.windows.getAll({populate : true}, function (window_list) {
        var list = [];
        for(var i=0;i<window_list.length;i++) {
            list = list.concat(window_list[i].tabs);
        }
        console.log(list);
        if(callback) {
            callback(list);
        }
    });
}

//usage
list_session(function(tab_list) {
    //use array of tabs
});

Assuming you declared tabs permission in manifest, there are several problems with this code:

  • list_session() function will return empty list because you modify the list in a callback function, which could be called by chrome 15 minutes after your console.log(list); and return. You need to change your program structure to use callbacks instead.

  • concat method does not modify original array

  • in operator is not recommended to use to loop through an array as it might return not what you expect.

So I would write something like this:

function list_session(callback) {

    chrome.windows.getAll({populate : true}, function (window_list) {
        var list = [];
        for(var i=0;i<window_list.length;i++) {
            list = list.concat(window_list[i].tabs);
        }
        console.log(list);
        if(callback) {
            callback(list);
        }
    });
}

//usage
list_session(function(tab_list) {
    //use array of tabs
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文