Google ChromeコンソールのすべてのJavaScript変数のリストを表示
-
05-10-2019 - |
質問
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);})()
デビッド・ウォルシュには、これに対する素晴らしい解決策があります。彼のソリューションとこのスレッドでも発見されたものを組み合わせることで、これについての私の見解があります。
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
現在、グローバルだけがあります。
あなたはこれを試してみたいかもしれません Firebug Lite Chromeの拡張。
同じから更新されたメソッド 記事 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);
})();
JavaScriptコンソールで次のステートメントを入力します。
debugger
これで、通常のデバッグツールを使用してグローバルスコープを検査できます。
公平を期すために、あなたは得るでしょう すべての の中に window
ブラウザの組み込みを含むスコープなので、それは一種のヘイスタックのエクスペリエンスのようなものかもしれません。 :/
Chromeの変数を表示するには、「ソース」に移動してから「監視」して追加します。ここに「ウィンドウ」変数を追加すると、拡張して探索できます。
すべての「パブリック変数」は、実際には[ウィンドウ/タブ]のウィンドウオブジェクトのプロパティであるため、代わりに「ウィンドウ」オブジェクトを検査するだけです。複数のフレームがある場合は、とにかく(FireBugのように)正しいウィンドウオブジェクトを選択する必要があります。
この簡単なコマンドを試してください:
console.log(window)
タイプ: this
コンソールで、
を取得します window object
(?)、それは基本的にタイピングと同じだと思います window
コンソールで。
少なくともFirefox&Chromeで動作します。