可以列出使用JavaScript加载到网页上的外部资源吗? [英] Possible to list external resources loaded on a webpage with JavaScript?

查看:63
本文介绍了可以列出使用JavaScript加载到网页上的外部资源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个时间间隔来跟踪当前页面上正在加载的项目。例如,假设我有一个加载css文件的页面,一些脚本,图片,一个flash视频播放器,然后flash视频播放器加载一个视频文件。加载的元素可能来自或可能不是与页面相同的域。其中一些可能通过ajax或flash加载,而页面上没有标记。我想跟踪每个并创建一个存储有关它们的信息的数组。

I'd like to have an interval that keeps track of what items are being loaded on the current page. For example, let's say I have a page that loads a css file, a few scripts, pictures, a flash video player, and then the flash video player loads a video file. The elements loaded may or may not be from the same domain as the page. Some of them may be loaded through ajax or flash and not have a tag on the page. I want to keep track of each and make an array that stores information about them.

我想要一个类似于这个伪代码的脚本:

I'd like to have a script that does something similar to this pseudocode:

var all_external_resources = array();

setInterval(function() {
  var external_items = list_external_resources();
  for (var i in external_items) {
    if (all_external_resources.indexOf(external_items[i]) < 0)
      all_external_resources.push(external_items[i]);
  }
}, 100);

这可能吗?

推荐答案

您可以使用资源计时来检索资源名称:

You can possibly use Resource Timing to retrieve resource names:

var resources = window.performance.getEntriesByType("resource");
resources.forEach(function (resource) {
    console.log(resource.name);
});

这是导航时间我可以使用。 .. )并且在许多浏览器中都受支持。

It's an extension of Navigation Timing (Can I use...) and is supported in many browsers.

这篇关于可以列出使用JavaScript加载到网页上的外部资源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆