0

I'm building an extension for firefox that sends a get request to local js server, the request contains the url to a youtube video, and the local server will open the video in mpv. Everything works, except for the part that makes the request, and i cant figure out how to do it.

  • I've disabled cors on the recieving end.
  • I added webRequest to the manifest.
  • I tried using XMLHttpRequest and fetch.
  • I know the casting function is called
  • I know the url's im using are formatted correctly, because when i do a manual request in the browser they work as expected.

manifest.json:

{
  "manifest_version": 2,
  "name": "Youtube Caster",
  "version": "1.0",
  "description": "My own casting plugin",
  "icons": {
    "32": "icons/youtube-32.png"
  },
    "permissions": [
    "*://www.youtube.com/*",
    "activeTab",
    "webRequest"
    ],
  "browser_action": {
    "browser_style": true,
    "default_icon": "icons/youtube-32.png",
        "default_title": "Caster",
        "default_popup": "popup/cast_video.html"
    }
}

my content script: (only showing the function that should be making the request)

function cast() {
    const url = "http://127.0.0.1:8080/play?url="
    const videoURL = window.location.href;
    console.log("casting... ", url+videoURL)

    fetch(url+videoURL);

}

I would expect the request to go through when the function is called, but it doesn't, my local server does not recieve anything.

sevbesau
  • 1
  • 3
  • It might not be the issue, but are you sure you should add a complete url as an other url's url parameter? Why do you pass the whole YT url? You should instead pass the YT id of the video. For example `http://127.0.0.1:8080/play?youtubeid=123456` – Adam Oct 08 '19 at 16:28
  • If you want to make requests to local host (`127.0.0.1...`), you have to add it to the "permissions" field in the manifest. – hindmost Oct 08 '19 at 16:30

3 Answers3

0

Try in including "http://localhost/*" or "http://127.0.0.1:8080/*" in your permissions. Reference here.

  • I tried this, but it seems to not have solved the issue – sevbesau Oct 08 '19 at 18:26
  • Are you sure you should pass the entire youtube url? Have you tried it with just the youtube id as mentioned in a comment above? Also, the way you are using the fetch command looks like you are either doing a POST or GET. But, it's doing that as the API endpoint. If you don't have an endpoint that corresponds with that, the fetch will do nothing as the server has no endpoint connecting it. Try copy and pasting what is in `url+videoURL` into Postman. If you get a 404 error that means it's not an endpoint and it's something wrong on your server side code. – Alex Valdez Oct 08 '19 at 20:46
0

After doing a bit more reading i discovered that you should'nt use ports in the permissions, and that content-scripts can only make requests on port 80. So after changing the port my server runs on to 80, everything works as expected.

refrence

I feel like it's worth mentioning why i'm using the full url. I am trying to build a system that allows me to "cast" videos to another pc on my network, comparable to how a chromecast works. So only sending over the youtube video id would limit me to only using this system for youtube, however by using the full url, every webpage that has an html5 player on it becomes 'castable'.

Thank you for all the responses!

sevbesau
  • 1
  • 3
0

I found this:

Note: The path pattern string should not include a port number. Adding a port, as in: "http://localhost:1234/" causes the match pattern to be ignored. However, "http://localhost:1234" will match with "http://localhost/"

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns

It seems Firefox and Chrome behaves a little bit differently when they see port numbers.

https://bugzilla.mozilla.org/show_bug.cgi?id=1362809