From 1cbab78989a9ba2f8381955d15a7aec427512d52 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 11 Jul 2023 08:37:27 +0800 Subject: [PATCH] feat: add chrome extension --- .gitignore | 2 -- extension/background.js | 21 +++++++++++++++++++++ extension/common.js | 11 +++++++++++ extension/manifest.json | 18 ++++++++++++++++++ extension/popup.html | 14 ++++++++++++++ extension/popup.js | 23 +++++++++++++++++++++++ 6 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 extension/background.js create mode 100644 extension/common.js create mode 100644 extension/manifest.json create mode 100644 extension/popup.html create mode 100644 extension/popup.js diff --git a/.gitignore b/.gitignore index db92a3e..99d0edd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,3 @@ web/dist build .DS_Store - -extension diff --git a/extension/background.js b/extension/background.js new file mode 100644 index 0000000..fc15c2d --- /dev/null +++ b/extension/background.js @@ -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 }); +}); diff --git a/extension/common.js b/extension/common.js new file mode 100644 index 0000000..124a240 --- /dev/null +++ b/extension/common.js @@ -0,0 +1,11 @@ +export const getShortifyData = () => { + return new Promise((resolve, reject) => { + chrome.storage.local.get(["shortify"], (data) => { + if (data?.shortify) { + resolve(data.shortify); + } else { + reject("shortify data not found"); + } + }); + }); +}; diff --git a/extension/manifest.json b/extension/manifest.json new file mode 100644 index 0000000..03eed63 --- /dev/null +++ b/extension/manifest.json @@ -0,0 +1,18 @@ +{ + "name": "Shortify", + "description": "", + "version": "0.1.0", + "manifest_version": 3, + "omnibox": { + "keyword": "s/" + }, + "action": { + "default_popup": "popup.html" + }, + "background": { + "service_worker": "background.js", + "type": "module" + }, + "permissions": ["tabs", "activeTab", "storage"], + "host_permissions": ["*://s/*"] +} diff --git a/extension/popup.html b/extension/popup.html new file mode 100644 index 0000000..77907c4 --- /dev/null +++ b/extension/popup.html @@ -0,0 +1,14 @@ + + + +

Shortify extension

+
+ Domain + +
+
+ +
+ + + diff --git a/extension/popup.js b/extension/popup.js new file mode 100644 index 0000000..3de7fd3 --- /dev/null +++ b/extension/popup.js @@ -0,0 +1,23 @@ +import { getShortifyData } from "./common.js"; + +const saveButton = document.body.querySelector("#save-button"); +const domainInput = document.body.querySelector("#domain-input"); + +saveButton.addEventListener("click", () => { + chrome.storage.local.set({ + shortify: { + domain: domainInput.value, + }, + }); +}); + +(async () => { + try { + const shortifyData = await getShortifyData(); + if (shortifyData) { + domainInput.value = shortifyData.domain; + } + } catch (error) { + // do nothing. + } +})();