commit bee1f61d8f7c47cab0b11cd5ae9ddfa42326d15f Author: agatha Date: Sat Apr 13 18:41:22 2024 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f380988 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.idea +venv + +__pycache__ +*.py[cod] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/extension/README.md b/extension/README.md new file mode 100644 index 0000000..c5dc95f --- /dev/null +++ b/extension/README.md @@ -0,0 +1,2 @@ +# Unshorten +A Firefox extension to unshorten links from sites like Twitter. \ No newline at end of file diff --git a/extension/unshorten/background.js b/extension/unshorten/background.js new file mode 100644 index 0000000..22203cc --- /dev/null +++ b/extension/unshorten/background.js @@ -0,0 +1,41 @@ +const shortenerDomains = [ + "t.co" +]; + +browser.contextMenus.create({ + id: "unshorten-link", + title: "Unshorten link", + contexts: ["link"], + }, + // See https://extensionworkshop.com/documentation/develop/manifest-v3-migration-guide/#event-pages-and-backward-compatibility + // for information on the purpose of this error capture. + () => void browser.runtime.lastError, +); + +browser.contextMenus.onClicked.addListener((info, tab) => { + if (info.menuItemId === "unshorten-link") { + const linkUrl = info.linkUrl; + const linkDomain = getDomainFromUrl(linkUrl); + const canShorten = shortenerDomains.includes(linkDomain); + + if (canShorten) { + unshortenUrl(linkUrl); + } + } +}); + +function getDomainFromUrl(linkUrl) { + const url = new URL(linkUrl); + return url.hostname; +} + +function unshortenUrl(linkUrl) { + console.log(linkUrl); + + fetch("http://localhost:8000/?url=" + linkUrl) + .then(res => { + if (res.ok) { + console.log(res); + } + }); +} \ No newline at end of file diff --git a/extension/unshorten/icons/border-48.png b/extension/unshorten/icons/border-48.png new file mode 100644 index 0000000..90687de Binary files /dev/null and b/extension/unshorten/icons/border-48.png differ diff --git a/extension/unshorten/manifest.json b/extension/unshorten/manifest.json new file mode 100644 index 0000000..906f51e --- /dev/null +++ b/extension/unshorten/manifest.json @@ -0,0 +1,23 @@ +{ + "manifest_version": 2, + "name": "Unshorten", + "version": "0.1", + + "description": "Unshorten links from Twitter.", + + "icons": { + "48": "icons/border-48.png" + }, + + "background": { + "scripts": [ + "background.js" + ], + "persistent": false + }, + + "permissions": [ + "activeTab", + "contextMenus" + ] +} diff --git a/server/README.md b/server/README.md new file mode 100644 index 0000000..a9982bb --- /dev/null +++ b/server/README.md @@ -0,0 +1,2 @@ +# Unshorten Server +Simple FastAPI app to unshorten URLs. \ No newline at end of file diff --git a/server/requirements.txt b/server/requirements.txt new file mode 100644 index 0000000..8e0578a --- /dev/null +++ b/server/requirements.txt @@ -0,0 +1,2 @@ +fastapi +uvicorn[standard] \ No newline at end of file diff --git a/server/src/main.py b/server/src/main.py new file mode 100644 index 0000000..0682abe --- /dev/null +++ b/server/src/main.py @@ -0,0 +1,29 @@ +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from typing import Optional +from urllib.parse import urlparse + +SHORTEN_DOMAINS = [ + 't.co' +] + +app = FastAPI(docs_url=None, redoc_url=None) + +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["GET"] +) + + +@app.get('/') +async def shorten_url(url: Optional[str] = None): + if url is None: + return {"error": "no url provided"} + + domain = urlparse(url).netloc + + if domain in SHORTEN_DOMAINS: + return {"result": "shortened url"} + else: + return {"error": f"cannot shorten {url}"}