Search code examples
javascriptfirefox-addon-webextensions

How do I make a GET request from content script of firefox extension?


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.


Solution

  • 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!