在 PhantomJS 和 GhostDriver 中将 URL 列入黑名单非常简单。首先使用处理程序初始化驱动程序:
PhantomJSDriver driver = new PhantomJSDriver();
driver.executePhantomJS(loadFile("/phantomjs/handlers.js"))
并配置处理程序:
this.onResourceRequested = function (requestData, networkRequest) {
var allowedUrls = [
/https?:\/\/localhost.*/,
/https?:\/\/.*\.example.com\/?.*/
];
var disallowedUrls = [
/https?:\/\/nonono.com.*/
];
function isUrlAllowed(url) {
function matches(url) {
return function(re) {
return re.test(url);
};
}
return allowedUrls.some(matches(url)) && !disallowedUrls.some(matches(url));
}
if (!isUrlAllowed(requestData.url)) {
console.log("Aborting disallowed request (# " + requestData.id + ") to url: '" + requestData.url + "'");
networkRequest.abort();
}
};
我还没有找到使用 HtmlUnitDriver 执行此操作的好方法。How to filter javascript from specific urls in HtmlUnit中提到了 ScriptPreProcessor ,但它使用 WebClient,而不是 HtmlUnitDriver。有任何想法吗?