2

別の質問に対するコメントのフォローアップとして、ページにロードされているすべてのjsコードのリストを取得する方法があるかどうかを自問しました。ファイアバグやクロームインスペクターが行うことのようなもの。

そのための純粋なJavaScriptの方法はありますか?

1つの方法は、スクリプトタグをスクレイプすることですが、動的にロードされたjsを見逃す可能性があります。APIが欲しいです。

他の質問の場合、呼び出しのすべてのスクリプトをgrepして、console.debug()それらを忘れて本番環境に移行させないようにすることができます。

ありがとう

4

3 に答える 3

1

手間がかからないものは思いつきません。これが私の最初の失敗した試みです。ウィンドウのすべての内部プロパティを反復しようとすると、無限再帰になります。

/**
 * 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でこれを行うのですか?

于 2010-11-04T17:40:08.863 に答える
0

jQueryを使用:

$(document).ready(function() {


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

});

于 2010-11-03T20:54:08.200 に答える
0

これがfirebugのやり方です。それなら、もっと手の込んだ方法はないと思います。

    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

于 2010-11-05T13:49:48.823 に答える