diff --git a/extension/background.js b/extension/background.js new file mode 100644 index 0000000..137d4bd --- /dev/null +++ b/extension/background.js @@ -0,0 +1,40 @@ +const getCorgiData = () => { + return new Promise((resolve) => { + chrome.storage.local.get(["corgi"], (data) => { + resolve(data?.corgi); + }); + }); +}; + +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?:\/\/go\/(.+)/; + +chrome.tabs.onUpdated.addListener(async (_, a, tab) => { + if (typeof tab.url === "string") { + const matchResult = urlRegex.exec(tab.url); + if (matchResult) { + const name = matchResult[1]; + const shortcut = await fetchShortcut(name); + if (shortcut && shortcut.link) { + chrome.tabs.update({ url: shortcut.link }); + } + } + } +}); + +chrome.omnibox.onInputEntered.addListener(async (text) => { + const shortcut = await fetchShortcut(text); + if (shortcut && shortcut.link) { + chrome.tabs.update({ url: shortcut.link }); + } +}); diff --git a/extension/manifest.json b/extension/manifest.json new file mode 100644 index 0000000..8dce49e --- /dev/null +++ b/extension/manifest.json @@ -0,0 +1,16 @@ +{ + "name": "Corgi", + "description": "Corgi is an URL manager.", + "version": "0.0.1", + "manifest_version": 3, + "omnibox": { + "keyword": "go" + }, + "action": { + "default_popup": "popup.html" + }, + "background": { + "service_worker": "background.js" + }, + "permissions": ["tabs", "activeTab", "storage"] +} diff --git a/extension/popup.html b/extension/popup.html new file mode 100644 index 0000000..54e1765 --- /dev/null +++ b/extension/popup.html @@ -0,0 +1,18 @@ + + +
+