Skip to content

Commit

Permalink
Added "Bookmark this tab" command
Browse files Browse the repository at this point in the history
  • Loading branch information
David Buezas committed Sep 22, 2021
1 parent 94980d3 commit f69089e
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 89 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@

A SublimeText/Atom/Vscode style command palette for chrome!

![alt text](screenshot.png)

## Installation

[Chrome Web Store](https://chrome.google.com/webstore/detail/chrome-palette/hjkpneggcnclhpkddehdhlkeljclcnbo)

Or you can unzip [chrome-palette.zip](https://github.com/dbuezas/chrome-palette/raw/master/chrome-palette.zip) and load it unpacked from [chrome://extensions](chrome://extensions)

![alt text](./screenshots/marquee.jpg)

## Features

- [x] Commands sorted by usage
- [x] Fuzzy search of commands
- [x] Search open tabs
- [x] Search history
- [x] Search in gdrive
- [x] Search sub commands (open tabs, bookmarks, history, etc)

## Command list

- New Tab
- New Window
- Show History Page
- Show Downloads
- Show Extensions
- Show Bookmarks
- Show Settings
- Close Current Tab
- Reload Tab
- Reload All Tabs
- Clear Cache and Reload Tab
- Toggle Pin
- Duplicate Tab
- New Incognito Window
- Close Other Tabs
- Close Tabs To Right
- Close Tabs To Left
- Mute/Unmute Tab
- Move Tab To Start
- Move Tab To End
- Move Tab Left
- Move Tab Right
- Reopen/Unclose Tab
- Deattach Tab (Move to New Window)
- Reattach Tab (Move Tab to Previous Window)
- Reset command history
- Search Tabs
- Search History
- Search Google Driveg
- Search Youtube
- Search Google
- Search Wikipedia
- [x] New Tab
- [x] New Window
- [x] Open History Page
- [x] Open Downloads
- [x] Open Extensions
- [x] Open Bookmarks
- [x] Add this tab to Bookmarks
- [x] Open Settings
- [x] Close Current Tab
- [x] Reload Tab
- [x] Reload All Tabs
- [x] Clear Cache and Reload Tab
- [x] Toggle Pin
- [x] Duplicate Tab
- [x] New Incognito Window
- [x] Close Other Tabs
- [x] Close Tabs To Right
- [x] Close Tabs To Left
- [x] Mute/Unmute Tab
- [x] Move Tab To Start
- [x] Move Tab To End
- [x] Move Tab Left
- [x] Move Tab Right
- [x] Reopen/Unclose Tab
- [x] Deattach Tab (Move to New Window)
- [x] Reattach Tab (Move Tab to Previous Window)
- [x] Reset command history
- [x] Toggle Full screen mode
- [x] Search Tabs
- [x] Search History
- [x] Search Google Drive
- [x] Search Youtube
- [x] Search Google
- [x] Search Wikipedia

## Development

Expand All @@ -62,7 +62,7 @@ npm start

## Full Manual testing

Import the dist folder as an unpacked extension in chrome. This folder is rebuilt on changes when using `npm start`
Import the `dist` folder as an unpacked extension in chrome. This folder is rebuilt on changes when using `npm start`

# Previous art

Expand Down
Binary file modified chrome-palette.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-palette",
"version": "1.1.0",
"version": "1.2.0",
"private": false,
"author": "[email protected]",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Chrome Palette",
"version": "1.1.0",
"version": "1.2.0",
"manifest_version": 2,
"description": "Chrome Palette, a command palette for chrome",
"icons": {
Expand Down
Binary file added screenshots/large.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/main.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/main2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/main3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/main4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/main5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/marquee.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/small.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/App.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
width: 605px;
height: 380px;
height: 395px;
background: rgba(0, 0, 0, 0.75);
padding: 10px;
}
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useAudibleTabSuggestions } from "./hooks/audioSuggestions";
import { useSwitchTabSuggestions } from "./hooks/tabsSuggestions";
import { useHistorySuggestions } from "./hooks/historySuggestions";
import { useBookmarkSuggestions } from "./hooks/bookmarkSuggestions";
import { useBookmarkThisSuggestions } from "./hooks/bookmarkThisSuggestions";

import { sortByUsed, storeLastUsed } from "./last-used";
import usePaletteInput from "./hooks/usePaletteInput";
Expand All @@ -28,6 +29,7 @@ function App() {
...useSwitchTabSuggestions("t", input),
...useHistorySuggestions("h", input),
...useBookmarkSuggestions("b", input),
...useBookmarkThisSuggestions("bt", input),
...useTemplatedSuggestions(input),
]);

Expand Down
3 changes: 3 additions & 0 deletions src/SampleAtomCommand.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
body .atom-suggestionsContainerOpen {
max-height: 330px;
}
.atom-category {
color: #fff;
margin-right: 6px;
Expand Down
52 changes: 23 additions & 29 deletions src/hooks/bookmarkSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import { useEffect, useRef, useState } from "react";
import { useRef, useState } from "react";
import { Command } from "./commandsSuggestions";
import { formatDistance } from "date-fns";
import { formatDistanceToNow } from "date-fns";
import { parseInputCommand } from "./parseInputCommand";
import browser from "webextension-polyfill";
import { UseSuggestionParam } from "./websitesSuggestions";
import { isDefined } from "./historySuggestions";
import niceUrl from "./niceUrl";

const traverse = (
nodes: browser.Bookmarks.BookmarkTreeNode[],
breadcrumb = ""
): (Command & { dateAdded: number })[] => {
return nodes
.flatMap(({ children, url, title, dateAdded }) => {
const path = breadcrumb ? breadcrumb + "/" + title : title;
if (children) {
return traverse(children, path);
}
url ||= "";
const niceUrl =
url.length <= 80 ? url : url.slice(0, 40) + "..." + url.slice(-37);

return {
name: `${title} > ${breadcrumb}\n${niceUrl}`,
icon: "chrome://favicon/" + url,
category: "Bookmark",
dateAdded: dateAdded || 0,
timeAgo:
dateAdded &&
formatDistance(new Date(dateAdded || 0), new Date(), {
addSuffix: true,
}),
command: async function () {
await browser.tabs.create({ url });
},
};
})
.sort((a, b) => b.dateAdded - a.dateAdded);
return nodes.flatMap(({ children, url, title, dateAdded }) => {
const path = breadcrumb ? breadcrumb + "/" + title : title;
if (children) {
return traverse(children, path);
}
url ||= "";
return {
name: `${title} > ${breadcrumb}\n${niceUrl(url)}`,
icon: "chrome://favicon/" + url,
category: "Bookmark",
dateAdded: dateAdded || 0,
timeAgo: dateAdded
? formatDistanceToNow(new Date(dateAdded || 0))
: undefined,
command: async function () {
await browser.tabs.create({ url });
},
};
});
// .sort((a, b) => b.dateAdded - a.dateAdded);
};

export function useBookmarkSuggestions(
Expand Down
78 changes: 78 additions & 0 deletions src/hooks/bookmarkThisSuggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useRef, useState } from "react";
import { Command } from "./commandsSuggestions";
import { formatDistanceToNow } from "date-fns";
import { parseInputCommand } from "./parseInputCommand";
import browser from "webextension-polyfill";
import { UseSuggestionParam } from "./websitesSuggestions";
import niceUrl from "./niceUrl";
import { isDefined } from "./historySuggestions";

const traverse = (
nodes: browser.Bookmarks.BookmarkTreeNode[],
breadcrumb = ""
): Command[] => {
return nodes.flatMap(({ id, children, url, title, dateAdded }) => {
const path = breadcrumb ? breadcrumb + " / " + title : title;
const list: Command[] = [];
if (!url && path !== "") {
list.push({
name: path,
icon: "chrome://favicon/",
category: "Add Bookmark",
timeAgo:
dateAdded !== 0
? undefined
: formatDistanceToNow(new Date(dateAdded || 0)),
command: async function () {
const [tab] = await browser.tabs.query({
currentWindow: true,
active: true,
});
await browser.bookmarks.create({
index: 0,
url: tab.url,
title: tab.title,
parentId: id,
});
window.close();
},
});
}
if (children) {
list.push(...traverse(children, path));
}
return list;
});
};

export function useBookmarkThisSuggestions(
KEYWORD: string,
{ setInputValue, inputValue }: UseSuggestionParam
) {
const [commands, setCommands] = useState<Command[]>([]);
const { didMatch, keyword } = parseInputCommand(inputValue);
const myMatch = keyword === KEYWORD;
const didFetch = useRef(false);
if (myMatch && !didFetch.current) {
didFetch.current = true;
const fetch = async () => {
if (!browser) return;
const root = await browser.bookmarks.getTree();
setCommands(traverse(root));
};
fetch();
}

if (myMatch) return commands;
if (didMatch) return [];
return [
{
name: "Bookmark this tab",
category: "Add Bookmark",
command: async function () {
setInputValue(KEYWORD + ">");
},
keyword: KEYWORD + ">",
},
];
}
34 changes: 29 additions & 5 deletions src/hooks/commandsSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { parseInputCommand } from "./parseInputCommand";
import { UseSuggestionParam } from "./websitesSuggestions";
export type Command = {
name: string;
icon?: string;
category?: string;
shortcut?: string;
timeAgo?: string;
keyword?: string;
command: Function;
onHighlighted?: Function;
Expand Down Expand Up @@ -38,38 +40,46 @@ function useCommandSuggestions({
},
},
{
name: "Show History Page",
name: "Open History Page",
category: "Command",
shortcut: "⌘ y",
command: async function () {
await browser.tabs.create({ url: "chrome://history" });
},
},
{
name: "Show Downloads",
name: "Open Downloads",
category: "Command",
shortcut: "⌘⇧ d",
command: async function () {
await browser.tabs.create({ url: "chrome://downloads" });
},
},
{
name: "Show Extensions",
name: "Open Extensions",
category: "Command",
command: async function () {
await browser.tabs.create({ url: "chrome://extensions" });
},
},
{
name: "Show Bookmarks",
name: "Open Bookmark Manager",
category: "Command",
shortcut: "⌘⌥ b",
command: async function () {
await browser.tabs.create({ url: "chrome://bookmarks" });
},
},
{
name: "Show Settings",
name: "Show/hide Bookmarks Bar",
category: "Command",
shortcut: "⌘⇧ b",
command: async function () {
setInputValue("Unsupported. Use [⌘⇧ b] instead.");
},
},
{
name: "Open Settings",
category: "Command",
shortcut: "⌘ ,",
command: async function () {
Expand Down Expand Up @@ -322,6 +332,20 @@ function useCommandSuggestions({
await browser.tabs.update(currentTab.id!, { highlighted: true });
},
},
{
name: "Toggle full screen",
category: "Command",
shortcut: "⌃⌘ f",
command: async function () {
const currWindow = await browser.windows.getCurrent();
const state =
currWindow.state === "fullscreen" ? "normal" : "fullscreen";
browser.windows.update(currWindow.id!, {
state,
});
window.close();
},
},
{
name: "Reset command history",
category: "Command",
Expand Down
Loading

0 comments on commit f69089e

Please sign in to comment.