Skip to content

Commit

Permalink
chore: update extension with web request listener
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Nov 9, 2023
1 parent 53df3a9 commit f0afa13
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
12 changes: 8 additions & 4 deletions frontend/extension/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slash-extension",
"displayName": "Slash",
"version": "1.0.0",
"version": "1.0.1",
"description": "An open source, self-hosted bookmarks and link sharing platform. Save and share your links very easily.",
"scripts": {
"dev": "plasmo dev",
Expand Down Expand Up @@ -50,11 +50,15 @@
},
"manifest": {
"omnibox": {
"keyword": "s"
"keyword": "s/"
},
"permissions": [
"tabs",
"storage"
"activeTab",
"storage",
"webRequest"
],
"host_permissions": [
"*://*/*"
]
}
}
43 changes: 27 additions & 16 deletions frontend/extension/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,40 @@ import type { Shortcut } from "@/types/proto/api/v2/shortcut_service";
const storage = new Storage();
const urlRegex = /https?:\/\/s\/(.+)/;

chrome.tabs.onUpdated.addListener(async (tabId, _, tab) => {
if (!tab.url) {
return;
}
chrome.webRequest.onBeforeRequest.addListener(
(param) => {
(async () => {
if (!param.url) {
return;
}

const shortcutName = getShortcutNameFromUrl(tab.url);
if (shortcutName) {
const shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
const shortcut = shortcuts.find((shortcut) => shortcut.name === shortcutName);
if (!shortcut) {
return;
}
return chrome.tabs.update(tabId, { url: shortcut.link });
}
});
const shortcutName = getShortcutNameFromUrl(param.url);
if (shortcutName) {
const shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
const shortcut = shortcuts.find((shortcut) => shortcut.name === shortcutName);
if (!shortcut) {
return;
}
return chrome.tabs.update({ url: shortcut.link });
}
})();
},
{ urls: ["*://s/*", "*://*/search*"] }
);

chrome.omnibox.onInputEntered.addListener(async (text) => {
chrome.omnibox.onInputEntered.addListener(async (text, disposition) => {
const shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
const shortcut = shortcuts.find((shortcut) => shortcut.name === text);
if (!shortcut) {
return;
}
return chrome.tabs.update({ url: shortcut.link });
if (disposition === "currentTab") {
chrome.tabs.update({ url: shortcut.link });
} else if (disposition === "newForegroundTab") {
chrome.tabs.create({ url: shortcut.link });
} else if (disposition === "newBackgroundTab") {
chrome.tabs.create({ url: shortcut.link, active: false });
}
});

const getShortcutNameFromUrl = (urlString: string) => {
Expand Down

0 comments on commit f0afa13

Please sign in to comment.