adding browser extension and some QoL
This commit is contained in:
242
browser-extension/background.js
Normal file
242
browser-extension/background.js
Normal file
@@ -0,0 +1,242 @@
|
||||
// f0ckm Uploader - Background Service Worker
|
||||
|
||||
const DEFAULT_SETTINGS = {
|
||||
uploadMode: "desktop", // "desktop" (f0ckm://) or "direct" (API)
|
||||
apiUrl: "https://f0ckm.com/api/v2/upload",
|
||||
apiKey: "",
|
||||
rating: "",
|
||||
tags: "",
|
||||
visibility: "0",
|
||||
isOc: false,
|
||||
urlType: "post"
|
||||
};
|
||||
|
||||
async function getSettings() {
|
||||
return new Promise((resolve) => {
|
||||
chrome.storage.sync.get(DEFAULT_SETTINGS, (items) => {
|
||||
resolve(items || DEFAULT_SETTINGS);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const badgeCache = {};
|
||||
|
||||
function updateBadge(tabId, url) {
|
||||
if (!tabId) return;
|
||||
const isWeb = url && (url.startsWith("http://") || url.startsWith("https://"));
|
||||
const isYT = isWeb && (url.includes("youtube.com") || url.includes("youtu.be"));
|
||||
const targetText = isWeb ? "1" : "";
|
||||
const targetColor = isYT ? "#ff0000" : "#0066ff";
|
||||
|
||||
const cacheKey = `${targetText}:${targetColor}`;
|
||||
if (badgeCache[tabId] === cacheKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
badgeCache[tabId] = cacheKey;
|
||||
|
||||
try {
|
||||
if (targetText) {
|
||||
chrome.action.setBadgeText({ text: targetText, tabId: tabId });
|
||||
chrome.action.setBadgeBackgroundColor({ color: targetColor, tabId: tabId });
|
||||
} else {
|
||||
chrome.action.setBadgeText({ text: "", tabId: tabId });
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
chrome.tabs.onActivated.addListener(async (activeInfo) => {
|
||||
try {
|
||||
const tab = await chrome.tabs.get(activeInfo.tabId);
|
||||
if (tab) updateBadge(activeInfo.tabId, tab.url);
|
||||
} catch (e) {}
|
||||
});
|
||||
|
||||
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||
if (tab && (changeInfo.status === "complete" || changeInfo.url)) {
|
||||
updateBadge(tabId, tab.url);
|
||||
}
|
||||
});
|
||||
|
||||
function setupContextMenus() {
|
||||
chrome.contextMenus.removeAll(() => {
|
||||
chrome.contextMenus.create({
|
||||
id: "f0ckm_upload_image",
|
||||
title: "Upload image to f0ckm",
|
||||
contexts: ["image"]
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: "f0ckm_upload_video",
|
||||
title: "Upload media to f0ckm",
|
||||
contexts: ["video", "audio"]
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: "f0ckm_upload_link",
|
||||
title: "Upload link with f0ckm",
|
||||
contexts: ["link"]
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: "f0ckm_upload_selection",
|
||||
title: "Upload selected URL with f0ckm",
|
||||
contexts: ["selection"]
|
||||
});
|
||||
chrome.contextMenus.create({
|
||||
id: "f0ckm_upload_page",
|
||||
title: "Upload page URL to f0ckm",
|
||||
contexts: ["page"]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
setupContextMenus();
|
||||
});
|
||||
|
||||
chrome.runtime.onStartup.addListener(() => {
|
||||
setupContextMenus();
|
||||
});
|
||||
|
||||
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||
let targetUrl = "";
|
||||
if (info.menuItemId === "f0ckm_upload_image" || info.menuItemId === "f0ckm_upload_video") {
|
||||
targetUrl = info.srcUrl || info.linkUrl;
|
||||
} else if (info.menuItemId === "f0ckm_upload_link") {
|
||||
targetUrl = info.linkUrl;
|
||||
} else if (info.menuItemId === "f0ckm_upload_selection") {
|
||||
targetUrl = info.selectionText ? info.selectionText.trim() : "";
|
||||
} else if (info.menuItemId === "f0ckm_upload_page") {
|
||||
targetUrl = info.pageUrl || (tab ? tab.url : "");
|
||||
}
|
||||
|
||||
if (targetUrl) {
|
||||
await processUpload(targetUrl);
|
||||
}
|
||||
});
|
||||
|
||||
chrome.commands.onCommand.addListener(async (command) => {
|
||||
if (command === "upload-page") {
|
||||
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
|
||||
if (tabs && tabs.length > 0 && tabs[0].url) {
|
||||
await processUpload(tabs[0].url);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
async function processUpload(targetUrl) {
|
||||
if (!targetUrl) return;
|
||||
|
||||
const settings = await getSettings();
|
||||
|
||||
if (settings.uploadMode === "desktop") {
|
||||
// 1. Try local loopback HTTP API first (Direct desktop handoff)
|
||||
const localApiUrl = "http://127.0.0.1:18739/upload?url=" + encodeURIComponent(targetUrl);
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 1500);
|
||||
const res = await fetch(localApiUrl, { signal: controller.signal });
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (res.ok) {
|
||||
return;
|
||||
}
|
||||
} catch (err) {}
|
||||
|
||||
// 2. Fallback: Custom protocol scheme in active tab
|
||||
const customUrl = "f0ckm://upload?url=" + encodeURIComponent(targetUrl);
|
||||
try {
|
||||
chrome.tabs.create({ url: customUrl, active: true }, (newTab) => {
|
||||
if (newTab && newTab.id) {
|
||||
setTimeout(() => {
|
||||
chrome.tabs.remove(newTab.id).catch(() => {});
|
||||
}, 4500);
|
||||
}
|
||||
});
|
||||
} catch (e) {}
|
||||
} else {
|
||||
// Direct API Upload mode -> POST JSON payload with url to f0ckm API!
|
||||
showNotification("Uploading...", `Uploading ${targetUrl.substring(0, 45)}...`, "uploading");
|
||||
|
||||
try {
|
||||
if (!settings.apiUrl || !settings.apiKey) {
|
||||
showNotification("Upload Error", "API URL and API Key must be configured in extension options.", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
url: targetUrl,
|
||||
tags: settings.tags || "url,upload",
|
||||
visibility: settings.visibility || "0"
|
||||
};
|
||||
|
||||
if (settings.rating && settings.rating !== "default") {
|
||||
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, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-Api-Key": settings.apiKey
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
showNotification("Upload Successful!", `Link: ${resultUrl}`, "success");
|
||||
return resultUrl;
|
||||
} else {
|
||||
const msg = json.msg || "Upload failed";
|
||||
showNotification("Upload Failed", msg, "error");
|
||||
}
|
||||
} catch (err) {
|
||||
showNotification("Upload Error", err.message || "An error occurred", "error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function showNotification(title, message, iconType = "info") {
|
||||
if (chrome.notifications) {
|
||||
chrome.notifications.create({
|
||||
type: "basic",
|
||||
iconUrl: "icons/icon128.png",
|
||||
title: title,
|
||||
message: message,
|
||||
priority: 2
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.action === "get_active_tab") {
|
||||
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
|
||||
if (tabs && tabs.length > 0 && tabs[0].url) {
|
||||
sendResponse({ tab: tabs[0] });
|
||||
} else {
|
||||
chrome.tabs.query({ active: true, currentWindow: true }, (tabs2) => {
|
||||
if (tabs2 && tabs2.length > 0 && tabs2[0].url) {
|
||||
sendResponse({ tab: tabs2[0] });
|
||||
} else {
|
||||
chrome.tabs.query({ active: true }, (tabs3) => {
|
||||
sendResponse({ tab: (tabs3 && tabs3.length > 0) ? tabs3[0] : null });
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} else if (request.action === "upload_url") {
|
||||
processUpload(request.url).then((resUrl) => {
|
||||
sendResponse({ status: "ok", resultUrl: resUrl || "" });
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user