简体   繁体   English

获取 web 页面上加载的所有 JS 文件的列表

[英]Get list of all JS files loaded on a web page

A bit of a newbie question but what is the best way to get a list with full name path of all JS files loaded on my website?有点新手问题,但是获取我网站上加载的所有 JS 文件的全名路径列表的最佳方法是什么?

To clarify: I have a page www.mypage.com - I want to get a list of all the JS files with their full name paths which are loaded on this page.澄清一下:我有一个页面 www.mypage.com - 我想获取所有 JS 文件的列表及其全名路径,这些文件已加载到该页面上。

You should get all the script tags first::您应该首先获取所有脚本标签::

var scripts = document.getElementsByTagName('script');

And then loop through it:然后循环遍历它:

for(var i=0;i<scripts.length;i++){
scripts[i].src;
}

Something like that will do in modern browsers with ECMAScript 6 support.在支持 ECMAScript 6 的现代浏览器中,类似的事情会发生。 However, some scripts may remove their appropriate tags which would hide them from that list.但是,某些脚本可能会删除其适当的标记,从而将它们从该列表中隐藏。

var jsFilePaths = Array.prototype.slice
    .apply(document.querySelectorAll('script'))
    .filter(s => s.src)
    .map(s => s.src);

Use normal for cycle if lambdas are not an option.如果 lambdas 不是一个选项,请使用 normal for cycle。

Scripts which were loaded through some data-loading method like XHR or JSONP and then executed through eval or new Function would not show up as well.通过某些数据加载方法(如 XHR 或 JSONP)加载然后通过evalnew Function执行的脚本也不会显示。

If you need to intercept and record XHRs and you don't have a centralized place where all XHRs are done, you will probably need to override XMLHttpRequest and add some logic to its readystatechange event to check whether the retrieved response is an executable JavaScript, see this question for more information: How to easily listen for xhr requests with javascript?如果您需要拦截和记录 XHR 并且您没有一个集中的地方来完成所有 XHR,您可能需要覆盖XMLHttpRequest并向其readystatechange事件添加一些逻辑以检查检索到的响应是否是可执行的 JavaScript,请参阅这个问题的更多信息: 如何使用javascript轻松侦听xhr请求? . .

If you need to intercept all cases when a <script> is being added to the DOM or having its src attribute manipulated, you can use DOM MutationObserver , see an MDN article for more information.如果您需要在将<script>添加到 DOM 或对其src属性进行操作时拦截所有情况,您可以使用DOM MutationObserver ,请参阅MDN 文章以获取更多信息。

You may also need to intercept document.write and see if some script adds more inline scripts.您可能还需要拦截document.write并查看某些脚本是否添加了更多内联脚本。 Furthermore, non-script elements like <img/> may run scripts as well, as in the well-known XSS hack <img src="http://nonexistent.url/x.png" onerror="alert('xss!')" /> .此外,像<img/>这样的非脚本元素也可以运行脚本,就像众所周知的 XSS hack <img src="http://nonexistent.url/x.png" onerror="alert('xss!')" />

Additionally, it is important to note that any such XHR-intercepting and especially usage of DOM Mutation Events and DOM MutationObserver are detrimental to the page's performance, sometimes rather noticeably.此外,重要的是要注意,任何此类 XHR 拦截,尤其是使用 DOM Mutation Events 和 DOM MutationObserver 都会对页面性能有害,有时甚至很明显。

You can also use the Performance API :您还可以使用性能 API

 var entries = performance.getEntriesByType('resource'); entries.map(function(entry) { if (entry.initiatorType === 'script') { console.log('JS file:', entry.name); } });

Try out this code试试这段代码

var x = document.querySelectorAll("script");
var myarray = []
for (var i=0; i<x.length; i++){
var nametext = x[i].textContent;
var cleantext = nametext.replace(/\s+/g, ' ').trim();
var cleanlink = x[i].src;
myarray.push([cleantext,cleanlink]);
};
function make_table() {
    var table = '<table><thead><th>Name</th><th>Links</th></thead><tbody>';
   for (var i=0; i<myarray.length; i++) {
            table += '<tr><td>'+ myarray[i][0] + '</td><td>'+myarray[i][1]+'</td></tr>';
    };
 
    var w = window.open("");
w.document.write(table); 
}
make_table()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保网页或android应用中的所有css和js文件都已加载? - How to make sure that all css and js files in web page or android app are loaded? Puppeteer获取有关加载页面的信息 - 加载的文件列表及其大小 - Puppeteer get information about page loaded - list of files loaded and their sizes 如何确定网页中已加载JS文件的真实层次结构 - How to determine the true hierarchy of loaded JS files in a web page 获取整个网站上加载的所有 javascript 文件列表的最佳方法 - best way to get list of all javascript files loaded on entire website CakePHP-网页尝试在webroot中加载所有js文件 - CakePHP - Web page try to load all the js files in the webroot 如何使网页在目录树中包含所有js文件? - How to have web page include all js files in a directory tree? 如何检测 web 页面的所有内容是否已加载 - How to detect if all contents of a web page are loaded 加载所有JS文件时的浏览器事件 - Browser event when all JS files loaded 如何计算页面上加载的所有 JS 文件的哈希值(验证 html5 游戏运行时) - How to calculate a hash of all JS files loaded on page (to verify html5 game runtime) 首次加载页面时未加载任何js文件 - None of js files gets loaded when a page is loaded for the first time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM