Google Chrome contains some functionality in its chrome.webrequest API (e.g., http://developer.chrome.com/extensions/samples.html#12a7bf1490a26359eadf10917e37c5b9 ) that can be used to redirect certain URLs to a specified web page. The Chrome extension uses a blocking event listener (chrome.webRequest.onBeforeRequest.addListener) and does a redirect for targeted URLs. How might I do something similar in a FireFox Add-on?
This question was asked for a very long time. I'm not sure if there was solution for this at then. But now we can do such things using Firefox WebExtensions API.
It is compatible with Chrome in most APIs (though some of them are not supported, or not fully supported).
For this very specific question, Firefox WebExtensions support webRequest API, you can use it like the way in Chrome:
chrome.webRequest.onBeforeRequest.addListener(...);
Note: To use
webRequest
API, you must have thewebRequest
permission in your manifest.json.
You can use the same request format using browser
namespace. Below is a sample code for the same. Here listener
is a callback function.
browser.webRequest.onBeforeRequest.addListener(
listener, // function
filter // object
)
browser.webRequest.onBeforeRequest.removeListener(listener)
browser.webRequest.onBeforeRequest.hasListener(listener)
You can find more details about this api here.
Anything that we can show you will be disabled at an unknown time and would not work for webextensions, due to Kev Needham's plan for the future of Firefox extensions so all that you can do is wait until webextensions are finally supported by Firefox.
© 2022 - 2024 — McMap. All rights reserved.
browser.webRequest is undefined
orchrome.webRequest is undefined
. – Quinary