Using URL object from extension
Moderators: FranklinDM, Lootyhoof
-
xtal256
- Moon lover

- Posts: 95
- Joined: 2014-06-22, 00:32
- Location: here
Using URL object from extension
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

- Posts: 2515
- Joined: 2018-05-05, 13:29
Re: Using URL object from extension
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);-
Moonchild
- Project founder

- Posts: 38812
- Joined: 2011-08-28, 17:27
- Location: Sweden
Re: Using URL object from extension
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.
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
"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

- Posts: 95
- Joined: 2014-06-22, 00:32
- Location: here
Re: Using URL object from extension
Ok thanks, I will use nsIURI.
-
Moonchild
- Project founder

- Posts: 38812
- Joined: 2011-08-28, 17:27
- Location: Sweden
Re: Using URL object from extension
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
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite
-
moonbat
- Knows the dark side

- Posts: 5766
- Joined: 2015-12-09, 15:45
Re: Using URL object from extension
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:vannilla wrote: ↑2024-12-28, 12:30Code: 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);
Code: Select all
Components.utils.import("resource://gre/modules/Services.jsm");
const myURI = Services.io.newURI('https://example.org', null, null);
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."

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

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