如何在CasperJS中禁用CSS? [英] How do you Disable css in CasperJS?

查看:54
本文介绍了如何在CasperJS中禁用CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何禁用图像和插件,但是在CasperJS中似乎没有明显的选项可以禁用css。

I know how to disable images and plugins, but there doesn't seem to be an apparent option to disable css in CasperJS.

有人知道它的工作原理吗?

Anyone knows how that works?

推荐答案

假设您要禁止加载所有外部样式表,可以通过中止加载CSS文件的请求来做到,这是通过将函数分配给 options.onResourceRequested来完成的

Assuming you want to suppress the loading of all external style sheets, you can do it by aborting requests to load css files, which is done by assigning a function to options.onResourceRequested:

var casper = require('casper').create();
casper.options.onResourceRequested = function(C, requestData, request) {
  if ((/https?:\/\/.+?\.css/gi).test(requestData['url']) || requestData['Content-Type'] == 'text/css') {
    console.log('Skipping CSS file: ' + requestData['url']);
    request.abort();
    }
  }

为避免使用内联样式表,我唯一的想法是在页面加载后立即使用一些JavaScript删除所有样式。

To avoid the usage of inline style sheets, my only idea is to use some JavaScript to remove all styles just after the page has loaded.

如果您将SlimerJS与CasperJS一起使用,那么Gecko引擎中几乎可以选择禁用CSS(基于Web Developer插件具有选项的事实)。

If you used SlimerJS with CasperJS then there is almost certainly an option in the Gecko engine to disable CSS (based on the fact that the Web Developer plugin has an option).

这篇关于如何在CasperJS中禁用CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆