在 Google Chrome 控制台中查看所有 JavaScript 变量的列表

IT技术 javascript google-chrome google-chrome-devtools
2021-02-14 22:01:19

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

有没有办法——或者至少是一个命令——让 Chrome 的控制台显示所有公共变量和对象的列表?这将节省大量的打字。

6个回答

这是您正在寻找的输出类型吗?

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

可这将列出一切的window对象(所有的函数和变量,例如,$jQuery此页面上,等等)。不过,这是一个相当多的清单;不知道有多大帮助...

否则就做window并开始沿着它的树走下去:

window

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

或者你可以直接输入这个;
2021-04-15 22:01:19
@CMS - 是的,这是真的......但问题是专门关于 Chrome 的,所以实现是已知的。
2021-04-17 22:01:19
我也想查看变量的值,所以我使用了: for(var b in window) { if(window.hasOwnProperty(b)) console.log(b+" = "+window[b]); }
2021-05-02 22:01:19
@ntownsend -我的控制台不同意你的观点 :)它是 的一个属性object,为什么没有它?
2021-05-08 22:01:19
“为什么没有呢?” [[Prototype]]全局对象内部属性是依赖实现的,在几乎所有主要实现中 -V8、Spidermonkey、Rhino 等,全局对象在某些时候继承自Object.prototype,但例如在其他实现中 -JScript、BESEN、DMDScript 等。 ..- 它不存在,所以window.hasOwnProperty不存在,为了测试它,我们可以:Object.prototype.isPrototypeOf(window);
2021-05-10 22:01:19

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

铬全球

我可以在不停止的情况下从执行上下文中吐出变量,例如断点显示吗?
2021-04-30 22:01:19
@MildFuzz 然后使用 Nick Craver 的解决方案(已接受的解决方案)。
2021-05-13 22:01:19

打开控制台,然后输入:

  • keys(window) 查看变量
  • dir(window) 看物体
仅供参考dir(window)在 Firefox 中不起作用(是的,我知道这个线程是关于 Chrome 的),但key(window)在 Firefox 中起作用
2021-04-24 22:01:19
dir(Function("return this")()) 使它也适用于 Web Workers
2021-05-11 22:01:19

window对象包含所有公共变量,因此您可以在控制台中键入它,然后展开以查看所有变量/属性/函数。

chrome-show-all-variables-expand-window-object

好的!到目前为止,这是最简单的方法,因为您可以递归地扩展变量。
2021-04-24 22:01:19
这是一个很好的方法,document就像浏览器通过元素和document.文档(点)来查看文档属性一样。window.document澄清了很多关于窗口与文档的混淆
2021-05-09 22:01:19

如果您想排除 window 对象的所有标准属性并查看特定于应用程序的全局变量,这会将它们打印到 Chrome 控制台:

{

    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);

    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
}

该脚本可以很好地用作书签。要将脚本用作书签,请创建一个新书签并将 URL 替换为以下内容:

javascript:(() => {
    const standardGlobals = new Set(["window", "self", "document", "name", "location", "customElements", "history", "locationbar", "menubar", "personalbar", "scrollbars", "statusbar", "toolbar", "status", "closed", "frames", "length", "top", "opener", "parent", "frameElement", "navigator", "origin", "external", "screen", "innerWidth", "innerHeight", "scrollX", "pageXOffset", "scrollY", "pageYOffset", "visualViewport", "screenX", "screenY", "outerWidth", "outerHeight", "devicePixelRatio", "clientInformation", "screenLeft", "screenTop", "defaultStatus", "defaultstatus", "styleMedia", "onsearch", "isSecureContext", "performance", "onappinstalled", "onbeforeinstallprompt", "crypto", "indexedDB", "webkitStorageInfo", "sessionStorage", "localStorage", "onabort", "onblur", "oncancel", "oncanplay", "oncanplaythrough", "onchange", "onclick", "onclose", "oncontextmenu", "oncuechange", "ondblclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "ondurationchange", "onemptied", "onended", "onerror", "onfocus", "onformdata", "oninput", "oninvalid", "onkeydown", "onkeypress", "onkeyup", "onload", "onloadeddata", "onloadedmetadata", "onloadstart", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreset", "onresize", "onscroll", "onseeked", "onseeking", "onselect", "onstalled", "onsubmit", "onsuspend", "ontimeupdate", "ontoggle", "onvolumechange", "onwaiting", "onwebkitanimationend", "onwebkitanimationiteration", "onwebkitanimationstart", "onwebkittransitionend", "onwheel", "onauxclick", "ongotpointercapture", "onlostpointercapture", "onpointerdown", "onpointermove", "onpointerup", "onpointercancel", "onpointerover", "onpointerout", "onpointerenter", "onpointerleave", "onselectstart", "onselectionchange", "onanimationend", "onanimationiteration", "onanimationstart", "ontransitionrun", "ontransitionstart", "ontransitionend", "ontransitioncancel", "onafterprint", "onbeforeprint", "onbeforeunload", "onhashchange", "onlanguagechange", "onmessage", "onmessageerror", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onrejectionhandled", "onstorage", "onunhandledrejection", "onunload", "alert", "atob", "blur", "btoa", "cancelAnimationFrame", "cancelIdleCallback", "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", "createImageBitmap", "fetch", "find", "focus", "getComputedStyle", "getSelection", "matchMedia", "moveBy", "moveTo", "open", "postMessage", "print", "prompt", "queueMicrotask", "releaseEvents", "requestAnimationFrame", "requestIdleCallback", "resizeBy", "resizeTo", "scroll", "scrollBy", "scrollTo", "setInterval", "setTimeout", "stop", "webkitCancelAnimationFrame", "webkitRequestAnimationFrame", "chrome", "caches", "ondevicemotion", "ondeviceorientation", "ondeviceorientationabsolute", "originAgentCluster", "cookieStore", "showDirectoryPicker", "showOpenFilePicker", "showSaveFilePicker", "speechSynthesis", "onpointerrawupdate", "trustedTypes", "crossOriginIsolated", "openDatabase", "webkitRequestFileSystem", "webkitResolveLocalFileSystemURL"]);
    for (const key of Object.keys(window)) {
        if (!standardGlobals.has(key)) {
            console.log(key)
        }
    }
})()
这是当前 Chrome 和 Firefox 默认全局变量的列表:pastebin.com/wNj3kfg0
2021-04-22 22:01:19
2021 年更新,但其他一些答案似乎常青。
2021-05-05 22:01:19
不幸的是一个过时的列表,但仍然非常有帮助!如果我有更多的时间,我会提供一种方法来以简单的方式在空白窗口中获取当前的标准全局变量(几个小时后提醒我,我应该有一些时间,以防有人感兴趣并且不能这样做它自己)。
2021-05-10 22:01:19