Possible to create an extenstion that sends link via UDP?

Talk about add-ons and extension development.
Post Reply
Khat
Posts: 3
Joined: November 17th, 2017, 5:57 pm

Possible to create an extenstion that sends link via UDP?

Post by Khat »

Hello.

I have a device on my local network that I want to be able to send URLs to. My original plan was to just have it bind a UDP socket and listen for them there, but the scripting on that end is simple and I can basically have it listen for anything. The issue is that I'm completely new to extension development and the Mozilla tutorials didn't really point me in the direction that I'd like to go.

What I want to do is write an extension such that when I r-click a link in my browser there's an option that says "Send to Trio", and then when I click that it sends the URL of the link, either via UDP or whatever other method is available for extensions to use. This is just something for private use in my home, so I'm not looking for anything more complex than necessary.

Could anyone point me in the right direction here? How do I create context-menu items for links, and what would be the simplest method for sending the link?

Thanks in advance for any advice or links to relevant documentation. :D
Khat
Posts: 3
Joined: November 17th, 2017, 5:57 pm

Re: Possible to create an extenstion that sends link via UDP

Post by Khat »

I have the context menu part figured out, but I'm not sure how to send the link to the other device.

Is there a simple way to send a UDP packet, or to make an HTTP post/request?
Khat
Posts: 3
Joined: November 17th, 2017, 5:57 pm

Re: Possible to create an extenstion that sends link via UDP

Post by Khat »

Okay, I've figured this out. Here's what I ended up with:

Code: Select all

function SendLink(link) {
  var oReq = new XMLHttpRequest();
  oReq.open("POST", "http://192.168.1.123:4321");
  var blob = new Blob([link], {type: 'text/plain'});
  oReq.send(blob);
}

browser.menus.create({
  id: "sendToTrio",
  title: "Send to Trio",
  contexts: ["link"]
});

browser.menus.onClicked.addListener((info, tab) => {
  if(info.menuItemId == "sendToTrio") {
    SendLink(info.linkUrl);
  }
});
Post Reply