it-swarm.com.de

Liste aller JavaScript-Variablen in Google Chrome Console anzeigen

In Firebug zeigt die Registerkarte DOM eine Liste aller öffentlichen Variablen und Objekte. In der Chrome-Konsole müssen Sie den Namen der öffentlichen Variablen oder des Objekts eingeben, die Sie erkunden möchten.

Gibt es eine Möglichkeit - oder zumindest einen Befehl - für die Chrome-Konsole, um eine Liste aller öffentlichen Variablen und Objekte anzuzeigen? Es wird viel Tipp sparen.

204
GRboss

Ist dies die Art von Ausgabe, nach der Sie suchen?

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

Dadurch wird alles aufgelistet, was im Objekt window verfügbar ist (alle Funktionen und Variablen, z. B. $ und jQuery auf dieser Seite usw.). Dies ist zwar eine ziemlich Liste; nicht sicher, wie hilfreich es ist ...

Ansonsten mache einfach window und gehe den Baum herunter:

window

Dadurch erhalten Sie DOMWindow, ein erweiterbares/ausbaufähiges Objekt.

275
Nick Craver

Wenn die Skriptausführung angehalten wird (z. B. an einem Haltepunkt), können Sie einfach alle Globals im rechten Bereich des Developer Tools-Fensters anzeigen:

chrome-globals

68
Marcel Korpel

Öffnen Sie die Konsole und geben Sie Folgendes ein:

  • keys(window), um Variablen anzuzeigen
  • dir(window) um Objekte zu sehen 
49
arkadiusm

Das Objekt window enthält alle öffentlichen Variablen. Sie können es also in die Konsole eingeben und dann erweitern, um alle Variablen/Attribute/Funktionen anzuzeigen. 

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

34
Fabien Ménager

Wenn Sie alle Standardeigenschaften des Fensterobjekts ausschließen und anwendungsspezifische Globals anzeigen möchten, werden diese in der Chrome-Konsole gedruckt:

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

Das Skript funktioniert gut als Bookmarklet. Um das Skript als Lesezeichen zu verwenden, erstellen Sie ein neues Lesezeichen und ersetzen Sie die URL durch Folgendes:

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

David Walsh hat dafür eine schöne Lösung. Hier ist meine Meinung dazu und kombiniert seine Lösung mit dem, was in diesem Thread entdeckt wurde.

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 hat jetzt nur die Globals.

7

Sie können diese Firebug Lite Erweiterung für Chrome ausprobieren.

4
KooiInc

Um eine beliebige Variable in Chrome anzuzeigen, gehen Sie zu "Quellen" und dann "Beobachten" und fügen Sie sie hinzu. Wenn Sie hier die "Fenster" -Variable hinzufügen, können Sie sie erweitern und erkunden.

3
TigerBear

Aktualisierte Methode aus demselben article Avindra-Programm - injiziert iframe und vergleicht seine contentWindow-Eigenschaften mit den globalen Fenstereigenschaften.

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

3
jmsmrgn

Da alle "öffentlichen Variablen" tatsächlich Eigenschaften des Fensterobjekts (des Fensters/der Registerkarte, das Sie gerade betrachten) sind, können Sie stattdessen nur das "Fenster" -Objekt untersuchen. Wenn Sie mehrere Frames haben, müssen Sie trotzdem das richtige Fensterobjekt auswählen (wie in Firebug).

1
mihi

Geben Sie in der Konsole Folgendes ein: this,

um den window object zu erhalten, denke ich (?), ist es im Grunde dasselbe, als würde man window in die Konsole eingeben.

Es funktioniert zumindest in Firefox und Chrome.

1
Sebastian Norr

enter image description here

1
GRboss

Versuchen Sie diesen einfachen Befehl:

console.log (Fenster)
1
Vaclav Muller

Geben Sie die folgende Anweisung in die Javascript-Konsole ein:

debugger

Jetzt können Sie den globalen Bereich mit den normalen Debugging-Tools überprüfen.

Um fair zu sein, erhalten Sie alles im Bereich window, einschließlich der eingebauten Browser, so dass es eine Art Nadel-in-a-Heuhaufen-Erlebnis sein kann. : /

0
tangentstorm