Last active
May 5, 2020 15:56
-
-
Save stayradiated/1453e075df75ab5f55d0 to your computer and use it in GitHub Desktop.
Import & Export localStorage between domains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function exportLocalStorage () { | |
var output = {}; | |
for (var key in localStorage) { | |
if (!localStorage.hasOwnProperty(key)) { | |
continue; | |
} | |
output[key] = localStorage[key]; | |
} | |
return JSON.stringify(output).replace(/'/g, '\\\'').replace(/"/g, '\\"'); | |
} | |
function importLocalStorage (input) { | |
input = JSON.parse(input); | |
localStorage.clear(); | |
for (var key in input) { | |
if (!input.hasOwnProperty(key)) { | |
continue; | |
} | |
localStorage.setItem(key, input[key]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment