回答:
これはあなたが探している種類の出力ですか?
for(var b in window) {
if(window.hasOwnProperty(b)) console.log(b);
}
この意志のリストで利用可能なすべてwindow
のオブジェクト(たとえば、すべての関数と変数、$
およびjQuery
このページの、など)。しかし、これはかなりのリストです。それがどれほど役立つかわかりません...
それ以外の場合はwindow
、実行してツリーを下っていきます。
window
これDOMWindow
により、展開可能/探索可能なオブジェクトが提供されます。
[[Prototype]]
グローバルオブジェクトの内部プロパティは実装に依存します。ほとんどすべての主要な実装(V8、Spidermonkey、Rhinoなど)では、グローバルオブジェクトはある時点で継承しますObject.prototype
が、たとえば、他の実装では-JScript、BESEN、DMDScriptなどです。 ..-それは、そうしないwindow.hasOwnProperty
私達ができることをテストするために、存在しません:Object.prototype.isPrototypeOf(window);
for(var b in window) { if(window.hasOwnProperty(b)) console.log(b+" = "+window[b]); }
コンソールを開き、次のように入力します。
keys(window)
変数を確認するにはdir(window)
オブジェクトを見る dir(Function("return this")())
Webワーカーでも機能します
dir(window)
はFirefoxでは機能しません(そうです、このスレッドはChromeに関するものでした)がkey(window)
、Firefoxでは機能します
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
現在はグローバルのみです。
prop.toString
どこにでも存在するようには見えないので、条件はより防御的である可能性がありますif(window.hasOwnProperty(b) && prop && (prop.toString && !prop.toString().includes('native code')) && !standardGlobals.includes(b))
JavaScriptコンソールに次のステートメントを入力します。
debugger
これで、通常のデバッグツールを使用してグローバルスコープを検査できます。
公平を期すために、ブラウザーの組み込みを含め、スコープ内のすべてのものを取得するwindow
ので、これは一種の干し草のような体験になるかもしれません。:/
この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);
})();
タイプ:this
コンソールで、
window object
私が思う(?)を得るために、それは基本的にタイピングと同じだと思いますwindow
にはコンソールでの。
少なくともFirefoxとChromeでは機能します。
object
、なぜそれがないのですか?