How to filter javascript from specific urls in HtmlUnit

若如初见. 提交于 2019-12-11 04:57:42

问题


HtmlUnit takes lot of time to execute javascript, i would like to know if its possible to make HtmlUnit not to load javascript from url regex filters.


回答1:


Not exactly, you can't only disable javascript as a whole (probably you already know it):

final WebClient webClient = new WebClient();
webClient.getOptions().setJavascriptEnable(false);

but you can use a ScriptPreProcessor the javascript, and erase what you don't want:

webClient.setScriptPreProcessor(new ScriptPreProcessor() {

        @Override
        public String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement) {
            if (match...)
            return "";
        }
    });


来源:https://stackoverflow.com/questions/15962990/how-to-filter-javascript-from-specific-urls-in-htmlunit

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