Using Chrome's tabs.executeScript() method with an included script file

I'm in the process of creating a basic Chrome extension, where I need to actively monitor the AJAX calls being made on the page.

Here is the code snippet I have implemented to listen to AJAX calls:

var isAjaxWorking = false;
//timeout
var timeout;

//create new AJAX listener
var s_ajax = new Object();

//override the send() method of XMLHttpRequest
s_ajax.tempSend = XMLHttpRequest.prototype.send;

s_ajax.callback = function (event){
   isAjaxWorking = true;
   timeout = 500; //5 seconds
   var cycle = setInterval(function(){
      timeout--;
      if(timeout > 0){
         if(event.readyState === 4){
            isAjaxWorking = false;
            clearInterval(cycle);
         }
       }
       else{
        console.log('s_ajax.callback : timeout');
        clearInterval(cycle);
        isAjaxWorking = null;
      }
   }, 10);
}

XMLHttpRequest.prototype.send = function(a, b) {
   if (!a) var a = '';
   if (!b) var b = '';
   s_ajax.tempSend.apply(this, arguments);
   s_ajax.callback(this);
}

When I directly inject this script into the console of the desired page, it works perfectly. However, when I attempt to execute it using chrome.tabs.executeScript() within my Chrome extension, it seems like everything gets erased right after the script execution. I suspect it's a simple oversight on my part, but I'm unable to identify the issue. I appreciate any guidance on this matter. Thank you in advance.

Answer №1

Just the other day, I was struggling with a similar issue. Check out the following:

Isolated worlds provide a way for each content script to modify its JavaScript environment independently, without having to worry about interfering with the webpage or other content scripts. For instance, one content script may use JQuery v1 while the page uses JQuery v2, and they won't clash with each other. https://developer.chrome.com/extensions/content_scripts

Your code does not directly affect the page's code, so modifying the cross domain request method in your extension code will not impact the method used by the page.

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

Create a personalized and distinct name following the submission of data into multiple text fields either through Nuxt/Vue or by utilizing pure JavaScript

In my new app, users can register packages and then participate in a ballot session where the package will be assigned to someone else. To make this process smoother, I want each ballot session or box to have a unique Ballot ID attached to it. For example ...

After submitting a form via AJAX in Drupal 8, the input fields are not being cleared and the page does not automatically scroll

My form consists of a single field and a submit button with AJAX submission capabilities, as shown below - public function buildForm(array $form, FormStateInterface $form_state, $id = 0) { $form['fieldset']['message'] = arr ...

Choose an image and save the selection information for the following page (Tarot card)

I'm in the process of creating a website that showcases multiple tarot cards. The goal is for users to select the cards they're interested in and have their chosen card displayed on the next page. I've implemented some code for selecting the ...

Guide on sending a JavaScript variable to PHP using AJAX

I am currently utilizing the following JavaScript code to obtain data from a td element when a button is clicked within an onclick event. function rfk(element) { var element = element.parentElement.parentElement; var id = parseInt(element.childre ...

Checking email existence through remote jQuery validation

Utilizing the jQuery validator plugin, I am implementing an ajax function with a remote method to validate whether an email already exists in my database. However, I am encountering an error when making the ajax call within my validation function. "email ...

During the installation of a package, npm encountered a require stack error with the code MODULE_NOT_FOUND

Whenever I attempt to install something using the npm install command, it throws an error saying "require stack" and "code MODULE_NOT_FOUND" C:\Users\dell>npm audit fix node:internal/modules/cjs/loader:1075 const err = new Error(message); ...

The antithesis of jQuery's .parents() selector

I am currently developing a userscript for a webpage with an old-fashioned design consisting mainly of tables. My goal is to access a long table of fields so that they can be filled in automatically by the script. To simplify, the structure is as follows: ...

utilize ng-bind to apply numerous values simultaneously

Is it possible to bind multiple values using ng-bind in the following manner : <p ng-bind="instructor.first_name instructor.last_name"></p> Every time I attempt this, I encounter the following error: Error: $parse:syntax Syntax Error I am a ...

"Error handling in React AJAX post requests is not triggering the success or error methods as expected

Within my React Component, there is an AJAX POST request that successfully creates a new entry in the database (status 201 returned). However, I am facing an issue where the success and error methods are not being triggered. $.ajax({ url: &apo ...

Create a new variable and compile the sass/less file when it is requested

I'm exploring options to utilize Node.js or another server-side language to handle a specific type of request. For instance, receiving a request like this: The goal is to extract the value from the URL parameter and use it to dynamically update a var ...

Error encountered in Express Router middleware (`app.use() function must be provided with a middleware function`)

I've seen plenty of similar questions on this topic, but after reviewing them all, I still haven't found a solution. My current dilemma involves creating an app using Express Router, however I keep encountering the following error: app.use() re ...

Creating Stunning CSS Animations for a Slider Using SVG

Looking for help to create an SVG CSS animation for a slider using a mockup image. The animation will feature a balloon with a number value that zooms in and out from left to right or right to left. Currently stuck at the starting point and unsure how to ...

Arrange JSON information in an HTML table with distinct header rows for every data category

I have a JSON object with a key:value pair named callRoot. Some examples of values for this pair are @S, @C, and @W. There are multiple objects that share the same value and I want to create an HTML table head row at the beginning of each group and repeat ...

listening for events on nested classes

I am looking to dynamically toggle the class "collapsed" on each element with the class "category" when it is clicked. The issue arises when these "category" elements are nested within each other, causing the child elements to also trigger the class change ...

Using a jQuery script, you can enable a back button functionality that allows users

I created a survey to assist individuals in determining where to go based on the type of ticket they have. However, I am currently facing difficulties with implementing a "Back" button that will display the previous screen or "ul" that the user saw. Initia ...

What is the method to retrieve the string value from a JavaScript String object?

Is there a way to extend a method to the String prototype and manipulate the string value? I'm facing some difficulty in accessing the actual string value, as this, the current object context, seems to refer to the string object instead. String.pro ...

Issue with dialogue not appearing when clicking on a table cell

UPDATE: I am facing a challenge with this code not displaying a dialog upon click.: The issue lies in the fact that nothing happens when the Title is clicked. Any suggestions? The data is present, as removing the .hidden CSS class reveals it. $(". ...

Integrating Watson Conversation with Oracle Database

Hello everyone, I am currently working on a project where I need Watson to fetch a response manually set from our Oracle Databases. To achieve this, I am using async to access the database sequentially and return the response. Initially, I faced an issue ...

Utilizing Multiple PHP isset Statements on a Single Page

My goal is to implement multiple paginations on a single page. The pagination for all the main results is functional. However, I also want to include additional options such as Newest, Reputation, and Oldest. In the screenshot below, you can see th ...

The issue of null AJAX post data in a c# controller requires attention and troubleshooting

I'm struggling to figure out why this issue is occurring. Even after attempting the solutions suggested on Stack Overflow, the value received in the controller remains null. [HttpPost, ActionName("PayNow")] public async Task<IAct ...