export Data in localStorage for later re-import

后端 未结 5 658
既然无缘
既然无缘 2021-01-31 00:22

I want to export a few items from my localStorage to save it externally but in a format so that I can import it again later.

My attempt was to write executable code that

相关标签:
5条回答
  • 2021-01-31 00:49

    Just an improved version of Jeremy. To simplify the process

    copy('var data = '+JSON.stringify(localStorage)+';Object.keys(data).forEach(function (k){localStorage.setItem(k, data[k]);});');
    

    Run this in console where you need to export, it copies localstorage content along with code to clipboard and just paste it in the console where you want to import.

    0 讨论(0)
  • 2021-01-31 00:51

    You can encode Objects into Strings using JSON.stringify (object to String) and decode Strings into Objects using JSON.parse (String to Object).

    Write to localStorage

    localStorage.setItem("varname",JSON.stringify(originalVarname));
    

    Read from localStorage

    var originalVarname= JSON.parse(localStorage.getItem("varname"));
    
    0 讨论(0)
  • 2021-01-31 00:51

    Here's how to import/export your entire localStorage

    Export

    copy(JSON.stringify(localStorage));
    

    This will copy your localStorage to your clipboard. (You need two JSON.stringify()'s to get the quotes escaped.)

    Import

    var data = JSON.parse(/*paste stringified JSON from clipboard*/);
    Object.keys(data).forEach(function (k) {
      localStorage.setItem(k, data[k]);
    });
    
    0 讨论(0)
  • 2021-01-31 00:54

    Export

    copy(JSON.stringify(JSON.stringify(localStorage)));
    

    Import

    var data = JSON.parse(/*previously copied stringified JSON from clipboard*/);
    Object.keys(data).forEach(function (k) {
      localStorage.setItem(k, data[k]);
    });
    
    0 讨论(0)
  • 2021-01-31 01:01

    Just a modernized version of @iceLord answer.

    Just run this in the console, it will put the code to restore the localStorage back into your clipboard.

    copy(`Object.entries(${JSON.stringify(localStorage)})
    .forEach(([k,v])=>localStorage.setItem(k,v))`)
    

    Bookmarklet version

    javascript:prompt(`localStorage from ${location.host}${new Date().toLocaleString()}`, `/* localStorage from ${location.host}${new Date().toLocaleString()}*/Object.entries(  ${JSON.stringify(localStorage)}).forEach(([k,v])=>localStorage.setItem(k,v))`)
    
    0 讨论(0)
提交回复
热议问题