Stop zombie.js from loading exclusively third-party resources

During a test, I am using zombie.js to load a page from a local express server. However, the page contains a script element that makes a call to Google Analytics. I want to prevent this external script from loading while allowing other local scripts to run smoothly.

I am aware of the { runScripts : false } option with browser.visit(), but this prevents all scripts on the page from loading, not just those from external hosts. Is there a way to achieve what I need?

Answer №1

As of version 3.1, the zombie library no longer supports the browser.resources.mock method. Instead, you can utilize the nock library:

var nock = require('nock')

nock('http://www.google-analytics.com')
  .get('/analytics.js')
  .times(Math.Infinity)
  .reply(200, '{}')

var Browser = require('zombie')
var browser = new Browser()

Answer №2

When working with the Zombie framework, make use of the resources object.

If you need to customize responses for certain requests, you can specify them using the resources object. For example, to return an empty document from Google Analytics:

browser.resources.mock('http://google.com/url/to/analytics.js',{});

Remember to provide the exact URL that you want to mock, as partial URLs like domain names cannot be mocked.

Answer №3

Perhaps this code snippet could be a solution for you? It iterates through all available resources and cancels the ones that are supposed to be disregarded.

const Fetch = require('zombie/lib/fetch');

const ignoredResources = [
  'google-analytics.com'
];

browser.pipeline.addHandler((browser, request) => {
  let doAbort = false;

  ignoredResources.forEach(domain => {
    if (request.url.includes(domain)) {
      doAbort = true;
    }
  });

  if (doAbort) {
    return new Fetch.Response('', { status: 200 });
  }
});

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

Is it possible to change the button class within a div while ensuring only the last one retains the change?

Here is a code snippet I'm using to switch between classes for buttons: $('button').on('click', function(){ var btn=$(this); if(btn.attr('class')=='tct-button'){ btn.removeClass('tct-button ...

How can one get rid of a sudden strong beginning?

Recently, I delved into the world of CSS animation and encountered a frustrating issue. I've tried numerous methods and workarounds to achieve a smoothly looping animation without interruptions. Despite my efforts, I have not been able to replicate th ...

Display the option to "Delete" the link only when there are multiple images in my image collection

I am using jQuery and Ajax to remove banner images from my website. However, I want to make sure that if there is only one image left, it cannot be deleted. Each image in the list has a corresponding delete link: echo '<a class="delete j_bannerd ...

Ways to identify when a chemical reaction has taken place?

I have developed a Discord bot that interacts with an API related to a game server panel. The bot has tasks to start, restart, stop, and kill the server. I want to enable end users to trigger these tasks by reacting to a specific embed posted by the bot. ...

Controller/other file function

Currently, I am new to the world of node.js and expressjs, learning how to work with them. Everything functions correctly when all my code is in app.js. However, I would like to organize it by creating a function in the controllers/Home directory. fu ...

Tips for ensuring a consistent highlight for an active link in Bootstrap

Does anyone have a solution for maintaining highlighted links on a web page? <div id="productnav"> <nav> <ul class="nav"> <li><a href="<?php echo $prefix; ?>pages/one.php?category=1" id="navelement1"<?php if ($c == 1) e ...

Tips on leveraging state values within the makeStyles function in Material UI React

I'm in the process of building a Webpage and incorporating Material UI for the Components. Here's the code: import { makeStyles, Typography } from "@material-ui/core"; const useStyles = makeStyles((theme) => ({ container: { ...

Reorganizing an array of JSON data by date value with a limitation to just one

Currently, I am attempting to organize a JSON array based on the date key. However, it appears that my sorting function is either stopping after one sort or simply not functioning correctly. Here is the JavaScript code for my sorting function: function ...

Prevent right-clicking on links from a particular domain

Looking to prevent right-clicking on a link? Check out this code snippet: <script type="text/javascript" language="javascript> $(document).ready(function() { $('body').on('contextmenu', 'a', function(e){ ...

Tips for crafting a test scenario for input alterations within Angular

Hello there, currently I am working on an application using Angular and TypeScript. Here is a snippet of my template code: <input type="text" placeholder="Search Results" (input)="searchInput($event)"> And here is the TypeScript code for the searc ...

Vue.JS and its Onclick event handler allows for dynamic and interactive

These are my two buttons: <button onclick="filterSelection('Action')">Action</button> and <button onclick="filterSelection('Adventure')">Adventure</button> Now, I'm trying to achieve the same fun ...

JayData - Issue with OData query: "$data.Object cannot be converted to $data.ObjectID"

I have successfully set up an OData Endpoint using Node.js with the odata-server module by JayData. Here's how I did it: require("odata-server"); $data.Entity.extend("Service", { Id: {type: "id", key: true, computed: true, nullable: false}, Name ...

Use Javascript to deactivate the mouse cursor and rely solely on the keyboard cursor for navigation

I am facing an issue with a div that contains a textarea. The cursor is automatically positioned at the beginning of the text within the textarea. I would like to disable the mouse cursor when hovering over the textarea but still be able to navigate within ...

The "track by" feature in Angular doesn't seem to be functioning properly when used with an array from $

Within the method of my factory, I am retrieving an array from the server using Angular's $resource: var resource = $resource("vm", { session: function() { return Auth.token(); }, all: '@all', vmId: '@vmId' ...

Is it possible to update an object within a subdocument using Mongoose's findOneAndUpdate function?

I have a model structured like this: // Document var programSchema = new Schema({ name: { type: String }, session: [sessionSchema] }, { timestamps: true }); // Subdocument var sessionSchema = new Schema({ name: { typ ...

Retrieve information using the request npm package

Currently, I am in the process of developing an application with express js. My approach involves using request(v-2.88.2) to retrieve data from an api. request(url, function(error, request, body) { var data = JSON.parse(body); }); My goal is to utili ...

Incorporate Live Data into Google Charts Using Ajax Response for a Dynamic Visualization

I am struggling to successfully load a responsive Google Line Chart after an Ajax call. I have attempted to place the entire Google Chart code within the success part of the Ajax call, but it does not seem to work as expected. Below is my current Ajax code ...

A link is not allowed to be a child of another link

An issue arises in a React.js application Warning: validateDOMNesting(...): <a> cannot be nested under another <a>. Refer to Element > a > ... > a for more information. What is the significance of this warning? How can it be avoided ...

Managing global errors and intercepting requests in AngularJS can be easily achieved by utilizing $resource interceptors and global handlers for

My question pertains to the interceptor(responseError) of $resource. It is essential to note that I am working with angularjs version V1.3.6. The Issue: app.factory('authInterceptor',['$q', '$location', '$log', fun ...

NodeJS: Steps to efficiently transfer data from a master table to two separate tables while maintaining the order of the master table, utilizing asynchronous code wherever applicable

Are promises or async/await being used for this task? For instance: if the master table has columns (id, uuid, op_1, op_2) then the new tables should be table1(id, uuid) table2(id, op_1, op_2) The priority is to maintain the same order as the master ta ...