如果我不知道 javascript 中的完整变量名称,如何查找 variable/array

How find variable/array if I don't know full variable name in javascript

我正在尝试从站点获取更多信息,并且 variable/array 是随机定义的。像这样:

var firstvar_numbers= "already exist"
var secondvar_numbers= ["a","b","c","d"]

numbers 是网站给出的随机值。在 firefox DOM 中,当我写了一半 secondvar_ 它立即发现我想要的,因为只有一个以 second 开头的变量。我的问题是如何通过了解变量的一部分来获得 userscript/javascript 中的 value/array 变量。 不明白的例子: 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

//Code that will get array
alert("exists_"+seconpartofvar())
function seconpartofvar(){your code}

alert(autocomplate.exists_)

这里我可以在Firefox控制台写alert(exists_),它会推荐autocomplate。

因为它是在顶层用 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]);