Skip to content

Commit

Permalink
feat: Adds an adjusted chrome tab opening action
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed May 23, 2022
1 parent cb4bef0 commit 72e9565
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions scripts/chrome-tab-open.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import "@johnlindquist/kit"

// Menu: [c] Open Chrome Tab
// Description: List all Chrome tabs. Then switch to that tab or copy with CMD + c
// Author: John Lindquist
// Twitter: @johnlindquist

let currentTabs = await getTabs()

let bookmarks = await readFile(
home(
"Library/Application Support/Google/Chrome/Profile 1/Bookmarks"
)
)

const parsedBookmarks = JSON.parse(bookmarks.toString())
const initialItems = parsedBookmarks.roots.bookmark_bar.children

const buildFolderItems = (prefixes, items): any[] => {
let newItems = [];
for(const bookmarkItem of items) {
if(bookmarkItem.type === 'folder') {
newItems = [...newItems, ...buildFolderItems([...prefixes, bookmarkItem.name], bookmarkItem.children)];
} else {
newItems.push({
name: bookmarkItem.name,
value: bookmarkItem.url,
// preview: bookmarkItem.url,
description: `${prefixes.join('/')}/${bookmarkItem.name}\n${bookmarkItem.url}`
});
}
}

return newItems;
}

let bookmarkChoices = buildFolderItems([], initialItems);

let currentOpenChoices = currentTabs.map(
({ url, title }) => ({
name: url,
value: url,
description: title,
preview: url
})
)

const bookmarksAndOpen = [
...bookmarkChoices,
...currentOpenChoices,
]
const choices = _.uniqBy(bookmarksAndOpen, "name")

const flags = {
open: {
name: "Open",
shortcut: "cmd+o",
},
copy: {
name: "Copy",
shortcut: "cmd+c",
},
};

let url = await arg(
{
placeholder: 'Select Chrome tab:',
flags
},
choices
)

if (flag?.copy) {
copy(url)
} else {
focusTab(url)
}

0 comments on commit 72e9565

Please sign in to comment.