How do you Disable css in CasperJS?

ε祈祈猫儿з 提交于 2019-12-10 13:26:10

问题


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?


回答1:


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();
    }
  }

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.

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).




回答2:


This is what worked for me

//filter css
casper.options.onResourceRequested = function(C, requestData, request) {
    var accept = requestData.headers[0];
    if ( accept.value.indexOf('text/css') !== -1 ) {
        console.log('Skipping CSS file: ' + requestData.url);
        request.abort();
    }
};

How do you Disable css in CasperJS? didn't work



来源:https://stackoverflow.com/questions/20819190/how-do-you-disable-css-in-casperjs

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