Files
hDM/browser-extension/background.js
2026-05-09 11:38:04 +03:00

61 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const NATIVE_HOST = "com.downloadmanager.bridge";
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "dm-download-link",
title: "hDM ile indir",
contexts: ["link"]
});
chrome.contextMenus.create({
id: "dm-grab-page",
title: "Siteyi hDM ile incele",
contexts: ["page", "link"]
});
});
chrome.downloads.onCreated.addListener((item) => {
chrome.downloads.cancel(item.id, () => {
console.log("İndirme yakalandı, bridge'e gönderiliyor:", item.url);
chrome.runtime.sendNativeMessage(NATIVE_HOST, {
action: "add_download",
url: item.url,
filename: item.filename,
referrer: item.referrer ?? ""
}, (response) => {
if (chrome.runtime.lastError) {
console.error("Bridge Hatası:", chrome.runtime.lastError.message);
} else {
console.log("Bridge Yanıtı:", response);
}
});
});
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "dm-download-link") {
chrome.runtime.sendNativeMessage(NATIVE_HOST, {
action: "add_download",
url: info.linkUrl
}, (response) => {
if (chrome.runtime.lastError) {
console.error("Bridge Hatası:", chrome.runtime.lastError.message);
} else {
console.log("Bridge Yanıtı:", response);
}
});
} else if (info.menuItemId === "dm-grab-page") {
const url = info.linkUrl || info.pageUrl;
chrome.runtime.sendNativeMessage(NATIVE_HOST, {
action: "open_grabber",
url: url
}, (response) => {
if (chrome.runtime.lastError) {
console.error("Bridge Hatası:", chrome.runtime.lastError.message);
} else {
console.log("Bridge Yanıtı:", response);
}
});
}
});