aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/completion')
-rw-r--r--src/background/completion/BookmarkRepository.ts8
-rw-r--r--src/background/completion/HistoryRepository.ts8
-rw-r--r--src/background/completion/OpenCompletionUseCase.ts59
-rw-r--r--src/background/completion/PropertyCompletionUseCase.ts16
-rw-r--r--src/background/completion/TabCompletionUseCase.ts55
-rw-r--r--src/background/completion/TabItem.ts11
-rw-r--r--src/background/completion/TabRepository.ts14
-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
11 files changed, 344 insertions, 0 deletions
diff --git a/src/background/completion/BookmarkRepository.ts b/src/background/completion/BookmarkRepository.ts
new file mode 100644
index 0000000..14105c8
--- /dev/null
+++ b/src/background/completion/BookmarkRepository.ts
@@ -0,0 +1,8 @@
+export type BookmarkItem = {
+ title: string
+ url: string
+}
+
+export default interface BookmarkRepository {
+ queryBookmarks(query: string): Promise<BookmarkItem[]>;
+}
diff --git a/src/background/completion/HistoryRepository.ts b/src/background/completion/HistoryRepository.ts
new file mode 100644
index 0000000..5eb3a2b
--- /dev/null
+++ b/src/background/completion/HistoryRepository.ts
@@ -0,0 +1,8 @@
+export type HistoryItem = {
+ title: string
+ url: string
+}
+
+export default interface HistoryRepository {
+ queryHistories(keywords: string): Promise<HistoryItem[]>;
+}
diff --git a/src/background/completion/OpenCompletionUseCase.ts b/src/background/completion/OpenCompletionUseCase.ts
new file mode 100644
index 0000000..1b63e7c
--- /dev/null
+++ b/src/background/completion/OpenCompletionUseCase.ts
@@ -0,0 +1,59 @@
+import { inject, injectable } from "tsyringe";
+import CachedSettingRepository from "../repositories/CachedSettingRepository";
+import CompletionType from "../../shared/CompletionType";
+import BookmarkRepository from "./BookmarkRepository";
+import HistoryRepository from "./HistoryRepository";
+
+export type BookmarkItem = {
+ title: string
+ url: string
+}
+
+export type HistoryItem = {
+ title: string
+ url: string
+}
+
+@injectable()
+export default class OpenCompletionUseCase {
+ constructor(
+ @inject('BookmarkRepository') private bookmarkRepository: BookmarkRepository,
+ @inject('HistoryRepository') private historyRepository: HistoryRepository,
+ @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository,
+ ) {
+ }
+
+ async getCompletionTypes(): Promise<CompletionType[]> {
+ const settings = await this.cachedSettingRepository.get();
+ const types: CompletionType[] = [];
+ for (const c of settings.properties.complete) {
+ switch (c) {
+ case 's':
+ types.push(CompletionType.SearchEngines);
+ break;
+ case 'h':
+ types.push(CompletionType.History);
+ break;
+ case 'b':
+ types.push(CompletionType.Bookmarks);
+ break;
+ }
+ // ignore invalid characters in the complete property
+ }
+ return types;
+ }
+
+ async requestSearchEngines(query: string): Promise<string[]> {
+ const settings = await this.cachedSettingRepository.get();
+ return Object.keys(settings.search.engines)
+ .filter(key => key.startsWith(query))
+ }
+
+ requestBookmarks(query: string): Promise<BookmarkItem[]> {
+ return this.bookmarkRepository.queryBookmarks(query);
+ }
+
+ requestHistory(query: string): Promise<HistoryItem[]> {
+ return this.historyRepository.queryHistories(query);
+ }
+} \ No newline at end of file
diff --git a/src/background/completion/PropertyCompletionUseCase.ts b/src/background/completion/PropertyCompletionUseCase.ts
new file mode 100644
index 0000000..049cfb8
--- /dev/null
+++ b/src/background/completion/PropertyCompletionUseCase.ts
@@ -0,0 +1,16 @@
+import { injectable } from "tsyringe";
+import Properties from "../../shared/settings/Properties";
+
+type Property = {
+ name: string;
+ type: 'string' | 'boolean' | 'number';
+}
+@injectable()
+export default class PropertyCompletionUseCase {
+ async getProperties(): Promise<Property[]> {
+ return Properties.defs().map(def => ({
+ name: def.name,
+ type: def.type,
+ }));
+ }
+} \ No newline at end of file
diff --git a/src/background/completion/TabCompletionUseCase.ts b/src/background/completion/TabCompletionUseCase.ts
new file mode 100644
index 0000000..dec86e9
--- /dev/null
+++ b/src/background/completion/TabCompletionUseCase.ts
@@ -0,0 +1,55 @@
+import { inject, injectable } from "tsyringe";
+import TabItem from "./TabItem";
+import TabRepository, { Tab } from "./TabRepository";
+import TabPresenter from "../presenters/TabPresenter";
+import TabFlag from "../../shared/TabFlag";
+
+@injectable()
+export default class TabCompletionUseCase {
+ constructor(
+ @inject('TabRepository') private tabRepository: TabRepository,
+ @inject('TabPresenter') private tabPresenter: TabPresenter,
+ ) {
+ }
+
+ async queryTabs(query: string, excludePinned: boolean): Promise<TabItem[]> {
+ const lastTabId = await this.tabPresenter.getLastSelectedId();
+ const allTabs = await this.tabRepository.getAllTabs(excludePinned);
+ const num = parseInt(query, 10);
+ let tabs: Tab[] = [];
+ if (!isNaN(num)) {
+ const tab = allTabs.find(t => t.index === num - 1);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else if (query == '%') {
+ const tab = allTabs.find(t => t.active);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else if (query == '#') {
+ const tab = allTabs.find(t => t.id === lastTabId);
+ if (tab) {
+ tabs = [tab];
+ }
+ } else {
+ tabs = await this.tabRepository.queryTabs(query, excludePinned);
+ }
+
+ return tabs.map(tab => {
+ let flag = TabFlag.None;
+ if (tab.active) {
+ flag = TabFlag.CurrentTab
+ } else if (tab.id == lastTabId) {
+ flag = TabFlag.LastTab
+ }
+ return {
+ index: tab.index + 1,
+ flag: flag,
+ title: tab.title,
+ url: tab.url,
+ faviconUrl : tab.faviconUrl
+ }
+ });
+ }
+}
diff --git a/src/background/completion/TabItem.ts b/src/background/completion/TabItem.ts
new file mode 100644
index 0000000..630855a
--- /dev/null
+++ b/src/background/completion/TabItem.ts
@@ -0,0 +1,11 @@
+import TabFlag from "../../shared/TabFlag";
+
+type TabItem = {
+ index: number
+ flag: TabFlag
+ title: string
+ url: string
+ faviconUrl?: string
+}
+
+export default TabItem; \ No newline at end of file
diff --git a/src/background/completion/TabRepository.ts b/src/background/completion/TabRepository.ts
new file mode 100644
index 0000000..fe1b601
--- /dev/null
+++ b/src/background/completion/TabRepository.ts
@@ -0,0 +1,14 @@
+export type Tab = {
+ id: number
+ index: number
+ active: boolean
+ title: string
+ url: string
+ faviconUrl?: string
+}
+
+export default interface TabRepository {
+ queryTabs(query: string, excludePinned: boolean): Promise<Tab[]>;
+
+ getAllTabs(excludePinned: boolean): Promise<Tab[]>
+}
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
+};