在 Google Chrome 控制台中查看所有 JavaScript 变量的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2934787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View list of all JavaScript variables in Google Chrome Console
提问by GRboss
In Firebug, the DOM tab shows a list of all your public variables and objects. In Chrome's console you have to type the name of the public variable or object you want to explore.
在 Firebug 中,DOM 选项卡显示所有公共变量和对象的列表。在 Chrome 的控制台中,您必须输入要探索的公共变量或对象的名称。
Is there a way - or at least a command - for Chrome's console to display a list all the public variables and objects? It will save a lot of typing.
有没有办法——或者至少是一个命令——让 Chrome 的控制台显示所有公共变量和对象的列表?这将节省大量的打字。
回答by Nick Craver
Is this the kind of output you're looking for?
这是您正在寻找的输出类型吗?
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
This will list everything available on the windowobject (all the functions and variables, e.g., $and jQueryon this page, etc.). Though, this is quite a list; not sure how helpful it is...
这将列出window对象上可用的所有内容(所有函数和变量,例如,$以及jQuery在此页面上等)。不过,这是一个相当多的清单;不确定它有多大帮助...
Otherwise just do windowand start going down its tree:
否则就做window并开始沿着它的树走下去:
window
This will give you DOMWindow, an expandable/explorable object.
这会给你DOMWindow,一个可扩展/可探索的对象。
回答by Marcel Korpel
回答by arkadiusm
Open the console and then enter:
打开控制台,然后输入:
keys(window)to see variablesdir(window)to see objects
keys(window)查看变量dir(window)看对象
回答by Fabien Ménager
The windowobject contains all the public variables, so you can type it in the console and then expand to view all variables/attributes/functions.
该window对象包含所有公共变量,因此您可以在控制台中键入它,然后展开以查看所有变量/属性/函数。


回答by Max Heiber
If you want to exclude all the standard properties of the window object and view application-specific globals, this will print them to the Chrome console:
如果您想排除 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);})()
The script works well as a bookmarklet. To use the script as a bookmarklet, create a new bookmark and replace the URL with the following:
该脚本可以很好地用作书签。要将脚本用作书签,请创建一个新书签并将 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);})()
回答by Avindra Goolcharan
David Walsh has a nice solution for this. Here is my take on this, combining his solution with what has been discovered on this thread as well.
大卫沃尔什对此有一个很好的解决方案。这是我对此的看法,将他的解决方案与在此线程上发现的内容相结合。
https://davidwalsh.name/global-variables-javascript
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);
xnow has only the globals.
x现在只有全局变量。
回答by tangentstorm
Type the following statement in the javascript console:
在 javascript 控制台中键入以下语句:
debugger
Now you can inspect the global scope using the normal debug tools.
现在您可以使用普通调试工具检查全局范围。
To be fair, you'll get everythingin the windowscope, including browser built-ins, so it might be sort of a needle-in-a-haystack experience. :/
说句公道话,你会得到一切的window范围,包括浏览器的内置插件,所以它可能是某种针-IN-A-草垛体验。:/
回答by TigerBear
To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.
要在 chrome 中查看任何变量,请转到“Sources”,然后“Watch”并添加它。如果在此处添加“window”变量,则可以展开它并进行探索。
回答by James Morgan
Updated method from same articleAvindra mentioned — injects iframe and compare its contentWindowproperties to global window properties.
来自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);
})();
回答by KooiInc
You may want to try this Firebug liteextension for Chrome.
你可能想试试这个适用于 Chrome 的Firebug lite扩展。

