Prevent zombie.js from loading only external resources

三世轮回 提交于 2019-12-04 09:11:46

You should use the resources object.

You can set certain requests to give specific responses if you don't want the request to actually go through. You'd do the following to make google analytics return an empty document:

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

Note that you have to provide the exact URL that you want to mock, there is no way to mock a partial URL such as a domain name.

Since zombie 3.1, the browser.resources.mock method is gone. The alternative is to use 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()

Maybe something like this would work for you? It loops through all resources and "aborts" the ones that should be ignored.

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 });
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!