Skip to content
Advertisement

How do I make an alert box’s input change the value in localstorage? [closed]

I’m trying to make a bookmarklet that modifies the “local storage” for a game online, but I want it to question the user the amount of “coins” the user wants via an alert.

This is the code

localStorage.setItem('mjs-drift-boss-game-v1.0.1-dailyreward',
 '{"sound":0.7,"music":0,"score":-3,"hasShownTutorial":true,"collectedCoin":999999,"cars":[0,1,2,3,4,5,6,7],"currentCar":7,"currentTip":0,"booster1":99999999,"booster2":9999999,"booster3":999999,"ko":0,"hasShownBoosterTutorial":true}')
alert('Modifed Game!')
location.reload();

Answer

You can do something like this:

const data = JSON.parse(localStorage.getItem('mjs-drift-boss-game-v1.0.1-dailyreward'));
const coins = window.prompt('Set Coins', data.collectedCoin);

localStorage.setItem('mjs-drift-boss-game-v1.0.1-dailyreward',
'{"sound":0.7,"music":0,"score":-3,"hasShownTutorial":true,"collectedCoin":' + coins + ',"cars":[0,1,2,3,4,5,6,7],"currentCar":7,"currentTip":0,"booster1":99999999,"booster2":9999999,"booster3":999999,"ko":0,"hasShownBoosterTutorial":true}')
alert('Modifed Game!')
location.reload();

This way you get the last stored value pre filled and you can change it to the new value, which gets stored in localStorage.

Advertisement