gpt4 book ai didi

java - 有没有办法在 Java 驱动程序中使用 PhantomJS 的 onResourceRequested 回调?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:33:10 25 4
gpt4 key购买 nike

我想避免在 PhantomJS 中加载 css 文件。我正在使用 Java 驱动程序。我看到了一个很好的 PhantomJS 示例 js 代码,如下所示:

page.onResourceRequested = function(requestData, request) {
if ((/http:\/\/.+?\.css/gi).test(requestData['url']) || requestData['Content-Type'] == 'text/css') {
request.abort();
}
};

我的代码是这样使用 PhantomJSDriver (scala):

val sb = new PhantomJSDriverService.Builder()
val svc = sb.usingPhantomJSExecutable(new java.io.File("./phantomjs")).usingCommandLineArguments(Array("--load-images=false","--disk-cache=true")).build()

有没有一种方法可以实现同样的目的,也许是通过 Java API?

最佳答案

我能够使用 PhantomJSDriver#executePhantomJS 方法注册页面的 onResourceRequested 回调。

这是禁用 Google Analytic 请求的示例:

PhantomJSDriver driver = new PhantomJSDriver(service, capabilities);
driver.executePhantomJS("this.onResourceRequested = function(request, net) {" +
" console.log('REQUEST ' + request.url);" +
" if (request.url.indexOf('google-analytics') !== -1) {" +
" console.log('Abort ' + request.url);" +
" net.abort();" +
" }" +
"};");

关于java - 有没有办法在 Java 驱动程序中使用 PhantomJS 的 onResourceRequested 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28154835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com