diff --git a/chrome.js b/chrome.js index 3431054..0b1de48 100755 --- a/chrome.js +++ b/chrome.js @@ -83,6 +83,10 @@ function chromeControl(argv) { if (argv.length !== 2) { usage() } const arg = argv[1] focus(arg) + } else if (cmd === 'copy') { + if (argv.length !== 2) { usage() } + const arg = argv[1] + copy(arg) } else { usage() } @@ -97,27 +101,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': [] } @@ -182,6 +187,22 @@ function focus(arg) { chrome.activate() } +// Focus on a specific tab +function copy(arg) { + let { winIdx, tabIdx } = parseWinTabIdx(arg) + let urlToTitle = {} + let selectedTab = tabIdx; + + chrome.windows().forEach((window, winIdx) => { + window.tabs().forEach((tab, tabIdx) => { + if (tabIdx === selectedTab) { + urlToTitle = tab.url(); + } + }) + }) + println(urlToTitle) +} + // Close duplicate tabs function dedup() { let urls = {} diff --git a/integrations/Alfred/Chrome Control.alfredworkflow b/integrations/Alfred/Chrome Control.alfredworkflow index dc2f583..1b3ee67 100644 Binary files a/integrations/Alfred/Chrome Control.alfredworkflow and b/integrations/Alfred/Chrome Control.alfredworkflow differ