0

I want to preface that I have asked many questions already and have consulted developer.mozilla.org and tried even openAI

This is my first project and please be patient with me.

I have already used a simple bat script to play URLs with the yt-dlp.exe program. So I've tested the 'command' so it does work when executed as a following batch

@echo off
:link
set "url="
set /p url="link: "
"C:\\Program Files\\VideoLAN\\VLC\\yt-dlp.exe" --get-url -f b "%url%" | "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe" --meta-title "%url%" --fullscreen --no-video-title

But the extension I try to write can not send it to the batch script

What it should do:

A context menu inside a right mouse menu allowing to send an URL from within YouTube site to a batch script. The batch script would execute the 'command' generated in background.js start yt-dlp.exe and pipe the generated stream to VLC.

What it does

Of course I write here because it doesn't work. In the debugger I get an error message after line 12 in background.js --

let port = browser.runtime.connectNative("vlchost");

Error: Attempt to postMessage on disconnected port

and what is peculiar the system is opening the vlchost.json file (my default application for json is VS Code) in the editor

Here are all the parts of the project

The registry entries for Native Messaging

[HKEY_CURRENT_USER\SOFTWARE\Mozilla\NativeMessagingHosts\vlchost]
@="C:\\Users\\me\\ytdlpvlc\\vlchost.json"
[HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla\NativeMessagingHosts\vlchost]
@="C:\\Users\\me\\ytdlpvlc\\vlchost.json"

The manifest

C:\Users\me\ytdlpvlc\manifest.json

{
  "manifest_version": 2,
  "name": "YTDLPVLC",
  "version": "0.8",
  "description": "YouTube ytdlp VLC",
  "applications": {
    "gecko": {
      "id": "vlchost@localhost",
      "strict_min_version": "50.0"
    }
  },
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "permissions": [
    "contextMenus",
    "nativeMessaging",
    "tabs"
  ]
}

The JS background script

It should make the context menu available and send the 'command' with appropriate URL copied from YT to yt-dlp.exe which resides in the same directory as VLC

C:\Users\me\ytdlpvlc\background.js

browser.contextMenus.create({
  id: "ytdlpvlc",
  title: "play in VLC",
  contexts: ["link"],
  targetUrlPatterns: ["*://*.youtube.com/watch*"]
});

browser.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === "ytdlpvlc") {
    let videoUrl = info.linkUrl;
    let command = `"C:\\Program Files\\VideoLAN\\VLC\\yt-dlp.exe" --get-url -f b "${videoUrl}" | "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe" --meta-title "${videoUrl}" --fullscreen --no-video-title fd://0`;
    let port = browser.runtime.connectNative("vlchost");
    port.postMessage(command);
    port.onMessage.addListener(response => {
      console.log(response);
    });
  }
});

Manifest for the host

C:\Users\me\ytdlpvlc\vlchost.json

{
  "name": "yt_dlp_vlc_host",
  "description": "Native messaging host for YT-DLP-VLC extension",
  "path": "C:\\Users\\me\\ytdlpvlc\\vlchost.bat",
  "type": "stdio",
  "allowed_extensions": ["vlchost@localhost"]
}

.bat script

this script should get the 'command' from background.js and execute it so the URL can be passed to yt-dlp.exe and piped to VLC

C:\Users\me\ytdlpvlc\vlchost.bat

@echo off
set command=%1
echo Command to execute: %command%
%command%
echo Done

Can please someone at least point me in the direction of a solution -- even it this solution would be an complete overhaul of this project. I can imagine that my extension could be resolved in many ways. I just know yt-dlp.exe and am used to it. I have Python installed on my computer if this information is helpful :)

thanks a lot in advance

0 Answers0