给定一个URL,脚本怎么能find哪些资源被加载?

给定一个url(例如localhost:8000 ),脚本如何find浏览器加载的资源(通过HTTP请求)?

例如,假设/路由响应:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Test</title> <link rel="stylesheet" href="/css/style.css"> </head> <body> some content <img src="/foo.png" alt=""> </body> </html> 

将被加载的资源是:

  • /css/style.css
  • foo.png

这很简单(只是通过cheerio或类似的dom迭代),但它不是原生的,我认为它应该。

响应HTML中的迭代对于额外的CSS @importbackground-image等不起作用。

用浏览器加载的CSS,图像和其他资源获取列表的本地方法是什么?

也许可以通过jsdom

像@ adeneobuild议的那样,缺less的关键字是无头浏览器 。 我通过zombie库find它非常简单。 下面你可以看到一个小例子,但是文档是一个很好的资源 。

 // Dependencies var Browser = require("zombie"); // Load localhost:9000 Browser.localhost("localhost", 9000); // Load the page from localhost, // including js, css, images and iframes var browser = new Browser({ features: "scripts css img iframe" }); // Open the page and list the resources browser.visit("/", function(error) { console.log(browser.resources.map(function (c) { return c.request.url; })); });