Skip to content

Commit

Permalink
feat: add custom search-engine to query shiori (#59)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
patrickpichler and Patrick Pichler authored Oct 29, 2023
1 parent 50b0d38 commit 137b2f3
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
85 changes: 85 additions & 0 deletions js/background-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}
4 changes: 4 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,9 @@
"gecko": {
"id": "{8547b985-a7a0-408d-82d1-d185f8be1ff5}"
}
},

"omnibox": {
"keyword": "@shiori"
}
}

0 comments on commit 137b2f3

Please sign in to comment.