Files
omarchycn/default/chromium/extensions/yt-dlp/background.js
T
David Heinemeier HanssonandClaude Opus 4.8 e5290b0a12 Add yt-dlp "Download Video" Chromium extension
Download the current page's video with yt-dlp via Alt+Shift+D or a click
on the toolbar icon. A native-messaging host runs the download, shows live
progress on the Quickshell OSD, and posts a clickable "Download complete"
toast that opens the file in mpv.

- Extension: pinned key for a stable id, green download-video icon,
  keyboard command + toolbar action (reads the active tab URL).
- Native host (omarchy-chromium-ytdlp-host): verifies the URL with
  yt-dlp --simulate (else "No video found"), streams progress to the OSD
  (time-throttled to ~4/s), saves to ~/Videos, opens mpv on click.
- Installer (omarchy-install-chromium-ytdlp) writes the native-messaging
  manifest into installed Chromium/Chrome/Brave/Edge profiles; wired into
  browser install and chromium refresh, with a migration for existing users.
- omarchy-osd: add -d/--duration so the OSD can persist during a download.
- Add yt-dlp to base packages, load the extension via --load-extension,
  and document the Alt+Shift+D binding.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 11:15:28 +02:00

42 lines
1.2 KiB
JavaScript

function sendUrl(url) {
if (!url || !/^https?:/i.test(url)) return;
// The native messaging host runs yt-dlp and owns all the desktop
// notifications, so we just hand off the URL and ignore the reply.
chrome.runtime.sendNativeMessage('com.omarchy.ytdlp', { url }, () => {
void chrome.runtime.lastError;
});
}
function triggerDownload(tab) {
if (!tab) return;
// The activeTab permission exposes tab.url whenever the user invokes the
// extension — both via the toolbar click and the keyboard shortcut.
if (tab.url) {
sendUrl(tab.url);
return;
}
// Fallback: read the URL straight from the page.
if (tab.id === undefined) return;
chrome.scripting
.executeScript({ target: { tabId: tab.id }, func: () => location.href })
.then((results) => sendUrl(results && results[0] && results[0].result))
.catch(() => {});
}
// Keyboard shortcut (Alt+Shift+D).
chrome.commands.onCommand.addListener((command) => {
if (command === 'download-video') {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
triggerDownload(tabs[0]);
});
}
});
// Clicking the extension's toolbar icon.
chrome.action.onClicked.addListener((tab) => {
triggerDownload(tab);
});