feat: add chrome extension

This commit is contained in:
Steven
2023-07-11 08:37:27 +08:00
parent 28df6e35fb
commit 1cbab78989
6 changed files with 87 additions and 2 deletions

21
extension/background.js Normal file
View File

@@ -0,0 +1,21 @@
import { getShortifyData } from "./common.js";
const urlRegex = /https?:\/\/s\/(.+)/;
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (typeof tab.url === "string") {
const matchResult = urlRegex.exec(tab.url);
if (matchResult) {
const shortifyData = await getShortifyData();
const name = matchResult[1];
const url = `${shortifyData.domain}/s/${name}`;
return chrome.tabs.update({ url });
}
}
});
chrome.omnibox.onInputEntered.addListener(async (text) => {
const shortifyData = await getShortifyData();
const url = `${shortifyData.domain}/s/${text}`;
return chrome.tabs.update({ url });
});