initial commit
This commit is contained in:
commit
bee1f61d8f
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
.idea
|
||||
venv
|
||||
|
||||
__pycache__
|
||||
*.py[cod]
|
||||
|
2
extension/README.md
Normal file
2
extension/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# Unshorten
|
||||
A Firefox extension to unshorten links from sites like Twitter.
|
41
extension/unshorten/background.js
Normal file
41
extension/unshorten/background.js
Normal 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);
|
||||
}
|
||||
});
|
||||
}
|
BIN
extension/unshorten/icons/border-48.png
Normal file
BIN
extension/unshorten/icons/border-48.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 225 B |
23
extension/unshorten/manifest.json
Normal file
23
extension/unshorten/manifest.json
Normal 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
2
server/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# Unshorten Server
|
||||
Simple FastAPI app to unshorten URLs.
|
2
server/requirements.txt
Normal file
2
server/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
fastapi
|
||||
uvicorn[standard]
|
29
server/src/main.py
Normal file
29
server/src/main.py
Normal 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}"}
|
Loading…
Reference in New Issue
Block a user