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",
|
"name": "slash-extension",
|
||||||
"displayName": "Slash",
|
"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.",
|
"description": "An open source, self-hosted bookmarks and link sharing platform. Save and share your links very easily.",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "plasmo dev",
|
"dev": "plasmo dev",
|
||||||
@ -50,11 +50,15 @@
|
|||||||
},
|
},
|
||||||
"manifest": {
|
"manifest": {
|
||||||
"omnibox": {
|
"omnibox": {
|
||||||
"keyword": "s"
|
"keyword": "s/"
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"tabs",
|
"activeTab",
|
||||||
"storage"
|
"storage",
|
||||||
|
"webRequest"
|
||||||
|
],
|
||||||
|
"host_permissions": [
|
||||||
|
"*://*/*"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,29 +4,40 @@ import type { Shortcut } from "@/types/proto/api/v2/shortcut_service";
|
|||||||
const storage = new Storage();
|
const storage = new Storage();
|
||||||
const urlRegex = /https?:\/\/s\/(.+)/;
|
const urlRegex = /https?:\/\/s\/(.+)/;
|
||||||
|
|
||||||
chrome.tabs.onUpdated.addListener(async (tabId, _, tab) => {
|
chrome.webRequest.onBeforeRequest.addListener(
|
||||||
if (!tab.url) {
|
(param) => {
|
||||||
return;
|
(async () => {
|
||||||
}
|
if (!param.url) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const shortcutName = getShortcutNameFromUrl(tab.url);
|
const shortcutName = getShortcutNameFromUrl(param.url);
|
||||||
if (shortcutName) {
|
if (shortcutName) {
|
||||||
const shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
|
const shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
|
||||||
const shortcut = shortcuts.find((shortcut) => shortcut.name === shortcutName);
|
const shortcut = shortcuts.find((shortcut) => shortcut.name === shortcutName);
|
||||||
if (!shortcut) {
|
if (!shortcut) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return chrome.tabs.update(tabId, { url: shortcut.link });
|
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 shortcuts = (await storage.getItem<Shortcut[]>("shortcuts")) || [];
|
||||||
const shortcut = shortcuts.find((shortcut) => shortcut.name === text);
|
const shortcut = shortcuts.find((shortcut) => shortcut.name === text);
|
||||||
if (!shortcut) {
|
if (!shortcut) {
|
||||||
return;
|
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) => {
|
const getShortcutNameFromUrl = (urlString: string) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user