Javascript Object Property Undefined (GM_getValue(), GM_list

Javascript Object Property Undefined (GM_getValue(), GM_listValues(), Greasemonkey, Tampermonkey)

本文关键字:GM list getValue Object Property Javascript Undefined      更新时间:2023-09-26
这可能

很简单,我不知道,但我正在尝试从 Tampermonkey(基本上是 Chrome 的 Greasemonkey)返回的对象创建一个字符串GM_getValue()函数。

最终代码如下所示...

//Call GM_listValues() to get keys of all stored values
var keys = GM_listValues();
//Code to create CSV string out of first 10 records
var csvString = '';
for (j=0; j<10; ++j) {
    var temp = GM_getValue(keys[j]);
    csvString = csvString + '"' + temp.id + '"' + ",";
    csvString = csvString + '"' + temp.time + '"' + ",";
    csvString = csvString + '"' + temp.outcome + '"' + ",";
    csvString = csvString + '"' + temp.condition + '"' + ",";
    csvString = csvString + '"' + temp.wager + '"' + ",";
    csvString = csvString + '"' + temp.chance + '"' + ",";
    csvString = csvString + '"' + temp.profit + '"' + ",";
    csvString = csvString + ''n';
}
//Code to download CSV
var a         = document.createElement('a');
a.href        = 'data:attachment/csv,' +  encodeURIComponent(csvString);
a.target      = '_blank';
a.download    = 'myFile.csv';
document.body.appendChild(a);
a.click();

但是,结果是一堆未定义的字段。 调试时,我看到变量keys被正确填充为键。 我看到变量temp也获得了正确的数据。 当我做typeof temp时,它说object所以我认为它是一个对象。

当我console.log(temp)时,它说json_val: {"id":"2132867232","time":"07:52","outcome":"84","condition":">1","wager":"0.50000m฿","chance":"98 %","profit":"+0.00520"}

json_val:与为什么console.log(temp.id)退货undefined有关吗?

我也尝试了temp["id"],也得到了未定义。

任何帮助都非常感谢。 提前谢谢。

确保在

文件顶部包含授权。

// ==UserScript==
// @grant     GM_getValue
// @grant     GM_listValues
// ==/UserScript==