admin管理员组文章数量:1289528
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: .html
phineas
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./chrome/extensions/windows.html
phineas
Share Improve this question asked Nov 11, 2010 at 17:26 f4lcof4lco 3,8245 gold badges29 silver badges54 bronze badges1 Answer
Reset to default 7Assuming 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 remended 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
});
本文标签: javascriptchromewindowsgetAll() is undefinedStack Overflow
版权声明:本文标题:javascript - chrome.windows.getAll() is undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741457522a2379840.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论