Firebugでは、DOMタブにすべてのパブリック変数とオブジェクトのリストが表示されます。 Chromeのコンソールでは、探索したいパブリック変数またはオブジェクトの名前を入力する必要があります。
Chromeのコンソールにすべてのパブリック変数とオブジェクトのリストを表示する方法、または少なくともコマンドを入力する方法はありますか?それは多くの入力を節約するでしょう。
これはあなたが探している種類の出力ですか?
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
これはwindow
オブジェクトで利用可能なすべてのものを一覧表示します(このページの$
やjQuery
など、すべての関数と変数など)。しかし、これはかなりのリストです。それがどれほど役に立つのかわからない...
それ以外の場合はwindow
を実行してツリーの下に移動してください。
window
これはあなたにDOMWindow
という、展開可能/探査可能なオブジェクトを与えるでしょう。
コンソールを開き、次のように入力します。
keys(window)
dir(window)
window
オブジェクトにはすべてのパブリック変数が含まれているため、コンソールに入力してから展開すると、すべての変数/属性/関数を表示できます。
ウィンドウオブジェクトのすべての標準プロパティを除外し、アプリケーション固有のグローバルを表示する場合は、これらがChromeコンソールに表示されます。
(function(){var standardGlobals=["top","window","location","external","chrome","document","inlineCSS","target","width","height","canvas","data","DOMURL","img","svg","ctx","url","w","a","speechSynthesis","webkitNotifications","localStorage","sessionStorage","applicationCache","webkitStorageInfo","indexedDB","webkitIndexedDB","crypto","CSS","performance","console","devicePixelRatio","styleMedia","parent","opener","frames","self","defaultstatus","defaultStatus","status","name","length","closed","pageYOffset","pageXOffset","scrollY","scrollX","screenTop","screenLeft","screenY","screenX","innerWidth","innerHeight","outerWidth","outerHeight","offscreenBuffering","frameElement","clientInformation","navigator","toolbar","statusbar","scrollbars","personalbar","menubar","locationbar","history","screen","postMessage","close","blur","focus","ondeviceorientation","ondevicemotion","onunload","onstorage","onresize","onpopstate","onpageshow","onpagehide","ononline","onoffline","onmessage","onhashchange","onbeforeunload","onwaiting","onvolumechange","ontimeupdate","onsuspend","onsubmit","onstalled","onshow","onselect","onseeking","onseeked","onscroll","onreset","onratechange","onprogress","onplaying","onplay","onpause","onmousewheel","onmouseup","onmouseover","onmouseout","onmousemove","onmouseleave","onmouseenter","onmousedown","onloadstart","onloadedmetadata","onloadeddata","onload","onkeyup","onkeypress","onkeydown","oninvalid","oninput","onfocus","onerror","onended","onemptied","ondurationchange","ondrop","ondragstart","ondragover","ondragleave","ondragenter","ondragend","ondrag","ondblclick","oncuechange","oncontextmenu","onclose","onclick","onchange","oncanplaythrough","oncanplay","oncancel","onblur","onabort","onwheel","onwebkittransitionend","onwebkitanimationstart","onwebkitanimationiteration","onwebkitanimationend","ontransitionend","onsearch","getSelection","print","stop","open","showModalDialog","alert","confirm","Prompt","find","scrollBy","scrollTo","scroll","moveBy","moveTo","resizeBy","resizeTo","matchMedia","requestAnimationFrame","cancelAnimationFrame","webkitRequestAnimationFrame","webkitCancelAnimationFrame","webkitCancelRequestAnimationFrame","captureEvents","releaseEvents","atob","btoa","setTimeout","clearTimeout","setInterval","clearInterval","TEMPORARY","PERSISTENT","getComputedStyle","getMatchedCSSRules","webkitConvertPointFromPageToNode","webkitConvertPointFromNodeToPage","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","openDatabase","addEventListener","removeEventListener","dispatchEvent"];
var appSpecificGlobals={};
for (var w in window){
if (standardGlobals.indexOf(w)==-1) appSpecificGlobals[w]=window[w];
}
console.log(appSpecificGlobals);})()
このスクリプトはブックマークレットとしてもうまく機能します。スクリプトをブックマークレットとして使用するには、新しいブックマークを作成してURLを次のように置き換えます。
javascript:(function(){var standardGlobals=["top","window","location","external","chrome","document","inlineCSS","target","width","height","canvas","data","DOMURL","img","svg","ctx","url","w","a","speechSynthesis","webkitNotifications","localStorage","sessionStorage","applicationCache","webkitStorageInfo","indexedDB","webkitIndexedDB","crypto","CSS","performance","console","devicePixelRatio","styleMedia","parent","opener","frames","self","defaultstatus","defaultStatus","status","name","length","closed","pageYOffset","pageXOffset","scrollY","scrollX","screenTop","screenLeft","screenY","screenX","innerWidth","innerHeight","outerWidth","outerHeight","offscreenBuffering","frameElement","clientInformation","navigator","toolbar","statusbar","scrollbars","personalbar","menubar","locationbar","history","screen","postMessage","close","blur","focus","ondeviceorientation","ondevicemotion","onunload","onstorage","onresize","onpopstate","onpageshow","onpagehide","ononline","onoffline","onmessage","onhashchange","onbeforeunload","onwaiting","onvolumechange","ontimeupdate","onsuspend","onsubmit","onstalled","onshow","onselect","onseeking","onseeked","onscroll","onreset","onratechange","onprogress","onplaying","onplay","onpause","onmousewheel","onmouseup","onmouseover","onmouseout","onmousemove","onmouseleave","onmouseenter","onmousedown","onloadstart","onloadedmetadata","onloadeddata","onload","onkeyup","onkeypress","onkeydown","oninvalid","oninput","onfocus","onerror","onended","onemptied","ondurationchange","ondrop","ondragstart","ondragover","ondragleave","ondragenter","ondragend","ondrag","ondblclick","oncuechange","oncontextmenu","onclose","onclick","onchange","oncanplaythrough","oncanplay","oncancel","onblur","onabort","onwheel","onwebkittransitionend","onwebkitanimationstart","onwebkitanimationiteration","onwebkitanimationend","ontransitionend","onsearch","getSelection","print","stop","open","showModalDialog","alert","confirm","Prompt","find","scrollBy","scrollTo","scroll","moveBy","moveTo","resizeBy","resizeTo","matchMedia","requestAnimationFrame","cancelAnimationFrame","webkitRequestAnimationFrame","webkitCancelAnimationFrame","webkitCancelRequestAnimationFrame","captureEvents","releaseEvents","atob","btoa","setTimeout","clearTimeout","setInterval","clearInterval","TEMPORARY","PERSISTENT","getComputedStyle","getMatchedCSSRules","webkitConvertPointFromPageToNode","webkitConvertPointFromNodeToPage","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","openDatabase","addEventListener","removeEventListener","dispatchEvent"]; var $appSpecificGlobals={};for (var w in window){if (standardGlobals.indexOf(w)==-1) $appSpecificGlobals[w]=window[w];} window.$appSpecificGlobals=$appSpecificGlobals;console.log(window.$appSpecificGlobals);})()
David Walshはこのためのいい解決法を持っています。これが私の考えであり、彼の解決策とこのスレッドで発見されたことを組み合わせています。
https://davidwalsh.name/global-variables-javascript
x = {};
var iframe = document.createElement('iframe');
iframe.onload = function() {
var standardGlobals = Object.keys(iframe.contentWindow);
for(var b in window) {
const prop = window[b];
if(window.hasOwnProperty(b) && prop && !prop.toString().includes('native code') && !standardGlobals.includes(b)) {
x[b] = prop;
}
}
console.log(x)
};
iframe.src = 'about:blank';
document.body.appendChild(iframe);
x
はグローバルのみを持つようになりました。
Chrome用のこの Firebug lite 拡張機能を試してみてください。
同じ 記事からメソッドを更新しました Avindraが言及した - iframeを注入し、そのcontentWindow
プロパティをグローバルウィンドウプロパティと比較します。
(function() {
var iframe = document.createElement('iframe');
iframe.onload = function() {
var iframeKeys = Object.keys(iframe.contentWindow);
Object.keys(window).forEach(function(key) {
if(!(iframeKeys.indexOf(key) > -1)) {
console.log(key);
}
});
};
iframe.src = 'about:blank';
document.body.appendChild(iframe);
})();
変数をクロムで表示するには、[ソース]に移動してから[ウォッチ]して追加します。ここに "window"変数を追加すれば、それを展開して探索することができます。
JavaScriptコンソールに次の文を入力します。
debugger
これで、通常のデバッグツールを使ってグローバルスコープを調べることができます。
公平を期すために、ブラウザ組み込みを含むwindow
スコープの everything を取得するので、ちょっとした不幸な経験になるかもしれません。 :/
すべての「パブリック変数」は実際には(見ているウィンドウ/タブの)ウィンドウオブジェクトのプロパティなので、代わりに単に「ウィンドウ」オブジェクトを調べることができます。複数のフレームがある場合は、とにかく正しいウィンドウオブジェクト(Firebugのように)を選択する必要があります。
入力:コンソールでthis
、
window object
を取得するには(?)、基本的にコンソールでwindow
と入力するのと同じだと思います。
少なくともFirefoxとchromeで動作します。
この簡単なコマンドを試してください。
console.log(ウィンドウ)