gpt4 book ai didi

javascript - PhantomJS 更新资源文件路径

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:04 25 4
gpt4 key购买 nike

我有一个 nodejs+phantomjs 设置。我正在尝试生成 html 并将其提供给 seo 机器人。该应用程序目前可以运行,但我想提高性能。由于加载了很多 js 文件,我试图更新 phantomjs 中的资源路径,以便资源指向本地,而不是在浏览器中发出 http 请求以提高性能。

我正在做这样的事情。

page.onResourceRequested = function (requestData, request) {

if(requestData.url.indexOf('.js') > 0) {
requestData.url = requestData.url.replace("http://foo.com/","/path to local directory of the resources");

}

但这似乎行不通。在网络选项卡中,我仍然看到从外部来源提取的资源。

有没有其他方法?我还查看了似乎没有帮助的 phantomjs 中的缓存。

非常感谢您的帮助。谢谢。

最佳答案

看了文档才找到解决方案。

请求资源

requests a resource. The first argument to the callback is the requestData metadata object. The second argument is the networkRequest object itself.

Example:

page.onResourceRequested = function(requestData, networkRequest) {
console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));
};
  • abort():中止当前网络请求。中止当前网络请求将调用 (onResourceError) 回调。

  • changeUrl(url):更改网络请求的当前URL。

request.changeUrl(url) 可用于更改 url。

http://phantomjs.org/api/webpage/handler/on-resource-requested.html

关于javascript - PhantomJS 更新资源文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223708/

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