Last active
October 12, 2022 10:41
-
-
Save ubershmekel/9f42b1165cea49b2e2676f20f72e979a to your computer and use it in GitHub Desktop.
Find an ace editor in your window or any other object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Paste this into JS to find an ACE editor in your browser window | |
// Based off of https://stackoverflow.com/questions/12102425/recursively-search-for-a-value-in-global-variables-and-its-properties/12103127#12103127 | |
function isTarget(obj) { | |
// return typeof obj == typeof value && obj == value | |
return obj && obj['getSelectedText']; | |
} | |
function globalSearch(startObject, isTargetFunc) { | |
var stack = [[startObject, '']]; | |
var searched = []; | |
var found = false; | |
var isArray = function (test) { | |
return Object.prototype.toString.call(test) === '[object Array]'; | |
} | |
while (stack.length) { | |
var fromStack = stack.pop(); | |
var obj = fromStack[0]; | |
var address = fromStack[1]; | |
try { | |
if (isTargetFunc(obj)) { | |
var found = address; | |
break; | |
} else if (typeof obj == "object" && searched.indexOf(obj) == -1) { | |
if (isArray(obj)) { | |
var prefix = '['; | |
var postfix = ']'; | |
} else { | |
var prefix = '.'; | |
var postfix = ''; | |
} | |
for (i in obj) { | |
stack.push([obj[i], address + prefix + i + postfix]); | |
} | |
searched.push(obj); | |
} | |
} catch (err) { | |
console.log("error at", address, err); | |
} | |
} | |
return found == '' ? true : found; | |
} | |
globalSearch(window, isTarget); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment