aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-04-09 10:38:37 +0900
committerGitHub <noreply@github.com>2020-04-09 10:38:37 +0900
commit1656d52d2cefb3846d968c6117484e6aefe7dabe (patch)
treeab58a99b832d2571e2168f2ee0e328bc12d9580e /src/background/completion/impl
parentc6c2da8547891b50aef2f08e5f36d258183831ff (diff)
parent5176643e64d8f4a6be5fc73f0eb48dc65322e496 (diff)
Merge pull request #730 from ueokande/refactor-console-and-completion
Refactor console and completions
Diffstat (limited to 'src/background/completion/impl')
-rw-r--r--src/background/completion/impl/BookmarkRepositoryImpl.ts28
-rw-r--r--src/background/completion/impl/HistoryRepositoryImpl.ts28
-rw-r--r--src/background/completion/impl/TabRepositoryImpl.ts41
-rw-r--r--src/background/completion/impl/filters.ts76
4 files changed, 173 insertions, 0 deletions
diff --git a/src/background/completion/impl/BookmarkRepositoryImpl.ts b/src/background/completion/impl/BookmarkRepositoryImpl.ts
new file mode 100644
index 0000000..58df129
--- /dev/null
+++ b/src/background/completion/impl/BookmarkRepositoryImpl.ts
@@ -0,0 +1,28 @@
+import { injectable } from "tsyringe";
+import BookmarkRepository, {BookmarkItem} from "../BookmarkRepository";
+
+const COMPLETION_ITEM_LIMIT = 10;
+
+@injectable()
+export default class BookmarkRepositoryImpl implements BookmarkRepository {
+ async queryBookmarks(query: string): Promise<BookmarkItem[]> {
+ const items = await browser.bookmarks.search({query});
+ return items
+ .filter(item => item.title && item.title.length > 0)
+ .filter(item => item.type === 'bookmark' && item.url)
+ .filter((item) => {
+ let url = undefined;
+ try {
+ url = new URL(item.url!!);
+ } catch (e) {
+ return false;
+ }
+ return url.protocol !== 'place:';
+ })
+ .slice(0, COMPLETION_ITEM_LIMIT)
+ .map(item => ({
+ title: item.title!!,
+ url: item.url!!,
+ }));
+ }
+}
diff --git a/src/background/completion/impl/HistoryRepositoryImpl.ts b/src/background/completion/impl/HistoryRepositoryImpl.ts
new file mode 100644
index 0000000..42691aa
--- /dev/null
+++ b/src/background/completion/impl/HistoryRepositoryImpl.ts
@@ -0,0 +1,28 @@
+import { injectable } from "tsyringe";
+import * as filters from "./filters";
+import HistoryRepository, {HistoryItem} from "../HistoryRepository";
+
+const COMPLETION_ITEM_LIMIT = 10;
+
+@injectable()
+export default class HistoryRepositoryImpl implements HistoryRepository {
+ async queryHistories(keywords: string): Promise<HistoryItem[]> {
+ const items = await browser.history.search({
+ text: keywords,
+ startTime: 0,
+ });
+
+ return [items]
+ .map(filters.filterBlankTitle)
+ .map(filters.filterHttp)
+ .map(filters.filterByTailingSlash)
+ .map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT))
+ .map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0]
+ .sort((x, y) => Number(y.visitCount) - Number(x.visitCount))
+ .slice(0, COMPLETION_ITEM_LIMIT)
+ .map(item => ({
+ title: item.title!!,
+ url: item.url!!,
+ }))
+ }
+}
diff --git a/src/background/completion/impl/TabRepositoryImpl.ts b/src/background/completion/impl/TabRepositoryImpl.ts
new file mode 100644
index 0000000..adcaba7
--- /dev/null
+++ b/src/background/completion/impl/TabRepositoryImpl.ts
@@ -0,0 +1,41 @@
+import TabRepository, { Tab } from "../TabRepository";
+
+const COMPLETION_ITEM_LIMIT = 10;
+
+export default class TabRepositoryImpl implements TabRepository {
+ async queryTabs(query: string, excludePinned: boolean): Promise<Tab[]> {
+ const tabs = await browser.tabs.query({ currentWindow: true });
+ return tabs
+ .filter((t) => {
+ return t.url && t.url.toLowerCase().includes(query.toLowerCase()) ||
+ t.title && t.title.toLowerCase().includes(query.toLowerCase());
+ })
+ .filter((t) => {
+ return !(excludePinned && t.pinned);
+ })
+ .filter(item => item.id && item.title && item.url)
+ .slice(0, COMPLETION_ITEM_LIMIT)
+ .map(TabRepositoryImpl.toEntity);
+ }
+
+ async getAllTabs(excludePinned: boolean): Promise<Tab[]> {
+ if (excludePinned) {
+ return (await browser.tabs.query({ currentWindow: true, pinned: true }))
+ .map(TabRepositoryImpl.toEntity)
+
+ }
+ return (await browser.tabs.query({ currentWindow: true }))
+ .map(TabRepositoryImpl.toEntity)
+ }
+
+ private static toEntity(tab: browser.tabs.Tab,): Tab {
+ return {
+ id: tab.id!!,
+ url: tab.url!!,
+ active: tab.active,
+ title: tab.title!!,
+ faviconUrl: tab.favIconUrl,
+ index: tab.index,
+ }
+ }
+}
diff --git a/src/background/completion/impl/filters.ts b/src/background/completion/impl/filters.ts
new file mode 100644
index 0000000..98957a7
--- /dev/null
+++ b/src/background/completion/impl/filters.ts
@@ -0,0 +1,76 @@
+type Item = browser.history.HistoryItem;
+
+const filterHttp = (items: Item[]): Item[] => {
+ const httpsHosts = items.map(x => new URL(x.url as string))
+ .filter(x => x.protocol === 'https:')
+ .map(x => x.host);
+ const hostsSet = new Set(httpsHosts);
+
+ return items.filter((item: Item) => {
+ const url = new URL(item.url as string);
+ return url.protocol === 'https:' || !hostsSet.has(url.host);
+ });
+};
+
+const filterBlankTitle = (items: Item[]): Item[] => {
+ return items.filter(item => item.title && item.title !== '');
+};
+
+const filterByTailingSlash = (items: Item[]): Item[] => {
+ const urls = items.map(item => new URL(item.url as string));
+ const simplePaths = urls
+ .filter(url => url.hash === '' && url.search === '')
+ .map(url => url.origin + url.pathname);
+ const pathsSet = new Set(simplePaths);
+
+ return items.filter((item) => {
+ const url = new URL(item.url as string);
+ if (url.hash !== '' || url.search !== '' ||
+ url.pathname.slice(-1) !== '/') {
+ return true;
+ }
+ return !pathsSet.has(url.origin + url.pathname.slice(0, -1));
+ });
+};
+
+const filterByPathname = (items: Item[], min: number): Item[] => {
+ const hash: {[key: string]: Item} = {};
+ for (const item of items) {
+ const url = new URL(item.url as string);
+ const pathname = url.origin + url.pathname;
+ if (!hash[pathname]) {
+ hash[pathname] = item;
+ } else if ((hash[pathname].url as string).length >
+ (item.url as string).length) {
+ hash[pathname] = item;
+ }
+ }
+ const filtered = Object.values(hash);
+ if (filtered.length < min) {
+ return items;
+ }
+ return filtered;
+};
+
+const filterByOrigin = (items: Item[], min: number): Item[] => {
+ const hash: {[key: string]: Item} = {};
+ for (const item of items) {
+ const origin = new URL(item.url as string).origin;
+ if (!hash[origin]) {
+ hash[origin] = item;
+ } else if ((hash[origin].url as string).length >
+ (item.url as string).length) {
+ hash[origin] = item;
+ }
+ }
+ const filtered = Object.values(hash);
+ if (filtered.length < min) {
+ return items;
+ }
+ return filtered;
+};
+
+export {
+ filterHttp, filterBlankTitle, filterByTailingSlash,
+ filterByPathname, filterByOrigin
+};