aboutsummaryrefslogtreecommitdiff
path: root/src/background/repositories
diff options
context:
space:
mode:
Diffstat (limited to 'src/background/repositories')
-rw-r--r--src/background/repositories/bookmark.js13
-rw-r--r--src/background/repositories/completions.js31
-rw-r--r--src/background/repositories/find.js18
-rw-r--r--src/background/repositories/persistent-setting.js16
-rw-r--r--src/background/repositories/setting.js23
-rw-r--r--src/background/repositories/version.js10
6 files changed, 111 insertions, 0 deletions
diff --git a/src/background/repositories/bookmark.js b/src/background/repositories/bookmark.js
new file mode 100644
index 0000000..99f7ec4
--- /dev/null
+++ b/src/background/repositories/bookmark.js
@@ -0,0 +1,13 @@
+export default class BookmarkRepository {
+ async create(title, url) {
+ let item = await browser.bookmarks.create({
+ type: 'bookmark',
+ title,
+ url,
+ });
+ if (!item) {
+ throw new Error('Could not create a bookmark');
+ }
+ return item;
+ }
+}
diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js
new file mode 100644
index 0000000..1318d36
--- /dev/null
+++ b/src/background/repositories/completions.js
@@ -0,0 +1,31 @@
+export default class CompletionsRepository {
+ async queryBookmarks(keywords) {
+ let items = await browser.bookmarks.search({ query: keywords });
+ return items.filter((item) => {
+ let url = undefined;
+ try {
+ url = new URL(item.url);
+ } catch (e) {
+ return false;
+ }
+ return item.type === 'bookmark' && url.protocol !== 'place:';
+ });
+ }
+
+ queryHistories(keywords) {
+ return browser.history.search({
+ text: keywords,
+ startTime: 0,
+ });
+ }
+
+ async queryTabs(keywords, excludePinned) {
+ let tabs = await browser.tabs.query({ currentWindow: true });
+ return tabs.filter((t) => {
+ return t.url.toLowerCase().includes(keywords.toLowerCase()) ||
+ t.title && t.title.toLowerCase().includes(keywords.toLowerCase());
+ }).filter((t) => {
+ return !(excludePinned && t.pinned);
+ });
+ }
+}
diff --git a/src/background/repositories/find.js b/src/background/repositories/find.js
new file mode 100644
index 0000000..9482e78
--- /dev/null
+++ b/src/background/repositories/find.js
@@ -0,0 +1,18 @@
+import MemoryStorage from '../infrastructures/memory-storage';
+
+const FIND_KEYWORD_KEY = 'find-keyword';
+
+export default class FindRepository {
+ constructor() {
+ this.cache = new MemoryStorage();
+ }
+
+ getKeyword() {
+ return Promise.resolve(this.cache.get(FIND_KEYWORD_KEY));
+ }
+
+ setKeyword(keyword) {
+ return this.cache.set(FIND_KEYWORD_KEY, keyword);
+ }
+}
+
diff --git a/src/background/repositories/persistent-setting.js b/src/background/repositories/persistent-setting.js
new file mode 100644
index 0000000..247ea6f
--- /dev/null
+++ b/src/background/repositories/persistent-setting.js
@@ -0,0 +1,16 @@
+import Setting from '../domains/setting';
+
+export default class SettingRepository {
+ save(settings) {
+ return browser.storage.local.set({ settings: settings.serialize() });
+ }
+
+ async load() {
+ let { settings } = await browser.storage.local.get('settings');
+ if (!settings) {
+ return null;
+ }
+ return Setting.deserialize(settings);
+ }
+}
+
diff --git a/src/background/repositories/setting.js b/src/background/repositories/setting.js
new file mode 100644
index 0000000..6d48525
--- /dev/null
+++ b/src/background/repositories/setting.js
@@ -0,0 +1,23 @@
+import MemoryStorage from '../infrastructures/memory-storage';
+
+const CACHED_SETTING_KEY = 'setting';
+
+export default class SettingRepository {
+ constructor() {
+ this.cache = new MemoryStorage();
+ }
+
+ get() {
+ return Promise.resolve(this.cache.get(CACHED_SETTING_KEY));
+ }
+
+ update(value) {
+ return this.cache.set(CACHED_SETTING_KEY, value);
+ }
+
+ async setProperty(name, value) {
+ let current = await this.get();
+ current.properties[name] = value;
+ return this.update(current);
+ }
+}
diff --git a/src/background/repositories/version.js b/src/background/repositories/version.js
new file mode 100644
index 0000000..4c71d05
--- /dev/null
+++ b/src/background/repositories/version.js
@@ -0,0 +1,10 @@
+export default class VersionRepository {
+ async get() {
+ let { version } = await browser.storage.local.get('version');
+ return version;
+ }
+
+ update(version) {
+ return browser.storage.local.set({ version });
+ }
+}