如何找出在javascript中加载了哪些javascript? [英] How to find out which javascripts are loaded in javascript?
问题描述
在另一个评论上,我问自己一个问题,我问自己是否有一种方法可以获取页面上加载的所有js代码的列表.类似于萤火虫或Chrome浏览器的功能.
Following up on a comment on another I question I asked myself if there's a way to get a list of all js code that is loaded on a page. Something like what firebug or chrome inspector do.
是否有一种纯JavaScript方式?
Is there a pure javascript way for that?
一种方法是抓取脚本标签,但是那样您可能会错过动态加载的js.我希望有一个API.
One way would be to scrape for script tags, but then you could miss dynamically loaded js. I'd hope for an API.
对于另一个问题,可以对所有调用console.debug()
的脚本进行grep处理,以防止忘记它们并使它们进入生产环境.
In the case of the other question, one could grep all the scripts for calls to console.debug()
to prevent forgetting them and letting them slip into production.
谢谢
推荐答案
我想不出需要很多工作的东西.这是我最初的失败尝试.尝试迭代窗口的所有内部属性时,它会进入无限递归.
I can't think of something that doesn't require a lot of work. Here's my initial failed attempt. It gets into infinite recursion as it tries to iterate through all the inner properties of window.
/**
* You have to run this in firefox, pass window the first time
* @return boolean Whether the given object contains a function where its
* source code contains the word console.
*/
function lookForConsole( obj ) {
var found = false;
for (var prop in obj) {
var current = obj[prop];
if (typeof current == "function") {
if (current.toSource.indexOf("console" + ".log") != -1) {
found = true;
break;
}
} else if (typeof current == "object"){
found = lookForConsole(current);
if (found) {
break;
}
}
}
return found;
}
您曾经听过这样的表达:当您仅有的工具是锤子时,每个问题都像钉子一样?"
You ever hear the expression, "when the only tool you have is a hammer, every problem looks like a nail"?
您为什么要在JS中这样做?
Why would you do this in JS?
这篇关于如何找出在javascript中加载了哪些javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!