如何基于lastFocusedWindow对getAllwindows的结果进行排序?

时间:2019-02-07 07:43:52

标签: javascript google-chrome-extension

是否可以根据lastFocused窗口对getAllWindows的结果进行排序?

chrome.windows.getAll(null, (windows) => {
    /* how to sort the windows absed on lastFocused */
})

1 个答案:

答案 0 :(得分:0)

您可以在阵列中维护集中的Windows历史记录。然后按历史记录数组中的索引对窗口项进行排序:

var focusedWindowHistoty=[];


chrome.windows.onFocusChanged.addListener(function(window) {

    focusedWindowHistoty = focusedWindowHistoty.filter(function(it){
       return it != window;
    });
    focusedWindowHistoty.push(window);
});

chrome.windows.getAll(null, (windows) => {
    var sorted=windows.sort(function(a,b){
       return getIndex(a)>getIndex(b);
    });
})

function getIndex(item){
  var foundIndex=-1;
  focusedWindowHistoty.forEach(function(it,index){
    if(it==item){
      foundIndex=index;
    }
  })
  return foundIndex;
}