[英]How find variable/array if I don't know full variable name in javascript
I am trying to get additional information from site and there is variable/array that defined randomly.我正在尝试从站点获取更多信息,并且有随机定义的变量/数组。 Like this:
像这样:
var firstvar_numbers= "already exist"
var secondvar_numbers= ["a","b","c","d"]
numbers is random value that given by site.数字是网站给出的随机值。 In firefox DOM when I wrote half of
secondvar_
it immediately find that I wanted because there is only one variable that starts with second
.在 firefox DOM 中,当我写了
secondvar_
的一半时,它立即发现我想要,因为只有一个以second
开头的变量。 My question is how I can get value/array of variable in userscript/javascript by knowing part of the variable.我的问题是如何通过了解变量的一部分来获取 userscript/javascript 中的变量值/数组。 Example if you don't understood: Html
不懂的例子:Html
//Variable that I need from server
<div id="variables" class="container">
<script type="text/javascript">
var exists_73647286="hello world"
var array_636353=[62,96,11,28]
</script>
</div>
Javascript Javascript
//Code that will get array
alert("exists_"+seconpartofvar())
function seconpartofvar(){your code}
OR或者
alert(autocomplate.exists_)
Here I can write alert(exists_)
in Firefox console and it will recommend autocomplate.在这里,我可以在 Firefox 控制台中编写
alert(exists_)
,它会推荐自动编译。
Since it's declared with var
on the top level, you can iterate over the properties of the window
and find one which startsWith
what you're looking for:由于它是在顶层使用
var
声明的,因此您可以遍历window
的属性并找到一个以您要查找的内容startsWith
的:
// Example site code: var array_636353 = [62, 96, 11, 28]; // Userscript code: const prop = Object.keys(window).find(key => key.startsWith('array_')); console.log(prop, window[prop]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.