mirror of
https://github.com/andwati/kget-capture.git
synced 2026-10-23 05:22:19 +00:00
Ctrl+S on a page you navigated to directly via file:// is still a "download" as far as chrome.downloads is concerned, so auto-capture was intercepting it and handing the local path to KGet, which rejects it with a confusing "protocol is not supported" dialog since it's a network download manager, not a file copier. Extend the existing blob:/data: exclusion to also cover file:/filesystem:, and apply the same guard to the right-click capture path.
113 lines
3.8 KiB
JavaScript
113 lines
3.8 KiB
JavaScript
const NATIVE_HOST = "com.kget_capture.host";
|
|
const MENU_ID = "kget-capture-download";
|
|
|
|
function updateBadge(enabled) {
|
|
chrome.action.setBadgeText({ text: enabled ? "ON" : "OFF" });
|
|
chrome.action.setBadgeBackgroundColor({ color: enabled ? "#2e7d32" : "#9e9e9e" });
|
|
}
|
|
|
|
async function refreshBadge() {
|
|
const { autoCaptureEnabled } = await chrome.storage.local.get({ autoCaptureEnabled: true });
|
|
updateBadge(autoCaptureEnabled);
|
|
}
|
|
|
|
// KGet is a network download manager -- it can't "download" a URL that's
|
|
// already local (or embedded in the page), so don't hand it these schemes.
|
|
// file: shows up when saving a page you navigated to directly (Ctrl+S on a
|
|
// file:// URL is still a "download" as far as chrome.downloads is
|
|
// concerned), and KGet's addTransfer rejects it with a confusing
|
|
// "protocol is not supported" dialog if we try.
|
|
const UNSUPPORTED_SCHEMES = ["blob:", "data:", "file:", "filesystem:"];
|
|
|
|
function isCapturableUrl(url) {
|
|
return !UNSUPPORTED_SCHEMES.some((scheme) => url.startsWith(scheme));
|
|
}
|
|
|
|
function sendToKGet(url, filename) {
|
|
chrome.runtime.sendNativeMessage(
|
|
NATIVE_HOST,
|
|
{ action: "addTransfer", url, filename: filename || null },
|
|
(response) => {
|
|
if (chrome.runtime.lastError) {
|
|
console.error("[kget-capture] native host error:", chrome.runtime.lastError.message);
|
|
return;
|
|
}
|
|
if (!response || !response.ok) {
|
|
console.error("[kget-capture] KGet reported failure:", response && response.error);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
chrome.runtime.onInstalled.addListener(async (details) => {
|
|
chrome.contextMenus.create({
|
|
id: MENU_ID,
|
|
title: "Download with KGet",
|
|
contexts: ["link", "image", "video", "audio"],
|
|
});
|
|
|
|
if (details.reason === "install") {
|
|
await chrome.storage.local.set({ autoCaptureEnabled: true });
|
|
}
|
|
refreshBadge();
|
|
});
|
|
|
|
chrome.contextMenus.onClicked.addListener((info) => {
|
|
if (info.menuItemId !== MENU_ID) return;
|
|
const url = info.linkUrl || info.srcUrl;
|
|
if (!url) return;
|
|
if (!isCapturableUrl(url)) {
|
|
console.warn("[kget-capture] cannot hand a local/embedded URL to KGet:", url);
|
|
return;
|
|
}
|
|
sendToKGet(url, null);
|
|
});
|
|
|
|
// onDeterminingFilename (rather than onCreated) fires once Chrome has
|
|
// already resolved the real filename (from Content-Disposition or the URL),
|
|
// which is exactly what we need to hand KGet the correct name instead of an
|
|
// opaque URL path segment.
|
|
chrome.downloads.onDeterminingFilename.addListener((item, suggest) => {
|
|
// We're about to cancel this download regardless, so Chrome's own choice
|
|
// doesn't matter -- just let it proceed so the event resolves promptly.
|
|
suggest();
|
|
captureDownload(item);
|
|
});
|
|
|
|
async function captureDownload(item) {
|
|
const { autoCaptureEnabled } = await chrome.storage.local.get({ autoCaptureEnabled: true });
|
|
if (!autoCaptureEnabled) return;
|
|
|
|
const url = item.finalUrl || item.url;
|
|
if (!isCapturableUrl(url)) {
|
|
console.warn("[kget-capture] cannot hand a local/embedded URL to KGet, letting Chrome handle it:", url);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
await chrome.downloads.cancel(item.id);
|
|
// erase() only clears the history entry -- it does NOT delete the
|
|
// partial file Chrome already wrote to disk. removeFile() does that.
|
|
await chrome.downloads.removeFile(item.id);
|
|
} catch (e) {
|
|
// Expected if Chrome finished the download before cancel() landed, or
|
|
// hadn't written any bytes yet -- not worth surfacing to the user.
|
|
}
|
|
try {
|
|
await chrome.downloads.erase({ id: item.id });
|
|
} catch (e) {
|
|
console.error("[kget-capture] failed to erase Chrome download entry:", e);
|
|
}
|
|
|
|
const filename = item.filename ? item.filename.split(/[\\/]/).pop() : null;
|
|
sendToKGet(url, filename);
|
|
}
|
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
|
if (area === "local" && "autoCaptureEnabled" in changes) {
|
|
updateBadge(changes.autoCaptureEnabled.newValue);
|
|
}
|
|
});
|
|
|
|
refreshBadge();
|