Files
2024-07-21 18:56:08 +02:00

35 lines
986 B
JavaScript

const URL = `http://localhost:5000/download`;
let lastRightClickCoords = { x: 0, y: 0 };
document.addEventListener("contextmenu", (event) => {
lastRightClickCoords = { x: event.clientX, y: event.clientY };
console.log("Right-click coordinates:", lastRightClickCoords);
});
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
const { x, y } = lastRightClickCoords;
let element = document.elementFromPoint(x, y);
while (element && element.tagName != "YTD-RICH-ITEM-RENDERER") {
element = element.parentElement;
}
if (!element.tagName == "YTD-RICH-ITEM-RENDERER") {
console.error("No video element found.");
return;
}
const link = element.querySelector("a#video-title-link").href;
fetch(URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
link: link,
}),
}).then((res) => {
console.log(res);
res.json().then((data) => console.log(data));
});
});