chrome.windows.getAll() is undefined?

前端 未结 1 490
Happy的楠姐
Happy的楠姐 2021-01-01 04:03

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\'

相关标签:
1条回答
  • 2021-01-01 04:26

    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
    });
    
    0 讨论(0)
提交回复
热议问题