Domanda

Let's say I had an array test that was stored via GM_setValue('test', []); when the script has been run for the first time to ensure that it's there.

Now if I were to assign the local variable test to this via var test = GM_getValue('test');, push a new object at some point in the script like so test.push({id: '...'}); and then save over it by doing this GM_setValue('test', test); the stored array would be this [{ id: '...' }].

Now I need to get the value with the key id from the object in the array later in the script, so I'll simply do this test[0]['id']. However this appears to return undefined.

Alrighty, it's possible it's not storing the object for some reason, so here's what I'll do:

console.log(test);
console.log(test[0]);
console.log(test[0]['id']);

Amazingly, test returns an Array containing the object and in turn the id, test[0] returns the Object containing the id with the correct value, however test[0]['id'] still returns undefined.

This works perfectly on Chrome with Tampermonkey, but the problem arises when the script is running on Firefox with Greasemonkey. What could the problem possibly be?

È stato utile?

Soluzione

GM_setValue does not store objects; it stores strings or integers. See the doc page.

Use a serializer like GM_SuperValue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top