Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6385185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:52:03+00:00 2026-05-25T02:52:03+00:00

I am trying to develop a Firefox extension that drops every HTTP request to

  • 0

I am trying to develop a Firefox extension that drops every HTTP request to a certain site and returns a fake response. No request should get through to the original web server, but I want to be able to create a custom response. I tried to intercept the “http-on-modify-request” message, but cancelling the request doesn’t seem to work, as I cannot simulate a real response afterwards. Similarly, using an nsITraceableStream instance, I don’t seem to be able to really cancel the request. I am out of ideas, can somebody help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:52:04+00:00Added an answer on May 25, 2026 at 2:52 am

    The answer below has been superseded as of Firefox 21, now the nsIHttpChannel.redirectTo() method does the job nicely. You can redirect to a data: URI, something like this will work:

    Components.utils.import("resource://gre/modules/Services.jsm");
    const Ci = Components.interfaces;
    
    [...]
    
    onModifyRequest: function(channel)
    {
      if (channel instanceof Ci.nsIHttpChannel && shouldRedirect(channel.URI.spec))
      {
        let redirectURL = "data:text/html," + encodeURIComponent("<html>Hi there!</html>");
        channel.redirectTo(Services.io.newURI(redirectURI, null, null));
      }
    }
    

    Original answer (outdated)

    Each channel has its associated stream listener that gets notified when data is received. All you need to do to fake a response is to get this listener and feed it with wrong data. And nsITraceableChannel is in fact the way to do it. You need to replace the channel’s usual listener by your own that won’t do anything, after that you can cancel the channel without the listener being notified about it. And then you trigger the listener and give it your own data. Something like this:

    Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    const Cc = Components.classes;
    const Ci = Components.interfaces;
    
    [...]
    
    onModifyRequest: function(channel)
    {
      if (channel instanceof Ci.nsIHttpChannel && channel instanceof Ci.nsITraceableChannel)
      {
        // Our own listener for the channel
        var fakeListener = {
          QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamListener,
                            Ci.nsIRequestObserver, Ci.nsIRunnable]),
          oldListener: null,
          run: function()
          {
            // Replace old listener by our fake listener
            this.oldListener = channel.setNewListener(this);
    
            // Now we can cancel the channel, listener old won't notice
            //channel.cancel(Components.results.NS_BINDING_ABORTED);
          },
          onDataAvailable: function(){},
          onStartRequest: function(){},
          onStopRequest: function(request, context, status)
          {
            // Call old listener with our data and set "response" headers
            var stream = Cc["@mozilla.org/io/string-input-stream;1"]
                           .createInstance(Ci.nsIStringInputStream);
            stream.setData("<html>Hi there!</html>", -1);
            this.oldListener.onStartRequest(channel, context);
            channel.setResponseHeader("Refresh", "5; url=http://google.com/", false);
            this.oldListener.onDataAvailable(channel, context, stream, 0, stream.available());
            this.oldListener.onStopRequest(channel, context, Components.results.NS_OK);
          }
        }
    
        // We cannot replace the listener right now, see
        // https://bugzilla.mozilla.org/show_bug.cgi?id=646370.
        // Do it asynchronously instead.
        var threadManager = Cc["@mozilla.org/thread-manager;1"]
                              .getService(Ci.nsIThreadManager);
        threadManager.currentThread.dispatch(fakeListener, Ci.nsIEventTarget.DISPATCH_NORMAL);
      }
    }
    

    The problem with this code is still that the page shows up blank if the channel is canceled (so I commented that line) – it seems that the listener still looks at the channel and notices that it is canceled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a firefox extension that inserts additional HTTP header fields into
I'm trying to develop an extension that detects every connection made by the browser
I'm trying to develop an application that will use getImageData in javascript in Firefox
I'm going to develop a Firefox extension which should put a button in the
I am trying to develop a Firefox extension. The following code: var divList =
I'm trying to develop a firefox extension which draws a toolbar at the base
I am trying to develop a plugin/extension for a browser (Firefox or IE or
Im trying to develop an application that should show a map using the Google
I am trying to develop a firefox plugin, which should be executed, whenever a
I am trying to develop a multimedia site and I am leaning heavily toward

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.