在Google Chrome控制台中查看所有JavaScriptvariables的列表

在Firebug中,DOM选项卡显示了所有公共variables和对象的列表。 在Chrome的控制台中,您必须键入要探索的公共variables或对象的名称。

有没有一种方法 – 或者至less是一个命令 – Chrome的控制台显示列出所有的公共变​​量和对象? 这将节省很多打字。

这是你正在寻找的输出?

for(var b in window) { if(window.hasOwnProperty(b)) console.log(b); } 

这将列出window对象(所有的函数和variables,例如这个页面上的$jQuery等)上可用的所有东西。 虽然,这是一个相当的名单,不知道这是多么有帮助…

否则,只要做window并开始下去它的树:

 window 

这会给你DOMWindow ,一个可扩展/可探索的对象。

当脚本执行停止时(例如,在断点处),您可以简单地在“开发人员工具”窗口的右窗格中查看所有全局variables:

铬全局

打开控制台,然后input:

  • keys(window)来查看variables
  • dir(window)来查看对象

window对象包含所有的公共变​​量,所以你可以在控制台中键入,然后展开查看所有variables/属性/function。

铬显示,所有变量展开窗口对象

如果您想要排除窗口对象的所有标准属性并查看特定于应用程序的全局variables,则会将它们打印到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);})() 

该脚本作为一个小书签。 要将该脚本用作书签,请创build一个新的书签,并使用以下内容replaceURL:

 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);})() 

大卫·沃尔什有一个很好的解决scheme。 这是我的承诺,结合他的解决scheme,以及在这个线程上发现的。

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现在只有全局variables。

您可能想要试用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); })(); 

在这里输入图像说明

要查看chrome中的任何variables,请转到“Sources”,然后select“Watch”并将其添加。 如果你在这里添加“窗口”variables,那么你可以扩展它并探索。

由于所有“公共variables”实际上是窗口对象的属性(您正在查看的窗口/选项卡的属性),因此您可以仅检查“窗口”对象。 如果你有多个框架,你将不得不select正确的窗口对象(就像在Firebug中一样)。