mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
chore: update extension with web request listener
This commit is contained in:
parent
53df3a9c1c
commit
f0afa13b8d
@ -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",
|
||||
@ -50,11 +50,15 @@
|
||||
},
|
||||
"manifest": {
|
||||
"omnibox": {
|
||||
"keyword": "s"
|
||||
"keyword": "s/"
|
||||
},
|
||||
"permissions": [
|
||||
"tabs",
|
||||
"storage"
|
||||
"activeTab",
|
||||
"storage",
|
||||
"webRequest"
|
||||
],
|
||||
"host_permissions": [
|
||||
"*://*/*"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -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) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user