initial commit

This commit is contained in:
agatha 2024-04-13 18:41:22 -04:00
commit bee1f61d8f
9 changed files with 105 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.idea
venv
__pycache__
*.py[cod]

0
README.md Normal file
View File

2
extension/README.md Normal file
View File

@ -0,0 +1,2 @@
# Unshorten
A Firefox extension to unshorten links from sites like Twitter.

View File

@ -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);
}
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 B

View File

@ -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"
]
}

2
server/README.md Normal file
View File

@ -0,0 +1,2 @@
# Unshorten Server
Simple FastAPI app to unshorten URLs.

2
server/requirements.txt Normal file
View File

@ -0,0 +1,2 @@
fastapi
uvicorn[standard]

29
server/src/main.py Normal file
View File

@ -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}"}