chrome.windows.getAll() 未定义?
我想为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您在清单中声明了
tabs
权限,则此代码存在几个问题:list_session()
函数将返回空列表,因为您在回调函数中修改了列表,它可以在console.log(list);
和return
15 分钟后由 chrome 调用。您需要更改程序结构以使用回调。concat
方法不修改原始数组in
运算符不建议使用循环遍历数组,因为它可能返回的不是您所期望的。所以我会写这样的东西:
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 yourconsole.log(list);
andreturn
. You need to change your program structure to use callbacks instead.concat
method does not modify original arrayin
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: