如何找出javascript中加载了哪些javascript?

发布于 2024-09-30 16:09:37 字数 254 浏览 9 评论 0原文

继另一个问题的评论之后,我问自己是否有办法获取页面上加载的所有 js 代码的列表。就像 Firebug 或 chrome Inspector 所做的那样。

有没有一个纯javascript的方法?

一种方法是抓取脚本标签,但这样你可能会错过动态加载的 js。我希望有一个API。

对于另一个问题,可以 grep 所有调用 console.debug() 的脚本,以防止忘记它们并让它们进入生产环境。

谢谢

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.

Is there a pure javascript way for that?

One way would be to scrape for script tags, but then you could miss dynamically loaded js. I'd hope for an API.

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.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-10-07 16:09:37

我想不出不需要做很多工作的事情。这是我最初失败的尝试。当它尝试迭代 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;
}

您是否听过这样一句话:“当您拥有的唯一工具是锤子时,所有问题看起来都像钉子”?

为什么要在 JS 中这样做?

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"?

Why would you do this in JS?

勿忘初心 2024-10-07 16:09:37

使用 jQuery:

$(document).ready(function() {


$('script').each(function() {
    if($(this).attr('src')) {
        alert($(this).attr('src'))
    }
    else {
        alert("inline")
    }
})

});

using jQuery:

$(document).ready(function() {


$('script').each(function() {
    if($(this).attr('src')) {
        alert($(this).attr('src'))
    }
    else {
        alert("inline")
    }
})

});

瑶笙 2024-10-07 16:09:37

这就是萤火虫的做法。我想那就没有更好的方法了。

    var doc = Firebug.browser.document;
    var scripts = doc.getElementsByTagName("script");
    var selectNode = this.selectNode = createElement("select");

    for(var i=0, script; script=scripts[i]; i++)
    {
        // Don't show Firebug Lite source code in the list of options
        if (Firebug.ignoreFirebugElements && script.getAttribute("firebugIgnore"))
            continue;

        var fileName = getFileName(script.src) || getFileName(doc.location.href);
        var option = createElement("option", {value:i});

        option.appendChild(Firebug.chrome.document.createTextNode(fileName));
        selectNode.appendChild(option);
    };

http://fbug.googlecode.com/svn /lite/branches/firebug1.3/content/firebug/script.js

This is how firebug does it. I guess there's no fancier way then.

    var doc = Firebug.browser.document;
    var scripts = doc.getElementsByTagName("script");
    var selectNode = this.selectNode = createElement("select");

    for(var i=0, script; script=scripts[i]; i++)
    {
        // Don't show Firebug Lite source code in the list of options
        if (Firebug.ignoreFirebugElements && script.getAttribute("firebugIgnore"))
            continue;

        var fileName = getFileName(script.src) || getFileName(doc.location.href);
        var option = createElement("option", {value:i});

        option.appendChild(Firebug.chrome.document.createTextNode(fileName));
        selectNode.appendChild(option);
    };

http://fbug.googlecode.com/svn/lite/branches/firebug1.3/content/firebug/script.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文