update browser extension and app

This commit is contained in:
2026-08-13 17:21:58 +02:00
parent 7e723ae821
commit 9050faf0a4
14 changed files with 809 additions and 97 deletions

View File

@@ -122,6 +122,73 @@ chrome.commands.onCommand.addListener(async (command) => {
}
});
function normalizeApiUrl(url) {
if (!url) return "";
url = url.trim().replace(/^['"]|['"]$/g, "");
if (!url) return "";
if (!/^https?:\/\//i.test(url)) {
url = "http://" + url;
}
try {
const parsed = new URL(url);
let path = parsed.pathname.replace(/\/+$/, "");
if (!path || path === "") {
return `${parsed.origin}/api/v2/upload`;
} else if (path.endsWith("/api/v2")) {
return `${parsed.origin}/upload`;
} else if (!path.endsWith("/upload")) {
return `${parsed.origin}/api/v2/upload`;
}
return url;
} catch (e) {
return url;
}
}
async function copyToClipboard(text) {
if (!text) return;
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
await navigator.clipboard.writeText(text);
}
} catch (e) {}
}
async function testConnection(apiUrl, apiKey) {
if (!apiUrl || !apiKey) {
return { success: false, msg: "API URL and API Key are required." };
}
const normUrl = normalizeApiUrl(apiUrl);
try {
const res = await fetch(normUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Api-Key": apiKey,
"Authorization": `Bearer ${apiKey}`
},
credentials: "omit",
body: JSON.stringify({ api_key: apiKey, key: apiKey })
});
if (res.status === 200) {
return { success: true, msg: "Connection successful! (HTTP 200)" };
} else if (res.status === 401) {
return { success: false, msg: "Unauthorized: Invalid API Key." };
} else if (res.status === 400 || res.status === 422) {
return { success: true, msg: "Connection successful! (API key verified)" };
} else {
let text = `HTTP ${res.status}`;
try {
const json = await res.json();
if (json.msg || json.error || json.message) text = json.msg || json.error || json.message;
} catch (e) {}
return { success: false, msg: text };
}
} catch (err) {
return { success: false, msg: err.message || "Connection failed. Check server URL and network." };
}
}
async function processUpload(targetUrl) {
if (!targetUrl) return;
@@ -160,45 +227,69 @@ async function processUpload(targetUrl) {
try {
if (!settings.apiUrl || !settings.apiKey) {
showNotification("Upload Error", "API URL and API Key must be configured in extension options.", "error");
return;
return null;
}
const normalizedUrl = normalizeApiUrl(settings.apiUrl);
const payload = {
url: targetUrl,
tags: settings.tags || "url,upload",
visibility: settings.visibility || "0"
visibility: settings.visibility || "0",
api_key: settings.apiKey,
key: settings.apiKey
};
if (settings.rating && settings.rating !== "default") {
if (settings.rating && settings.rating !== "default" && settings.rating !== "") {
const ratingMap = { s: "sfw", q: "nsfw", e: "nsfl", sfw: "sfw", nsfw: "nsfw", nsfl: "nsfl" };
payload.rating = ratingMap[settings.rating.toLowerCase()] || settings.rating;
}
if (settings.isOc) payload.is_oc = "1";
const uploadRes = await fetch(settings.apiUrl, {
const uploadRes = await fetch(normalizedUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Api-Key": settings.apiKey
"X-Api-Key": settings.apiKey,
"Authorization": `Bearer ${settings.apiKey}`
},
credentials: "omit",
body: JSON.stringify(payload)
});
if (!uploadRes.ok) {
let errorMsg = `HTTP Error ${uploadRes.status}`;
try {
const errJson = await uploadRes.json();
if (errJson.msg || errJson.error || errJson.message) {
errorMsg = errJson.msg || errJson.error || errJson.message;
}
} catch (e) {}
showNotification("Upload Failed", errorMsg, "error");
return null;
}
const json = await uploadRes.json();
if (json.success && (json.url || json.file_url || json.direct_url || json.target_url || json.file)) {
const postUrl = json.url || json.post_url || "";
const directUrl = json.file_url || json.direct_url || json.target_url || json.file || "";
const resultUrl = (settings.urlType === "direct" && directUrl) ? directUrl : (postUrl || directUrl);
const postUrl = json.url || json.post_url || json.link || (json.data && (json.data.url || json.data.post_url)) || "";
const directUrl = json.file_url || json.direct_url || json.target_url || json.file || (json.data && (json.data.file_url || json.data.direct_url)) || "";
const resultUrl = (settings.urlType === "direct" && directUrl) ? directUrl : (postUrl || directUrl);
showNotification("Upload Successful!", `Link: ${resultUrl}`, "success");
if (resultUrl) {
await copyToClipboard(resultUrl);
showNotification("Upload Successful!", `Link copied to clipboard: ${resultUrl}`, "success");
return resultUrl;
} else if (json.success) {
showNotification("Upload Successful!", "Upload processed successfully.", "success");
return "success";
} else {
const msg = json.msg || "Upload failed";
const msg = json.msg || json.error || "Upload failed";
showNotification("Upload Failed", msg, "error");
return null;
}
} catch (err) {
showNotification("Upload Error", err.message || "An error occurred", "error");
showNotification("Upload Error", err.message || "An error occurred during network request", "error");
return null;
}
}
}
@@ -238,5 +329,10 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
sendResponse({ status: "ok", resultUrl: resUrl || "" });
});
return true;
} else if (request.action === "test_connection") {
testConnection(request.apiUrl, request.apiKey).then((res) => {
sendResponse(res);
});
return true;
}
});