From 137b2f3d4eda37e00ad4fd09cb53d688de76acfe Mon Sep 17 00:00:00 2001 From: Patrick Pichler Date: Sun, 29 Oct 2023 15:25:50 +0100 Subject: [PATCH] feat: add custom search-engine to query shiori (#59) It is now possible to query Shiori directly from the firefox search bar by simply prefixing the query with `@shiori`. This will trigger a shiori keyword search. It is also possible to search by tags, by prefixing words with `#`. fixes: #58 Co-authored-by: Patrick Pichler --- js/background-script.js | 85 +++++++++++++++++++++++++++++++++++++++++ manifest.json | 4 ++ 2 files changed, 89 insertions(+) diff --git a/js/background-script.js b/js/background-script.js index 37f2276..2e1cf0e 100644 --- a/js/background-script.js +++ b/js/background-script.js @@ -301,3 +301,88 @@ browser.tabs.onUpdated.addListener(updateActiveTab); browser.tabs.onActivated.addListener(updateActiveTab); browser.windows.onFocusChanged.addListener(updateActiveTab); updateActiveTab(); + +if (browser.omnibox) { + browser.omnibox.setDefaultSuggestion({ + description: 'Search for stored bookmarks' + }); + + browser.omnibox.onInputChanged.addListener((text, addSuggestions) => { + var data = text.split(" ").reduce((prev, curr) => { + if (curr[0] == "#") { + return { ...prev, tags: [...prev.tags, curr.substring(1)] } + } + + if (curr[0] == "!") { + return { ...prev, excludedTags: [...prev.excludedTags, curr.substring(1)] } + } + + return { ...prev, keyword: [prev.keyword, curr].join(" ") } + + }, { tags: [], excludedTags: [], keyword: "" }); + + return retreiveBookmarks(data.tags, data.excludedTags, data.keyword) + .then(addSuggestions) + }); + + async function retreiveBookmarks(tags, excludedTags, keyword) { + var tagValue = tags.join(",") + var excludedTagValue = excludedTags.join(",") + var config = await getExtensionConfig(); + + // Create API URL + var apiURL = ""; + try { + var api = new URL(config.server); + if (api.pathname.slice(-1) == "/") { + api.pathname = api.pathname + "api/bookmarks"; + } else { + api.pathname = api.pathname + "/api/bookmarks"; + } + api.searchParams.set("keyword", keyword) + api.searchParams.set("tags", tagValue) + api.searchParams.set("exclude", excludedTagValue) + apiURL = api.toString(); + } catch (err) { + throw new Error(`${config.server} is not a valid url`); + } + + var response = await fetch(apiURL, { + method: "GET", + headers: { + "Content-Type": "application/json", + "X-Session-Id": config.session, + } + }); + + if (!response.ok) { + var err = await response.text(); + throw new Error(err); + } + + return response.json() + .then(v => { + return v.bookmarks.map(b => ({ + content: b.url, + description: b.title + })) + } + ); + } + + browser.omnibox.onInputEntered.addListener((text, disposition) => { + let url = text; + + switch (disposition) { + case "currentTab": + browser.tabs.update({ url }); + break; + case "newForegroundTab": + browser.tabs.create({ url }); + break; + case "newBackgroundTab": + browser.tabs.create({ url, active: false }); + break; + } + }); +} diff --git a/manifest.json b/manifest.json index 395db7f..5aabb35 100644 --- a/manifest.json +++ b/manifest.json @@ -76,5 +76,9 @@ "gecko": { "id": "{8547b985-a7a0-408d-82d1-d185f8be1ff5}" } + }, + + "omnibox": { + "keyword": "@shiori" } }