diff options
Diffstat (limited to 'src/background/completion/impl/TabRepositoryImpl.ts')
-rw-r--r-- | src/background/completion/impl/TabRepositoryImpl.ts | 41 |
1 files changed, 41 insertions, 0 deletions
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, + } + } +} |