Compare commits

...

2 Commits

Author SHA1 Message Date
a655b3b02e unshortener copies to clipboard 2024-04-13 21:46:51 -04:00
ca2c990002 implement in memory cache 2024-04-13 21:44:41 -04:00
3 changed files with 16 additions and 6 deletions

View File

@ -28,8 +28,6 @@ function getDomainFromUrl(linkUrl) {
} }
function unshortenUrl(linkUrl) { function unshortenUrl(linkUrl) {
console.log(linkUrl);
fetch("http://localhost:8000/?url=" + linkUrl) fetch("http://localhost:8000/?url=" + linkUrl)
.then(res => { .then(res => {
if (!res.ok) { if (!res.ok) {
@ -37,7 +35,9 @@ function unshortenUrl(linkUrl) {
} }
return res.json(); return res.json();
}) })
.then(data => { .then(unshortenedUrl => {
console.log(data); console.log("unshortened: " + unshortenedUrl)
navigator.clipboard.writeText(unshortenedUrl)
.catch(err => console.error("couldn't copy to clipboard", err));
}); });
} }

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Unshortener", "name": "Unshortener",
"version": "0.2", "version": "0.3",
"description": "Unshorten links from Twitter.", "description": "Unshorten links from Twitter.",
@ -17,6 +17,8 @@
}, },
"permissions": [ "permissions": [
"activeTab",
"clipboardWrite",
"contextMenus" "contextMenus"
] ]
} }

View File

@ -9,6 +9,8 @@ UNSHORTEN = {
't.co': unshorten_twitter 't.co': unshorten_twitter
} }
CACHE = {}
app = FastAPI(docs_url=None, redoc_url=None) app = FastAPI(docs_url=None, redoc_url=None)
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
@ -29,4 +31,10 @@ async def receive_url(url: Optional[str] = None):
if domain not in UNSHORTEN: if domain not in UNSHORTEN:
return {"error": f"cannot unshorten {domain}"} return {"error": f"cannot unshorten {domain}"}
return UNSHORTEN[domain](url) if url in CACHE:
unshortened = CACHE[url]
else:
unshortened = UNSHORTEN[domain](url)
CACHE[url] = unshortened
return unshortened