Skip to content

Commit

Permalink
Improve list command performance. This is done by initally storing al…
Browse files Browse the repository at this point in the history
…l tabs titles/urls in double entries arrays (matching windows/tabs indexes) instead of requesting the values in a double for loop. (#7)
  • Loading branch information
AnthoPakPak committed Mar 10, 2021
1 parent 8fea921 commit 98df006
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,28 @@ function chromeControl(argv) {
// List all open tabs
function list() {
// Iterate all tabs in all windows
let urlToTitle = {}
chrome.windows().forEach((window, winIdx) => {
window.tabs().forEach((tab, tabIdx) => {
urlToTitle[tab.url()] = {
'title': tab.title() || 'No Title',
'url': tab.url(),
// Double entries arrays matching windows/tabs indexes (Using this improves a lot the performances)
let allTabsTitle = chrome.windows.tabs.title()
let allTabsUrls = chrome.windows.tabs.url()

var titleToUrl = {}
for (var winIdx = 0; winIdx < allTabsTitle.length; winIdx++) {
for (var tabIdx = 0; tabIdx < allTabsTitle[winIdx].length; tabIdx ++) {
let title = allTabsTitle[winIdx][tabIdx]
let url = allTabsUrls[winIdx][tabIdx]

titleToUrl[title] = {
'title': title || 'No Title',
'url': url,
'winIdx': winIdx,
'tabIdx': tabIdx,

// Alfred specific properties
'arg': `${winIdx},${tabIdx}`,
'subtitle': tab.url(),
'subtitle': url,
}
})
})

// Create a title to url map
let titleToUrl = {}
Object.keys(urlToTitle).forEach(url => {
titleToUrl[urlToTitle[url].title] = urlToTitle[url]
})
}
}

// Generate output
out = { 'items': [] }
Expand Down

0 comments on commit 98df006

Please sign in to comment.