chrome.windows.getAll()未定义? [英] chrome.windows.getAll() is undefined?
问题描述
我想写一个扩展名(一个会话管理器,它具有比画廊中已有的更多的功能和眼睛糖果)谷歌chrome / chromium。
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;
}
这是使用google api的一个相当简单的例子,而不是一个标签列表我只得到'undefined'值作为回报。此外,窗口列表似乎是空的。
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.
我目前正在Ubuntu 10.10下运行Chromium 7.0.517.44(64615)。我已经尝试了google的官方chrome版本以及相同的结果。
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文档可以在这里找到:
http://code.google.com/chrome/extensions/windows.html
API documentation can be found here: http://code.google.com/chrome/extensions/windows.html
phineas
推荐答案
假设您宣布标签
清单中的权限,此代码有几个问题:
Assuming you declared tabs
permission in manifest, there are several problems with this code:
-
list_session()
函数将返回空列表,因为您修改了回调函数中的列表,该函数可以在console.log(list);
之后15分钟由chrome调用返回
。您需要更改程序结构以改为使用回调。
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
方法不会修改原始数组
concat
method does not modify original array
所以我会这样写:
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
});
这篇关于chrome.windows.getAll()未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!