Add no workey extension

This commit is contained in:
PhatPhuckDave
2024-07-21 18:56:08 +02:00
parent c95613e240
commit 7cf0cf5033
3 changed files with 64 additions and 0 deletions

10
extension/background.js Normal file
View File

@@ -0,0 +1,10 @@
browser.menus.create({
id: "youtube-download",
title: "Download",
contexts: ["all"],
documentUrlPatterns: ["*://*.youtube.com/*"],
});
browser.menus.onShown.addListener((info, tab) => {
browser.tabs.sendMessage(tab.id, { action: "check-element", info: info });
});

34
extension/content.js Normal file
View File

@@ -0,0 +1,34 @@
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));
});
});

20
extension/manifest.json Normal file
View File

@@ -0,0 +1,20 @@
{
"manifest_version": 2,
"name": "Youtube Downloader",
"version": "1.0",
"permissions": [
"menus",
"activeTab",
"tabs",
"http://localhost:5000/*"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["*://*.youtube.com/*"],
"js": ["content.js"]
}
]
}