Use of fetch api in extensions
Moderators: FranklinDM, Lootyhoof
-
- Lunatic
- Posts: 358
- Joined: 2014-05-10, 18:19
- Location: UK
Use of fetch api in extensions
How can I actually get hold of the 'fetch' method if I want to use it an an extension?
Getting hold of some APIs that are available to web pages has always been a little tricky, and since the fetch API is fairly new, I couldn't find anything useful on the historical mozilla documentation
Getting hold of some APIs that are available to web pages has always been a little tricky, and since the fetch API is fairly new, I couldn't find anything useful on the historical mozilla documentation
Re: Use of fetch api in extensions
You don't - you use the existing mechanism of registering an HttpObserver for http-on-modify-request and an nsIHttpChannel, both of which are documented.
If you want to make extensions, you're better off reading the XUL and XUL School tutorials and following examples from there. Also see the documentation for Services.jsm for common services that are cached by this module.
As an example, instead of using
as in the nsIHttpChannel page above, you can use
. The Services.jsm docs indicates what all interfaces are offered by Services, you can substitute them accordingly. Just add the import statement at the top of your source file once.
These aren't weeny Web Extensions and so use different and more powerful API than the sort of Javascript that merely manipulates webpage content.
If you want to make extensions, you're better off reading the XUL and XUL School tutorials and following examples from there. Also see the documentation for Services.jsm for common services that are cached by this module.
As an example, instead of using
Code: Select all
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var ch = ios.newChannel("https://www.example.com/", null, null);
Code: Select all
Components.utils.import("Services.jsm");
var ch=Services.io.newChannel("https://www.example.com/", null, null);
These aren't weeny Web Extensions and so use different and more powerful API than the sort of Javascript that merely manipulates webpage content.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX
Re: Use of fetch api in extensions
This interface is also available for the XUL windows.thosrtanner wrote: ↑2023-03-26, 06:27Getting hold of some APIs that are available to web pages has always been a little tricky
You can, for example, get a browser window and call window.fetch()
-
- Lunatic
- Posts: 358
- Joined: 2014-05-10, 18:19
- Location: UK
Re: Use of fetch api in extensions
doesn't work terribly well outside of a browser window thoughKris_88 wrote: ↑2023-03-26, 15:13This interface is also available for the XUL windows.thosrtanner wrote: ↑2023-03-26, 06:27Getting hold of some APIs that are available to web pages has always been a little tricky
You can, for example, get a browser window and call window.fetch()
-
- Lunatic
- Posts: 358
- Joined: 2014-05-10, 18:19
- Location: UK
Re: Use of fetch api in extensions
Thanks for the references to the archived documentation and nsIHttpChannel which looks useful. It is worth noting however that the page in the tutorial about fetching remote content refers to XMLHttpRequest. There's nothing about setting up an http channel in the tutorialsmoonbat wrote: ↑2023-03-26, 06:58You don't - you use the existing mechanism of registering an HttpObserver for http-on-modify-request and an nsIHttpChannel, both of which are documented.
If you want to make extensions, you're better off reading the XUL and XUL School tutorials and following examples from there.
...
Re: Use of fetch api in extensions
works fine for me
Code: Select all
let windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
let win = windowMediator.getMostRecentWindow("navigator:browser");
win.fetch('https://google.com/')
.then((response) => { return response.text(); })
.then((data) => { Services.prompt.alert(null, 'fetch', data); });
Re: Use of fetch api in extensions
alternatively you can use XHR
Code: Select all
var xhr = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
xhr.open('GET', 'https://...', true);
xhr.timeout = 5000;
xhr.responseType = 'text';
xhr.onloadend = function() {
if (xhr.readyState != 4 || xhr.status != 200) return;
// xhr.response ...
};
xhr.send();
-
- Lunatic
- Posts: 358
- Joined: 2014-05-10, 18:19
- Location: UK
Re: Use of fetch api in extensions
I'm using xhr. with a wrapper round it so I can use async/await.
Having said which, I don't think fetch gives me what I want either, so it looks like using an observer might be the way to go.
Having said which, I don't think fetch gives me what I want either, so it looks like using an observer might be the way to go.
- FranklinDM
- Add-ons Team
- Posts: 518
- Joined: 2017-01-14, 02:40
- Location: Philippines
- Contact:
Re: Use of fetch api in extensions
There is another option available: Components.utils.importGlobalProperties.
I haven't seen this used in any extensions (yet), aside from XPC tests and internal platform modules. See this for usage examples.
I haven't seen this used in any extensions (yet), aside from XPC tests and internal platform modules. See this for usage examples.
Re: Use of fetch api in extensions
Thank you!