Detect XHR (ajax) response while monitoring http response in Firefox extension

My firefox addon (addon-sdk) has a feature where it listens to http responses and blocks them if the content type matches certain specified types. However, I do not want to listen to image, JavaScript files, or XHR (ajax) responses. Is there a way to filter out or identify XHR requests specifically so that unnecessary resources are not wasted on processing these responses?

A similar functionality can be achieved in chrome extensions by using "details.type" which can be main_frame, image, xhr, etc.

var httpResponseObserver =  
{  
  observe: function(subject, topic, data)  
  {  

    if (topic == "http-on-examine-response") 
    {
        var channel = subject.QueryInterface(Ci.nsIHttpChannel); 
        var contentType;
        try {
                contentType = channel.getResponseHeader('Content-Type');
                if (/\b(?:xml|rss|javascript|vnd|json|html|text|image|ocsp|x-shockwave-flash)\b/.test(contentType))
                {
                    return;
                } 
            } 
        catch(error) {
                return;
            }
}
}

Answer №1

Make a request for the nsIXMLHttpRequest interface from the notificationCallbacks

var isXHR;

try{
  var channel = subject.QueryInterface(Ci.nsIHttpChannel);
  var callbacks = channel.notificationCallbacks;
  var xhr = callbacks ? callbacks.getInterface(Ci.nsIXMLHttpRequest) : null;
  isXHR = !!xhr;
}
catch(e){
  isXHR = false;
}

Answer №2

channel.cancel(Components.results.NS_BINDING_ABORTED);

Make sure to insert this line before the return statement within that 'if' block :)

If you want to identify the type of request, such as image or ajax, you'll need to check the flags. Refer to the code below, search for 'disposition', and examine those flags.

Based on some previous testing I conducted a while back for ajax/xhr requests, these were the specific combination of flags that indicated it:

LOAD_REQUESTMASK |
LOAD_BACKGROUND |
INHIBIT_PIPELINE |
LOAD_EXPLICIT_CREDENTIALS |
DISPOSITION_ATTACHMENT
// If the flags match the above configuration, it signifies an ajax request (may include INHIBIT_CACHING flag)

Please feel free to share any discoveries you make; I'm curious and always eager to learn more!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve information from a pop-up modal and showcase it in a table within the main controller

How can we transfer data from a modal to a main controller table and update the table on clicking the generate button in the modal? Currently, I am able to retrieve input text data from the generate button click event but the table is not being updated w ...

Trying my hand at using JQuery to dynamically update the image source of a draggable element upon dropping

Essentially, I have an object being dragged with the class .dragme, a dropzone with the class .dropzone1 for it to be dropped at, and an image that I want the .dragme object to become once it has been dropped. My current code looks like this: $(".dropzon ...

"Incorporate keyframes to create a mouseleave event with a distinctive reverse fade

After posing a similar question a few days back, I find myself encountering yet another hurdle in my project. The task at hand is to switch the background image when hovering over a button with a fade-in effect using @keyframes. However, I'm stuck bec ...

Unable to access the variable field of MongoDB in JavaScript

I'm facing an issue while trying to loop through an array I retrieved from mongodb. It seems the array is treated as a single element, and here is the code snippet: const project = await Project.findOne({ embedId: inititalData.embedId }); console. ...

Struggling to create a <td> with AngularJS directive

I am currently working on creating a directive for a large set of repeated HTML code that involves using tables and table data cells. Although I have experience creating directives for div elements in the past, this is my first attempt at incorporating the ...

Removing a feature through AJAX in CodeIgniter

Issue Resolved.. I have updated my code accordingly.. Thanks to everyone for the help Description: My code displays an array of message traces (each message is displayed in a grey panel). Users can delete unwanted messages by clicking on the delete button ...

Attempting to implement ajax for form submission, however finding that the $_POST array is coming back as empty

I'm attempting to send JavaScript arrays to a new page using Ajax. Although there are numerous questions on this topic on Stack Overflow, I have decided to implement Ajax in the following manner after examining various answers: var test = {}; test[& ...

Error: JSON parsing error due to unexpected token C at the beginning of the string

When using ajax and json formatting, I am encountering this message. In PHP, the code array("message" => "Success", "data" => $data) is displayed successfully. However, it is not able to callback to ajax. How can I resolve this issue without deleting ...

Sending a data value from a Controller to jQuery in CodeIgniter

Getting straight to the point. Check out some of the functions from my Student Model: public function get_exam_data($exam_id){ $this->db->select('exam_id, exam_name, duration'); $this->db->from('exams'); $this- ...

Transforming MongoDB array into a string within the database

I am facing an issue with parsing arrays from Mongo/Express. The array from the body appears to be correct when I check the response in the node console. However, the problem arises when I attempt to save the body to the Mongo database using the insert.one ...

Looking to update table content dynamically using jQuery ajax?

I need assistance in creating a dynamic table that can update its content based on the company selected from a dropdown menu. The data should be fetched using AJAX and only display information related to the chosen company. Furthermore, I would like to imp ...

Tips for importing a different js file from an npm package without needing to include the entire node_modules path

When using the ES2016 import syntax to load the select2 library from an npm module via Webpack, everything works smoothly and the select2.js file is loaded from the node_modules directory. The node_modules directory also contains a full version of the lib ...

Attempting to programmatically enlarge various div elements using a single function

I am attempting to create a script that allows me to dynamically expand and collapse multiple divs using the same code... The expansion and collapse are triggered by clicking on a span element, and I am aiming to target the next adjacent ID (the div that ...

What is causing the error message "TypeError: expressJwt is not a function" to appear? Is there a way to resolve it and fix the issue?

Authentication with JWT in Node.js: const expressJwt = require('express-jwt') function setupAuth() { const secret = process.env.SECRET_KEY return expressJwt({ secret, algorithms: ['HS256'] }) } module.expor ...

Initiate a click action upon receiving a focus event

I can't seem to figure out why this code is not functioning as expected: $('a').focus( function() { $(this).click(); }); Context: I am working on developing a form where navigating through different elements (such as textboxes) will au ...

Mongoose reminds us that static is not a method

I encountered an error message stating that "product.try() is not a function." Interestingly, when I immediately invoke the try() function, it works fine and node recognizes the model "product." I'm starting to wonder if there's something funda ...

I want to display events from my database table on their corresponding dates using JavaScript and jQuery. How can I achieve this?

Using the FullCalendar plugin, I attempted to achieve a specific functionality, but unfortunately fell short of my goal. Below is the snippet of my scripting code: $('#calendar').fullCalendar({ //theme: true, header: { ...

Tables that respond to changes in screen size, allowing nested table cells to inherit widths

I am currently working on a responsive table with unique content in each row and a concertina feature for expanding rows. When the concertina is activated, it adds another row to the table below the current row, using a td element with a colspan attribute ...

Use jQuery to detect the presence of the class .class, and if it exists, automatically append the same class to a specific #id element on a particular webpage

Need help with jQuery - adding a specific class to an element based on the presence of another class Hello everyone, I have searched through various forums and tried multiple code snippets in JavaScript and jQuery to no avail. Despite other scripts worki ...

Using Ajax to retrieve a Partial View in MVC 5

I've been trying to figure out how this is working, or not as the case may be. There's an Ajax call to my HomeController: function addPerson() { $.ajax({ dataType: "html", url: '@Url.Action("AddPerson")', ...