Using URL object from extension

Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.

Moderators: FranklinDM, Lootyhoof

xtal256
Moon lover
Moon lover
Posts: 95
Joined: 2014-06-22, 00:32
Location: here

Using URL object from extension

Post by xtal256 » 2024-12-28, 08:06

I am writing some code for an extension, and I would like to get the hostname part of a URL. The easiest way to do this is to construct a new URL object then access the hostname property. However, when I do that I get an error that "URL" is not defined. Does this object not exist in the scope of XUL extensions?

vannilla
Moon Magic practitioner
Moon Magic practitioner
Posts: 2515
Joined: 2018-05-05, 13:29

Re: Using URL object from extension

Post by vannilla » 2024-12-28, 12:30

I'm not sure about the state of URL in extensions, but you can use nsIURI if you don't have to do anything special that requires specifically the URL class.

Code: Select all

const IOService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
const myURI = IOService.newURI('https://example.org', null, null);

User avatar
Moonchild
Project founder
Project founder
Posts: 38812
Joined: 2011-08-28, 17:27
Location: Sweden

Re: Using URL object from extension

Post by Moonchild » 2024-12-28, 14:22

If you insist you could use a Web API in a XUL extension but that would be a kludge at best. XUL extensions have access to native interfaces for a reason; depending on what exactly you want to do, nsIURI, the window mediator or browser object may be best suited.
https://duckduckgo.com/?t=palemoonsp&q= ... XUL&ia=web has a few starting points for those.
"There is no point in arguing with an idiot, because then you're both idiots." - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

xtal256
Moon lover
Moon lover
Posts: 95
Joined: 2014-06-22, 00:32
Location: here

Re: Using URL object from extension

Post by xtal256 » 2024-12-28, 21:35

Ok thanks, I will use nsIURI.

User avatar
Moonchild
Project founder
Project founder
Posts: 38812
Joined: 2011-08-28, 17:27
Location: Sweden

Re: Using URL object from extension

Post by Moonchild » 2024-12-28, 22:10

A more specialized version of it is nsIURL
"There is no point in arguing with an idiot, because then you're both idiots." - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 5766
Joined: 2015-12-09, 15:45

Re: Using URL object from extension

Post by moonbat » 2024-12-29, 16:32

vannilla wrote:
2024-12-28, 12:30

Code: Select all

const IOService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
const myURI = IOService.newURI('https://example.org', null, null);
Far easier and performance wise better to import Services.jsm and use the exposed cached service from there instead of directly accessing the service classes, like so:

Code: Select all

Components.utils.import("resource://gre/modules/Services.jsm");
const myURI = Services.io.newURI('https://example.org', null, null);
Lots of additional services are cached by this module and you can refer to them above.
You will find most of the XUL documentation there at the main site from that link as well.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Image
KDE Neon on a Slimbook Excalibur (Ryzen 7 8845HS, 64 GB RAM)
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX
Jabber: moonbat@hot-chili.net