mirror of
https://github.com/aykhans/slash-e.git
synced 2025-07-23 05:14:26 +00:00
chore: update extension
This commit is contained in:
@@ -1,40 +1,21 @@
|
||||
const getCorgiData = () => {
|
||||
return new Promise((resolve) => {
|
||||
chrome.storage.local.get(["corgi"], (data) => {
|
||||
resolve(data?.corgi);
|
||||
});
|
||||
});
|
||||
};
|
||||
import { getCorgiData } from "./common.js";
|
||||
|
||||
const fetchShortcut = async (name) => {
|
||||
const corgiData = await getCorgiData();
|
||||
if (corgiData.domain && corgiData.openId) {
|
||||
const res = await fetch(`${corgiData.domain}/api/shortcut?openId=${corgiData.openId}&name=${name}`);
|
||||
const { data } = await res.json();
|
||||
if (data.length > 0) {
|
||||
return data[0];
|
||||
}
|
||||
}
|
||||
};
|
||||
const urlRegex = /https?:\/\/o\/(.+)/;
|
||||
|
||||
const urlRegex = /https?:\/\/go\/(.+)/;
|
||||
|
||||
chrome.tabs.onUpdated.addListener(async (_1, _2, tab) => {
|
||||
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
|
||||
if (typeof tab.url === "string") {
|
||||
const matchResult = urlRegex.exec(tab.url);
|
||||
if (matchResult) {
|
||||
const corgiData = await getCorgiData();
|
||||
const name = matchResult[1];
|
||||
const shortcut = await fetchShortcut(name);
|
||||
if (shortcut && shortcut.link) {
|
||||
chrome.tabs.update({ url: shortcut.link });
|
||||
}
|
||||
const url = `${corgiData.domain}/api/shortcut?openId=${corgiData.openId}&name=${name}&redirect=true`;
|
||||
return chrome.tabs.update({ url });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
chrome.omnibox.onInputEntered.addListener(async (text) => {
|
||||
const shortcut = await fetchShortcut(text);
|
||||
if (shortcut && shortcut.link) {
|
||||
chrome.tabs.update({ url: shortcut.link });
|
||||
}
|
||||
const corgiData = await getCorgiData();
|
||||
const url = `${corgiData.domain}/api/shortcut?openId=${corgiData.openId}&name=${text}&redirect=true`;
|
||||
return chrome.tabs.update({ url });
|
||||
});
|
||||
|
Reference in New Issue
Block a user